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 ...)