Time comparison in Java

Source: Internet
Author: User
Tags date now diff

Package com.newtouch.test;

Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Timecompare {
public static void Main (string[] args) {
SimpleDateFormat 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);
System.out.println (days);

// //////////////////////////////////////////////////////////

Java.util.Date now = Df.parse ("2004-03-26 13:31: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 hour = (L/(* * +)-day * 24);
Long min = ((l/(+))-day * 60-hour * 60);
Long s = (l/1000-day * * * * * 60-hour * * * 60-min * 60);
SYSTEM.OUT.PRINTLN ("+ Day +" days "+ Hour +" hours "+ min +" min "+ S +" SEC ");

// //////////////////////////////////////////////////////////

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;//divided by 1000 for conversion into 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 + "days" + Hour1 + "hours" + minute1 + "min" + Second1 + "SEC");

////////////////////////////////////////////////////////////

String s1= "2008-01-25 09:12:09";
String s2= "2008-01-29 09:12:11";
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");
Else
SYSTEM.OUT.PRINTLN ("C1 greater than C2");

} catch (Exception e) {

}
}
}

Time comparison in Java

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.