1 Packagedate Calendar class;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 Public classTestDate {8 Public Static voidMain (string[] args) {9Date date=Newjava.util.Date ();TenSYSTEM.OUT.PRINTLN (date);//Fri Apr 22:20:12 CST OneSimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); AString d=Sdf.format (date); -System.out.println (d);//2017-04-07 22:20:12 -String d2= "2017-04-06 12:30:30"; theDate date2=NewDate (); - Try { -Date2 =Sdf.parse (D2); -}Catch(ParseException e) { + e.printstacktrace (); - } +System.out.println (DATE2);//Thu Apr 12:30:30 CST A } at}
View Code
Java.lang.Object
Java.text.Format
Java.text.DateFormat->public Date Parse (String source) throws ParseException and public final String format (date date)
Java.text.SimpleDateFormat
1 Packagedate Calendar class;2 3 ImportJava.util.Calendar;4 Importjava.util.Date;5 6 Public classTestDate {7 Public Static voidMain (string[] args) {8Date date=Newjava.util.Date ();9SYSTEM.OUT.PRINTLN (date);//Fri Apr 22:53:21 CSTTenCalendar cal=calendar.getinstance (); OneSystem.out.println (CAL);//java.util.gregoriancalendar[time=1491576801409,arefieldsset=true,areallfieldsset=true,lenient=true,zone= Sun.util.calendar.zoneinfo[id= "Asia/shanghai", offset=28800000,dstsavings=0,usedaylight=false,transitions=19, Lastrule=null],firstdayofweek=1,minimaldaysinfirstweek=1,era=1,year=2017,month=3,week_of_year=14,week_of_month =2,day_of_month=7,day_of_year=97,day_of_week=6,day_of_week_in_month=1,am_pm=1,hour=10,hour_of_day=22,minute=53 , Second=21,millisecond=409,zone_offset=28800000,dst_offset=0] ADate Date2=cal.gettime ();//Calendar to date -System.out.println (DATE2);//Fri Apr 22:53:21 CST -StringBuffer sb=NewStringBuffer (); the - //month starts from 0, so add 1 -Sb.append (Cal.get (Calendar.year)). Append ("Year"). Append (Cal.get (calendar.month) +1). Append ("month"). Append (Cal.get ( Calendar.day_of_month). Append ("Day"). Append (Cal.get (Calendar.hour_of_day)). Append (":"). Append (Cal.get ( Calendar.minute). Append (":").). Append (Cal.get (Calendar.second)); -System.out.println (SB);//April 7, 2017 22:53:21 + } -}
View Code
Conversion between "Java" Date and string: Java.text.SimpleDateFormat, Public date parse (String source) throws parseexception and public Final String format (date date)