Monday, November 1, 2010

java calculate the time difference

Core Tip: The following procedure describes the time in JAVA in comparison, to obtain the time difference. For example, if you want to implement a function, can be submitted within 30 minutes, not more than 30 minutes after the submission. Then: calculate the time difference ** / SimpleDateFormat sdf = new SimpleDateFormat ('yyyy-MM-dd HH: mm: ss'); S. ..

 The following procedure describes the time in JAVA in comparison, to obtain the time difference. For example, if you want to implement a function, can be submitted within 30 minutes, not more than 30 minutes after the submission. Then:
Calculate the time difference ** /

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");

String systemTime = sdf. Format (new Date ()). ToString ();

To the time of the interception time format string into a string ** /

Date begin = sdf.parse (2008-03-28 11:55:30);

Date end = sdf.parse (systemTime);

long between = (end.getTime ()-begin.getTime ()) / 1000; / / divide by 1000 to convert seconds

long day = between / (24 * 3600);

long hour = between% (24 * 3600) / 3600;

long minute = between% 3600/60;

long second = between% 60/60;

if ((hour == 0) & & (day == 0) & & (minute <= 30)) {/****/}

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

Method One:

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)

{

}

Method Two:

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");

Method three:

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");

I am here out of a mistake, not the number of days to double check that the format of time

Here dateA, dateB format with the same yyyy-MM-dd! Remember!

/ **

* Based on the two dates, the number of days apart to obtain

* Method Name:

* @ Param dateA

* @ Param dateB

* @ Return

* /

public static int getBetweenDayNumber (String dateA, String dateB) {

long dayNumber = 0;

long DAY = 24L * 60L * 60L * 1000L;

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd");

try {

java.util.Date d1 = df.parse (dateA);

java.util.Date d2 = df.parse (dateB);

dayNumber = (d2.getTime () - d1.getTime ()) / DAY;

} Catch (Exception e) {

e.printStackTrace ();

}

return (int) dayNumber;

}

No comments:

Post a Comment