JAVA Compute Time Difference

Source: Internet
Author: User
Tags date now dateformat diff

For example: it's 2004-03-26 13:31:40
Last was: 2004-01-02 11:30:24
I now want to get two date difference, the form of difference is: xx days xx hours xx minutes xx seconds
Method One:

1DateFormat DF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 2 Try  3 {   4Date D1 = Df.parse ("2004-03-26 13:31:40"); 5Date D2 = Df.parse ("2004-01-02 11:30:24"); 6     Longdiff = d1.gettime ()-D2.gettime (); 7     LongDays = diff/(1000 * 60 * 60 * 24); 8 }   9 Catch(Exception e)Ten {    One}

Method Two:

1SimpleDateFormat DF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 2Java.util.Date now = Df.parse ("2004-03-26 13:31:40"); 3Java.util.Date date=df.parse ("2004-01-02 11:30:24"); 4    LongL=now.gettime ()-Date.gettime (); 5    Longday=l/(24*60*60*1000); 6    LongHour= (l/(60*60*1000)-day*24); 7    LongMin= ((l/(60*1000))-day*24*60-hour*60); 8    LongS= (l/1000-day*24*60*60-hour*60*60-min*60); 9System.out.println ("+day+" Days "+hour+" hours "+min+" minutes "+s+" seconds ");

Method Three:

1SimpleDateFormat DFS =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 2Java.util.Date begin=dfs.parse ("2004-01-02 11:30:24"); 3Java.util.Date end = Dfs.parse ("2004-03-26 13:31:40"); 4    LongBetween= (End.gettime ()-begin.gettime ())/1000;//divided by 1000 to convert into seconds .5    Longday1=between/(24*3600); 6    Longhour1=between% (24*3600)/3600; 7    LongMinute1=between%3600/60; 8    LongSecond1=between%60/60; 9System.out.println ("+day1+" Days "+hour1+" hours "+minute1+" minutes "+second1+" seconds ");

Compare time:

1String s1= "2008-01-25 09:12:09"; 2String s2= "2008-01-29 09:12:11"; 3Java.text.DateFormat df=NewJava.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 4Java.util.Calendar c1=java.util.Calendar.getInstance (); 5Java.util.Calendar c2=java.util.Calendar.getInstance (); 6 Try  7 {   8 C1.settime (Df.parse (S1)); 9 c2.settime (df.parse (S2)); Ten}Catch(java.text.ParseException e) { OneSYSTEM.ERR.PRINTLN ("Not formatted correctly");  A }    - intresult=C1.compareto (C2);  - if(result==0)    theSYSTEM.OUT.PRINTLN ("C1 equal C2");  - Else if(result<0)    -SYSTEM.OUT.PRINTLN ("C1 Less than C2");  - Else   +SYSTEM.OUT.PRINTLN ("C1 greater than C2");

JAVA Compute Time Difference

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.