1 ImportJava.text.DateFormat;2 Importjava.text.ParseException;3 ImportJava.text.SimpleDateFormat;4 ImportJava.util.Calendar;5 Importjava.util.Date;6 7 Importorg.apache.commons.lang.time.DateUtils;8 9 Public classTest {Ten One Public Static voidMain (string[] args)throwsParseException { A - //Set Format -SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); the - //String-Date -String txttodate = "2014-10-01 22:31:11"; -Date now =Sdf.parse (txttodate); +System.out.println (now);//Wed Oct 22:31:11 gmt+08:00 - + //Date-String AString Datetotxt =Sdf.format (now); atSystem.out.println (Datetotxt);//2014-10-01 22:31:11 - - //Date, Calendar -Calendar Cnow =calendar.getinstance (); - Cnow.settime (now); -System.out.println (Cnow);//java.util.gregoriancalendar[time=1412173871000,arefieldsset=true,areallfieldsset=true,lenient=true,zone= Sun.util.calendar.zoneinfo[id= "gmt+08:00", Offset=28800000,dstsavings=0,usedaylight=false,transitions=0, Lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=2014,month=9,week_of_year=40,week_of_month =1,day_of_month=1,day_of_year=274,day_of_week=4,day_of_week_in_month=1,am_pm=1,hour=10,hour_of_day=22,minute= 31,second=11,millisecond=0,zone_offset=28800000,dst_offset=0] in - //Calendar, Date tonow =cnow.gettime (); +System.out.println (now);//Wed Oct 22:31:11 gmt+08:00 - the //dateutils.truncate *System.out.println (Sdf.format (Dateutils.truncate (now, Calendar.second));//2014-10-01 22:31:11 $System.out.println (Sdf.format (Dateutils.truncate (now, Calendar.minute));//2014-10-01 22:31:00Panax NotoginsengSystem.out.println (Sdf.format (Dateutils.truncate (now, calendar.hour_of_day));//2014-10-01 22:00:00 -System.out.println (Sdf.format (Dateutils.truncate (now, calendar.day_of_month));//2014-10-01 00:00:00 theSystem.out.println (Sdf.format (Dateutils.truncate (now, calendar.month));//2014-10-01 00:00:00 +System.out.println (Sdf.format (Dateutils.truncate (now, calendar.year));//2014-10-01 00:00:00 A the //Calendar Add +Calendar temp =(Calendar) Cnow.clone (); -Temp.add (Calendar.day_of_year, 1); $System.out.println (Sdf.format (Temp.gettime ()));//2014-10-02 22:31:11 $Temp.add (Calendar.day_of_year, 2); -System.out.println (Sdf.format (Temp.gettime ()));//2014-10-02 22:31:11 - the //Calendar Set -Temp.set (2010, 8, 4, 22, 15, 20);//month minus 1WuyiSystem.out.println (Sdf.format (Temp.gettime ()));//2010-09-04 22:15:20 the -Calendar Temp2 =(Calendar) Temp.clone (); WuTemp2.add (Calendar.month, 1); - About //Calendar diff $ LongTimemillis = (Temp2.gettimeinmillis ()-Temp.gettimeinmillis ()); - LongTimedays = timemillis/1000/60/60/24; - System.out.println (timedays); - A + } the -}
Java-calendar Date dateutils SimpleDateFormat