I. Get acquisition, setting and formatting in Java 1) Java provides 3 date classes: Date, Calendar and DateFormat. The Date () method is mainly used to create a date object and obtain the date; Calendar () method is primarily used to obtain and set the date; the dateformat () method is mainly used to create a date formatting, and then convert the date to each by the formatter Date format string output. 2) The benchmark date specified in Java language is 1970.1.1.00:00:00, and the current date is converted from the number of milliseconds experienced by the benchmark date. 3) The DateFomat class is in the Java.Text package, the Date and Calendar classes are in the java.util package. 4) Examples are as follows: import java.util. *; Import java.text. *;
Public class displaydate {public static void main (string [] args) {date today; calendar now; DateFormat F1, F2; String S1, S2; System.out.Println ("/ n Display Date Class Related Usage); Today = new date (); system.out.println ("new date () = / t" Today); system.out.println ("/ N Displays various date formats with DateFormat"); // Show various Date format f1 = DateFormat.getdateInstance (); S1 = f1.format (Today); system.out.println ("DateFormat.getdateInstance () = / t" s1); f1 = DateFormat.getdateInstance (DateFormat.long, LOCALE .China); S1 = f1.format (Today); System.out.Println ("DateFormat.long, local.china = / t" s1); f1 = DateFormat.getdateInstance (DateFormat.Medium, Locale.china); S1 = f1.format (Today); System.out.Println ("DateFormat.Medium, locale.china) = / t" S1); f1 = DateFormat.getdateInstance (DateFormat.short , Locale.china); S1 = f1.format (Today); System.out.Println ("DateFormat.Short, locale.china) = / t" S1); System.out.Println ("/ n with DATE Format class display various time format "); // Displays various time format f1 = DateFormat.getTimeInstance (); s1 = f1.format (Today); system.out.println (" DateFormat.gettimeInstance () = / t " S1); F1 = DateFormat.gettimeInstance (DateFormat.long, local.china); S1 = f1.mmat (today); system.out.println ("DateFormat.gettimeInstance (DateFormat.long, locale.china) = / t " S1); F1 = DateFormat.gettimeInstance (DateFormat.medium, locale.china); s1 = f1.format (Today); System.Out.Println (" DateFormat.GettimeInstance (DateFormat.Medium, locale.china) = / T " S1); F1 =
Dateformat.gettimeInstance (DateFormat.Short, locale.china); S1 = f1.format (Today); System.out.Println ("DateFormat.GettimeInstance (DateFormat.Short, locale.china) = / t" S1); system .out.println ("/ n Display Calendar related time usage"); now = calendar.getInstance (); today = now.getime (); system.out.println ("Calendar.getInstance (). gettime () = / t " Today.toString ());}} The program running results are shown below:
Displaying the related usage of the DATE class new date () = fri may 02 13:29:32 CST 2003
Use the DateFormat class to display a variety of date format DateFormat.getdateInstance () = 2003-5-2dateFormat.getdateInstance (DateFormat.long, locale.china) = May 2, 2003 DateFormat.getdateInstance (DateFormat.Medium, locale.china) = 2003-5-2dateformat.getdateInstance (DateFormat.short, Locale.china) = 03-5-2
DateFormat class various display time format DateFormat.getTimeInstance () = 13: 29: 32DateFormat.getTimeInstance (DateFormat.LONG, Locale.CHINA) = 13:29:32 DateFormat.getTimeInstance (DateFormat.MEDIUM, Locale.CHINA) = 13: 29: 32dateFormat.gettimeInstance (DateFormat.short, locale.china) = 1:29 pm
Show Calendar's related time usage Calendar.GetInstance (). Gettime () = fri may 02 13:29:33 CST 2003