Java string to compare the size of the date and time

2011-05-08  来源:本站原创  分类:Java  人气:149 

Method One:
DateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd");
String dateBegin = fmt.format (carrierCommand.getDateBegin ());
String dateEnd = fmt.format (carrierCommand.getDateEnd ());
/ / If the date format is not available '2008-05-22 ', we must look to the date format
String dateBegin = request.getParameter ("dateBegin");
String dateEnd = request.getParameter ("dateEnd");
if (java.sql.Date.valueOf (dateBegin). after (java.sql.Date.valueOf (dateEnd))) {
/ / Start date is greater than end date
errors.rejectValue ("dateEnd", null, "Start Date must be less than the end date!");
}
StringBuffer timeBegin = new StringBuffer ();
StringBuffer timeEnd = new StringBuffer ();
timeBegin.append (request.getParameter ("timeBegin"));
timeEnd.append (request.getParameter ("timeEnd"));
timeBegin.append (": 00");
timeEnd.append (": 00");
if (java.sql.Date.valueOf (dateBegin). equals (java.sql.Date.valueOf (dateEnd)))
{/ / Start Date End Date is equal to
if (java.sql.Time.valueOf (timeBegin.toString ()). equals (java.sql.Time.valueOf
(TimeEnd.toString ())))// the same time
{
errors.rejectValue ("timeEnd", null, "the same start and end date, start time must be less than the end of time!");
}
if (java.sql.Time.valueOf (timeBegin.toString ()). after (java.sql.Time.valueOf
(TimeEnd.toString ())))// the same time
{
errors.rejectValue ("timeEnd", null, "the same start and end date, start time must be less than the end of time!");
}
}

Method Two:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/ **
*
* @ Author hp
* /
public class test {

public static void main (String args []) {
int i = compare_date ("1995-11-12 15:21", "1999-12-11 09:59");
System.out.println ("i ==" + i);
}

public static int compare_date (String DATE1, String DATE2) {

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd hh: mm");
try {
Date dt1 = df.parse (DATE1);
Date dt2 = df.parse (DATE2);
if (dt1.getTime ()> dt2.getTime ()) {
System.out.println ("dt1 dt2 before in");
return 1;
} Else if (dt1.getTime () <dt2.getTime ()) {
System.out.println ("dt1 dt2 after the");
return -1;
} Else {
return 0;
}
} Catch (Exception exception) {
exception.printStackTrace ();
}
return 0;
}
}

Method three:
For example: It is now 2004-03-26 13:31:40
Past :2004-01-02 11:30:24
I now get two dates is poor, poor form: XX XX days XX hours XX minutes seconds

java calculate the time difference and compare the size of time such as: It is now 2004-03-26 13:31:40
Past :2004-01-02 11:30:24
I now get two dates is poor, poor form: XX XX days XX hours XX minutes for a second:
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
try
{
Date d1 = df.parse ("2004-03-26 13:31:40");
Date d2 = df.parse ("2004-01-02 11:30:24");
long diff = d1.getTime () - d2.getTime ();
long days = diff / (1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}

II:
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
java.util.Date now = df.parse ("2004-03-26 13:31:40");
java.util.Date date = df.parse ("2004-01-02 11:30:24");
long l = now.getTime ()-date.getTime ();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000)-day * 24);
long min = ((l / (60 * 1000))-day * 24 * 60-hour * 60);
long s = (l/1000-day * 24 * 60 * 60-hour * 60 * 60-min * 60);
System.out.println ("" + day + "days" + hour + "hours" + min + "minutes" + s + "seconds");

III:
SimpleDateFormat dfs = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
java.util.Date begin = dfs.parse ("2004-01-02 11:30:24");
java.util.Date end = dfs.parse ("2004-03-26 13:31:40");
long between = (end.getTime ()-begin.getTime ()) / 1000; / / divide by 1000 to convert seconds
long day1 = between / (24 * 3600);
long hour1 = between% (24 * 3600) / 3600;
long minute1 = between% 3600/60;
long second1 = between% 60/60;
System.out.println ("" + day1 + "days" + hour1 + "hours" + minute1 + "sub" + second1 + "seconds");
================================================== ==

Comparison of time the size of java
String s1 = "2008-01-25 09:12:09";
String s2 = "2008-01-29 09:12:11";
java.text.DateFormat df = new java.text.SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
java.util.Calendar c1 = java.util.Calendar.getInstance ();
java.util.Calendar c2 = java.util.Calendar.getInstance ();
try
{
c1.setTime (df.parse (s1));
c2.setTime (df.parse (s2));
} Catch (java.text.ParseException e) {
System.err.println ("incorrect format");
}
int result = c1.compareTo (c2);
if (result == 0)
System.out.println ("c1 equal to c2");
else if (result <0)
System.out.println ("c1 is less than c2");
else
System.out.println ("c1 over c2");
For your safety, please only open source and reliable website

Open the Web site to cancel from: http://hi.baidu.com/pengwenfu/blog/item/d7a8af1e1b4f12c4a686691e.html

