Java learning notes (2)
2003/05/02 xuyunsheng
I. Get, setup and formatted 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;
The Calendar () method is mainly used to obtain and set the date;
The dateformat () method is primarily used to create a date formatting, and then convert the date to a variety of date format string outputs by the formatter.
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 Displays the DATE Class Related Use");
Today = new date ();
System.out.println ("New Date () = / t" Today);
System.out.println ("/ n Displays various date formats with DateFormat");
// Display a variety of date formats
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.getdateInstance (DateFormat.long, Locale.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.GetdateInstance (DateFormat.Short, Locale.china) = / T" S1);
System.out.println ("/ N Shows Various Time Formats" in DateFormat
// Display a variety of time formats
F1 = dateformat.gettimeInstance ();
S1 = f1.Format (Today);
System.out.println ("DateFormat.getTimeInstance () = / t" s1);
F1 = DateFormat.gettimeInstance (DateFormat.long, Locale.China);
S1 = f1.Format (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 Displays the related time usage of Calendar);
Now = Calendar.getInstance ();
Today = now.getime ();
System.out.println ("Calendar.GetInstance (). Gettime () = / t" Today.toString ());
}
}
The program operation results are shown below:
Display the related usage of the DATE class
New date () = fri may 02 13:29:32 CST 2003
Display a variety of date formats with DateFormat classes
DateFormat.getdateInstance () = 2003-5-2
DateFormat.getdateInstance (DateFormat.long, Locale.china) = May 2, 2003
Dateformat.getdateInstance (DateFormat.Medium, locale.china) = 2003-5-2
DateFormat.GetdateInstance (DateFormat.short, locale.china) = 03-5-2
Use the DateFormat class to display a variety of time formats
DateFormat.gettimeInstance () = 13:29:32
DateFormat.gettimeInstance (DateFormat.long, Locale.china) = 01:29 pm 32 seconds
DateFormat.gettimeInstance (DateFormat.Medium, locale.china) = 13:29:32
DateFormat.gettimeInstance (DateFormat.short, locale.china) = 1:29 pm
Display Calendar related time usage
Calendar.getInstance (). Gettime () = fri may 02 13:29:33 CST 2003