Java calculation time difference

Source: Internet
Author: User
Tags date now dateformat

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
Method 1:

Java Code
  1. Dateformat df =NewSimpledateformat ("Yyyy-mm-dd hh: mm: SS");
  2. Try
  3. {
  4. Date d1 = DF. parse ("13:31:40");
  5. Date D2 = DF. parse ("11:30:24");
  6. LongDiff = d1.gettime ()-d2.gettime ();
  7. LongDays = diff /(1000*60*60*24);
  8. }
  9. Catch(Exception E)
  10. {
  11. }
Dateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); try {date d1 = DF. parse ("13:31:40"); Date D2 = DF. parse ("11:30:24"); long diff = d1.gettime ()-d2.gettime (); long days = diff/(1000*60*60*24);} catch (exception E) {}

Method 2:

Java code
  1. Simpledateformat df =NewSimpledateformat ("Yyyy-mm-dd hh: mm: SS");
  2. Java. util. Date Now = DF. parse ("13:31:40");
  3. Java. util. Date = DF. parse ("11:30:24");
  4. LongL = now. gettime ()-date. gettime ();
  5. LongDay = L /(24*60*60*1000);
  6. long hour = (L/( 60 * 60 * 1000 )-day * 24 );
  7. long min = (L/( 60 * 24 * 60 -hour * 60 );
  8. long S = (L/ 1000 -day * 24 * 60 * 60 -hour * 60 * 60 -min * 60 );
  9. System. Out. println (""+ Day +"Day"+ Hour +"Hour"+ Min +"Points"+ S +"Seconds");
 
Simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Java. util. date Now = DF. parse ("13:31:40"); Java. util. date = DF. parse ("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 + "day" + hour + "Hour" + min + "Minute" + S + "second ");


Method 3:

Java code
  1. Simpledateformat DFS =NewSimpledateformat ("Yyyy-mm-dd hh: mm: SS");
  2. Java. util. date begin = DFS. parse ("11:30:24");
  3. Java. util. date end = DFS. parse ("13:31:40");
  4. LongBetween = (end. gettime ()-begin. gettime ())/1000;// Divide by 1000 to convert to seconds
  5. LongDay1 = /(24*3600);
  6. LongHour1 = between % (24*3600)/3600;
  7. LongMinute1 = between %3600/60;
  8. LongSecond1 = between %60/60;
  9. System. Out. println (""+ Day1 +"Day"+ Hour1 +"Hour"+ Minute1 +"Points"+ Second1 +"Seconds");
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 = between % 3600/60; long second1 = between % 60/60; system. out. println ("" + day1 + "day" + hour1 + "Hour" + minute1 + "Minute" + second1 + "second ");


========================================================== ================
Java comparison time size

Java code
  1. String S1 ="2008-01-25 09:12:09";
  2. String S2 ="2008-01-29 09:12:11";
  3. Java. Text. dateformat df =NewJava. Text. simpledateformat ("Yyyy-mm-dd hh: mm: SS");
  4. Java. util. Calendar C1 = java. util. Calendar. getinstance ();
  5. Java. util. Calendar C2 = java. util. Calendar. getinstance ();
  6. Try
  7. {
  8. C1.settime (DF. parse (S1 ));
  9. C2.settime (DF. parse (S2 ));
  10. }Catch(Java. Text. parseexception e ){
  11. System. Err. println ("Incorrect format");
  12. }
  13. IntResult = c1.compareto (C2 );
  14. If(Result =0)
  15. System. Out. println ("C1 equal to C2");
  16. Else If(Result <0)
  17. System. Out. println ("C1 less than C2");
  18. Else
  19. System. Out. println ("C1 is greater than C2");
String S1 = "09:12:09"; string S2 = "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 C2"); else if (result <0) system. out. println ("C1 less than C2"); elsesystem. out. println ("C1 greater than C2 ");

// Time format conversion

Java code
    1. Fastdateformat df = fastdateformat. getinstance ("Yyyy-mm-dd hh: mm: SS");
    2. String date = DF. Format (Java. util. Date ());
Fastdateformat df = fastdateformat. getinstance ("yyyy-mm-dd hh: mm: SS"); string date = DF. Format (Java. util. Date ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.