Calculation time difference **/
| The code is as follows: |
Copy code |
|
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String systemTime = sdf. format (new Date (). toString ();
Convert the intercepted time string to a time format string **/
Date begin = sdf. parse (11:55:30 );
Date end = sdf. parse (systemTime );
Long between = (end. getTime ()-begin. getTime ()/1000; // divide by 1000 to convert to seconds
Long day = between/(24*3600 );
Long hour = maid % (24*3600)/3600;
Long minute = maid % 3600/60;
Long second = maid % 60/60;
If (hour = 0) & (day = 0) & (minute <= 30 )){/****/} |
For example, it is 13:31:40.
Past: 11:30:24
Now I want to get two date differences in the form of: XX days XX Hours XX minutes XX seconds
Here, dateA and dateB must be in the same format as yyyy-MM-dd! Remember!
Instance
| The code is as follows: |
Copy code |
/**
* Root ???? Date, get the days separated ?? Br/> * 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;
} |
Instance
| The code is as follows: |
Copy code |
|
SimpleDateFormat dfs = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Java. util. Date begin = dfs. parse ("11:30:24 ");
Java. util. Date end = dfs. parse ("13:31:40 ");
Long between = (end. getTime ()-begin. getTime ()/1000; // divide by 1000 to convert to seconds
Long day1 = between/(24*3600 );
Long hour1 = between % (24*3600)/3600;
Long minute1 = maid % 3600/60;
Long second1 = maid % 60/60;
System. out. println ("" + day1 + "day" + hour1 + "hour" + minute1 + "minute" + second1 + "second ");
|
Instance
A simple example of calculating the number of days between two dates in java
| The code is as follows: |
Copy code |
|
Package com. color. program;
Import java. text. DateFormat;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
Public class CompareTime {
Public static void main (String [] args ){
String t1 = "2008-05-09 ";
String t2 = "2008-5-13 ";
Try {
System. out. println (CompareTime. getBetweenDays (t1, t2 ));
} Catch (ParseException e ){
E. printStackTrace ();
}
}
/**
* Get the time interval of two time periods
* Return the number of days between t2 and t1
* Throws ParseException: if the input date format is not 0000-00-00, an exception is thrown.
*/
Public static int getBetweenDays (String t1, String t2) throws ParseException {
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd ");
Int betweenDays = 0;
Date d1 = format. parse (t1 );
Date d2 = format. parse (t2 );
Calendar c1 = Calendar. getInstance ();
Calendar c2 = Calendar. getInstance ();
C1.setTime (d1 );
C2.setTime (d2 );
// Ensure that the second time must be later than the first time
If (c1.after (c2 )){
C1 = c2;
C2.setTime (d1 );
}
Int betweenYears = c2.get (Calendar. YEAR)-c1.get (Calendar. YEAR );
BetweenDays = c2.get (Calendar. DAY_OF_YEAR)-c1.get (Calendar. DAY_OF_YEAR );
For (int I = 0; I <betweenYears; I ++ ){
C1.set (Calendar. YEAR, (c1.get (Calendar. YEAR) + 1 ));
BetweenDays + = c1.getMaximum (Calendar. DAY_OF_YEAR );
}
Return betweenDays;
}
} |
(Format. parse (t1). getTime ()-format. parse (t2). getTime () is an interval of one year. If this data is very large for many years, will there be exceptions?
Instance
Example code for calculating the time difference between two dates:
| The code is as follows: |
Copy code |
|
Public static void main (String [] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Date currentTime = new Date ();
// Convert the intercepted time string into a time format string
Date beginTime = sdf. parse ("12:53:30 ");
// The default value is Millisecond. Divide by 1000 to convert to seconds.
Long interval = (currentTime. getTime ()-beginTime. getTime ()/1000; // second
Long day = interval/(24*3600); // day
Long hour = interval % (24*3600)/3600; // hour
Long minute = interval % 3600/60; // minute
Long second = interval % 60; // second
System. out. println ("two time differences:" + day + "day" + hour + "hour" + minute + "minute" + second + "second ");
}
|
You can convert the date time of the database to the number of milliseconds, and then calculate the time difference between them. If you only want to calculate the difference between the two time periods for a few days, you can: interval seconds/60*60*24.
Note: The date format in the database or provided must be the same as that in SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). Otherwise, errors may occur.