The example of this article tells the way to calculate the time difference in Java. Share it for everyone for your reference. The specifics are as follows:
Suppose it is 2004-03-26 13:31:40
The past is: 2004-01-02 11:30:24
To get two date differences, the form of difference is: xx Tianxiang XX hour xx, XX seconds
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 two:
SimpleDateFormat df = New SimpleDateFormat ("Yyyy-MM-DD HH: MM: SS"); java.util.date now = df.parse ("2004-03-26 131: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 hours = (L/(60*60*1000) -Day*24); long min = ((l/(60*1000))-Day*24*60-Hour*60); Long s = (l/l/ 1000-day*24*60*60-hour*60*60-min*60); System.out.println (""+Day+"Tian"+Hour+"Hour"+min+"+s+" second ") ;;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; // Except 1000 to convert to 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+"Sky"+Hour1+"Hour"+Minute1+"divide"+second1+"second");It is hoped that this article is helpful to everyone's Java program design.