The operation of time in Java (hope to criticize ...)

Source: Internet
Author: User

Code Demo:

Date to go in the database

Date s = list.get (0). Getsdate ();

System.out.println (s);//Tue APR 00:00:00 CST 2015

Manual New

Date d = new Date ();

System.out.println (d);//Tue APR 20:53:15 CST 2015

System.out.println ("+ +" + s.after (d));

Use the Calendar

Calendar calendar = Calendar.getinstance ();

Date time = Calendar.gettime ();

System.out.println (time);//Tue APR 20:55:16 CST 2015

Use date formatting

SimpleDateFormat format = new SimpleDateFormat ("Yyyy/mm/dd");

String FORMAT2 = Format.format (s);

System.out.println ("Format2" + format2);//2015/04/28

Re-parse

Date parse = Format.parse (FORMAT2);

System.out.println ("parse" + parse);//Tue APR 00:00:00 CST 2015

System.out.println ("???" + (S.gettime () >= parse.gettime ()));//True
Using Jodatime

DateTime dt = new DateTime ();

SYSTEM.OUT.PRINTLN (DT);//2015-04-28t21:00:28.780+08:00

System.out.println (Dt.todate ());//Tue APR 21:00:28 CST 2015

2 methods were written to handle:

1. Compare the 2 date sizes (only compared to the day of the year)/**  *  compares 2 dates, whether taken from a database or a new   *  @param  d1   *  @param  d2  *  @return   */ private String  Comparedate (DATE&NBSP;D1,DATE&NBSP;D2) {  if (d1==null | |  d2==null) {   return  "error";   }  long time1 = new  localdate (D1). ToDate (). GetTime ();   long time2 = new localdate (D2). ToDate () . GetTime ();   if (time1>time2) {   return  ">"   }else if (time1 ==time2) {   return  "= =";  }else{   return  "<";   } }2. Compare 2 Date sizes (compare only the Month day part)//  only compare the size  private string comparedate (Date s, &NBSP;DATE&NBSP;D)  throws ParseException {  SimpleDateFormat format =  New simpledateformat ("Yyyy/mm/dd");  if  (FORMAT.PArse (Format.format (s)). GetTime ()  > format.parse (    format.format (d)). GetTime ())  {   return  ">";  } else if  (Format.parse ( Format.format (s)). GetTime ()  == format.parse (    format.format (d)). GetTime ())  {   return  "= =";  } else {   return  "<";   } }

Have found the problem can leave a message, beginners forgive

The operation of time in Java (hope to criticize ...)

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.