Saturday, September 4, 2010

java calculate the time difference

The following procedures are introduced for time in JAVA in comparison, was the time difference. For example, if you want to implement a function, can be submitted within 30 minutes, not more than 30 minutes after submission. Then: calculate the time difference ** / SimpleDateFormat sdf = new SimpleDateFormat ('yyyy-MM-dd HH: mm: ss'); S. ..
The following procedures are introduced for time in JAVA in comparison, was the time difference. For example, if you want to implement a function, can be submitted within 30 minutes, not more than 30 minutes after submission. Then:
Calculate the time difference ** /
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss");
String systemTime = sdf. Format (new Date ()). ToString ();
The interception to the time string time format 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; / / divided 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: Now is 2004-03-26 13:31:40
Past :2004-01-02 11:30:24
I now have two dates is poor, poor form: XX day of XX hours XX min XX sec
Method 1:
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 2:
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 3:
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; / / divided 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 days, careful examination revealed the format of time
Here dateA, dateB format with yyyy-MM-dd line! Remember!
/ **
* Based on two dates, to obtain the number of days apart
* 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