Java computing time difference and comparison time

Source: Internet
Author: User
Tags date now dateformat

Javaz processes a lot of date and time, and the code lists the method for calculating the difference between 3 and date and time.

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

Java computing time difference and comparison time
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:
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: simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Java. util. Date Now = DF. parse ("13:31:40 ");
Java. util. Date date = df. parse ("11:30:24 ");
Long l = now. getTime ()-date. getTime ();
Long day = l/(24x60*60*1000 );
Long hour = (l/(60x60*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:
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 ");

========================================================== ================

Java comparison time size

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 to c2 ");
Else if (result <0)
System. out. println ("c1 less than c2 ");
Else
System. out. println ("c1 greater than c2 ");

 

From: http://jackmlw.blog.sohu.com/140277739.html

Related Article

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.