Java Learning note 07--Date operation class
First, Date class
The date class is defined in the Java.util package, and the date class itself is very simple to output its instantiated object directly.
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- Date date = new Date ();
- System.out.println ("Current date:" +date); //Current date: Thu 23:00:57 CST
- }
- }
Second, Calendar class
Use this class to make the date accurate to milliseconds
Public abstract class Calendar
extends Object The Calendar class is an abstract class, and since it is an abstract class, it must not be used directly, it is necessary to use the concept of object polymorphism to instantiate this type of object through an upward transformation relationship. get a full date through the Calendar class, using its subclasses. [Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- Calendar calendar = new GregorianCalendar ();
- //calendar C = calendar.getinstance ();
- SYSTEM.OUT.PRINTLN ("Year:" +calendar.get (Calendar.year));
- System.out.println ("Month:" + (Calendar.get (calendar.month) +1));
- System.out.println ("Day:" +calendar.get (Calendar.day_of_month));
- System.out.println ("Time:" +calendar.get (Calendar.hour_of_day));
- System.out.println ("points:" +calendar.get (Calendar.minute));
- System.out.println ("Seconds:" +calendar.get (Calendar.second));
- System.out.println ("MS:" +calendar.get (Calendar.millisecond));
- }
- }
Third, DateFormat class
This class is a date formatted class, specially formatted for the operation of the date, because the Java.util.Date class itself already contains the full date, so just need to install some good format for this date to format the display.
Public abstract classDateFormat
This class is also an abstract class, and the DateFormat class itself provides an operation that can be instantiated directly for it.
Get date DateFormat object public static final DateFormat getdateinstance ()
Gets the date and time of the DateFormat object public static final DateFormat getdatetimeinstance ()
Use the DateFormat class directly to complete the conversion of date.
Public final String format (date date)
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- DateFormat df1 = Dateformat.getdateinstance (); //Get date of DateFormat object
- DateFormat DF2 = Dateformat.getdatetimeinstance (); //Get the date and time of the DateFormat object
- System.out.println (Df1.format (new Date ()));
- System.out.println (Df2.format (new Date ()));
- }
- }
Four, SimpleDateFormat class
[Java]View Plaincopy
- Public class T2 {
- public static void Main (string[] args) throws parseexception {
- String str = "1990-04-13";
- String Parten = "Yyyy-mm-dd";
- SimpleDateFormat sdf1 = new SimpleDateFormat (Parten);
- SimpleDateFormat SDF2 = new SimpleDateFormat ("yyyy year mm DD day");
- Date date = Sdf1.parse (str); //Parse string, convert string to date type
- System.out.println (Sdf2.format (date));
- System.out.println ("cascading form:" +new SimpleDateFormat ("yyyy mm month DD Day"). Format (new SimpleDateFormat (" Yyyy-mm-dd "). Parse (str));
- System.out.println ("----sad dividing line------");
- String s = "2008-08-08";
- Date d = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (s);
- System.out.println (new SimpleDateFormat ("yyyy mm month DD Day"). Format (d));
- }
- }
Java Learning note 07--Date operation class