There are 3 main classes in Java for date format conversions DateFormat, SimpleDateFormat, Calendar
1.SimpleDateFormat
The class is a subclass of DateFormat, and the formatting of the general date is to instantiate the class implementation
The specific application is as follows
PackageCom.gree.java;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.logging.SimpleFormatter; Public classSimpledate { Public Static voidmain (String [] args) {SimpleDateFormat a=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Full date format, 24-hour systemSimpleDateFormat b=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss a");//Full date format, 12-hour systemSimpleDateFormat c=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//get the date after 5 daysSimpleDateFormat d=NewSimpleDateFormat ("Yyyy-mm-dd");//get month and day formatSimpleDateFormat e=NewSimpleDateFormat ("yyyy");//Get yearSimpleDateFormat f=NewSimpleDateFormat ("MM");//Get MonthSimpleDateFormat g=NewSimpleDateFormat ("DD");//Get DayCalendar Calendar=calendar.getinstance (); Calendar.add (Calendar.date,5); Date Date1=NewDate (); Date Date=Calendar.gettime (); System.out.println (A.format (date)); System.out.println (B.format (date1)); System.out.println (C.format (date1)); System.out.println (D.format (date1)); System.out.println (E.format (date1)); System.out.println (F.format (date1)); System.out.println (G.format (date1));//System.out.println (G.format (date1)); }}
The results are as follows:
2014-11-18 16:36:302014-11-13 04:36:30 pm 2014-11-13 16:36:302014-11-1320141113
Conversion and application of date formats in Java