相关文章
  • Java string to compare the size of the date and time 2011-05-08

    Method One: DateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd"); String dateBegin = fmt.format (carrierCommand.getDateBegin ()); String dateEnd = fmt.format (carrierCommand.getDateEnd ()); / / If the date format is not available '2008-05-22 ',

  • Java String的序列化小结 2013-11-20

    String对我们来说太熟悉了,因为它无处不在,更因为用String可以描述这个世界几乎所有的东西,甚至于为了描述精确的数值都需要String出马(因为计算机眼中的二进制和人类眼中的十进制间总有那么点隔膜).因为熟悉而变得简单,也容易被忽略.今天记录一下关于String的容易被忽略的两个问题. 字符串重用--节省内存 因为字符串太多,如果能够重用则能够节省很大的内存.首先看下面一个例子: String string1 = "HELLOHELLO"; String string2 = &

  • Mysql in time and compare the size of a subtraction 2010-06-11

    Using the Oracle to_date function of time used, and reduction of time in Oracle is also very simple, you can directly add and subtract. Mysql in a function of time in many, very free. Often used in the project is the time of addition and subtraction.

  • Java String type parameters in the problem resolution 2010-08-31

    First, the introduction of the sample public class StringAsParamOfMethodDemo { public static void main(String[] args) { StringAsParamOfMethodDemo sapm = new StringAsParamOfMethodDemo(); sapm.testA(); } private void testA() { String originalStr = "ori

  • C / C + + & Java string comparison splicing efficiency 2011-01-09

    C / C + + & Java string comparison splicing efficiency C / C + + splicing as a string of many, especially the so-called "VC + +" language (should not be called the language, but many use the VC + + programmers thrown it into a VC + + languag

  • C / C + + & Java string splicing efficiency of contrast (upper) [content over the length is truncated to complete documentation and source code can be downloaded] 2011-01-09

    C / C + + & Java string comparison splicing efficiency C / C + + splicing as a string of many, especially the so-called "VC + +" language (should not be called the language, but many use the VC + + programmers thrown it into a VC + + languag

  • C / C + + & Java string comparison splicing efficiency (lower) [content over the length is truncated to complete documentation and source code can be downloaded] 2011-01-10

    C / C + + string stitching test run results: Release version. TEST_TIME: 90000 C language memcpy append Used: 0 ms. string length: 630000 std:: string + = operator Used: 15 ms. string length: 630000 std:: string append Used: 16 ms. string length: 630

  • Java String object on a small summary in the 2011-05-05

    String is a pair of double quotes character sequence, in the JAVA language, the string data is actually achieved by the String class. Java string class is divided into two types: one is the change in the program will not be the same length of string;

  • Mysql in time and compare the size of the addition and subtraction 2010-08-24

    Using the Oracle to_date function of time used, time of addition and subtraction in Oracle is also very simple, you can directly add and subtract. Mysql in a function of time in many, very free. Frequently used in the project is the time of addition

  • java string encoding conversion 2010-03-29

    java string encoding conversion, whether the program localization is international, will involve the conversion of character encoding issues. Especially in web applications often need to deal with Chinese characters, when a string is needed, encoding

  • java string remove spaces. Enter. linefeed. Tab 2008-11-21

    java string remove spaces, carriage returns, line breaks, tabs, this method uses a regular expression java public class StringUtil ( public static void replaceBlank () ( Pattern p = Pattern.compile ( "\ \ s * | \ t | \ R | \ N"); String str = &q

  • Diagram to show java string's immutability 2010-07-06

    Diagram to show java string's immutability

  • [Transfer] java string to determine whether the number in the three methods 2010-10-29

    java string to determine whether the numbers in the three methods A function that comes with JAVA public static boolean isNumeric (String str) { for (int i = str.length ();-- i> = 0;) { if (! Character.isDigit (str.charAt (i))) { return false; } } re

  • About java String split 2011-05-08

    java String split deal with the problem public static void main (String args []) { String s = "a.txt"; String a [] = s.split ("."); System.out.println (a.length); } The output is 0 is not 2? Best answer . In the regular expression whic

  • java string 转date方法如何实现 2013-11-16

    在开发应用中经常会使用到java string 转date这种不是很常见的做法,本文将以此问题提供详细解决方案,需要了解的朋友可以参考下 针对JSON 返回String 类型 两次格式化就行了,例如: Java代码 String s = "2012-08-25"; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateForm

  • Java String字符串和Unicode字符相互转换代码 2014-09-25

    这篇文章主要介绍了Java String字符串和Unicode字符相互转换代码,需要的朋友可以参考下 java环境安装后jdk的bin目录有个native2ascii.exe可以实现类似的功能,但是通过java代码也可以实现同样的功能. 字符串转换unicode java方法代码片段: /** * 字符串转换unicode */ public static String string2Unicode(String string) { StringBuffer unicode = new Stri

  • java string类的常用方法详细介绍 2015-01-05

    在开发过程中经常会使用到java string类的方法,本文将以此问题进行详细介绍 String : 字符串类型 一.构造函数 String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. String(Sting original):构造一个original的副本.即:拷贝一个original. String(StringBuffer buffer):通过StringBuffer数组构造字符串对象.

  • Java String 字符串 2014-06-25

    转载自:http://my.codeweblog.com/brucelee80/blog String String是引用类型而非基本数据类型,代表不可变的字符序列. String对象的创建,比如: String str1 = new String("Java"); // 其他构造方法请参考API 其中的"Java"是字面量(直接量),上面的语句实际创建了两个String对象,一个是"Java"对象,存储在常量池中,一个是new时在堆中分配的内存

  • java:String类hashCode()的实现 2014-10-30

    一个对象只有一个hashcode,多个对象的hashcode可能相同. 源代码位置:java-source/java/lang/String.java,hashCode()方法: public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i

  • Java.String(学习笔记) 2015-01-08

    学习Java时的代码笔记,排版可能有些问题,代码拿来直接就可以用,毕竟全手打,难免会有错,欢迎指正 /* String类 字符串是一个特殊的对象 字符串一旦初始化就不可以被改变 String str = "abc"; String str1 = new String("abc"); 有什么区别? */ class StringMainDemo1 { public static void main(String[] args) { String s = new Str