The date class and the Calendar class can be used in Java to process dates
However, many methods of the date class are obsolete, and it is recommended to use the Canlendar class to process dates and to describe the formatting of dates. The following sections will describe
Date Class
Java provides a date class to handle dates and times, and the date class contains both dates and times. The date class began to exist from the JDK1.0 version, and existed for a long time, with 4 of the 6 constructors provided, which are currently being used:
- Date (): Generates a Date object that is subject to the system's current time date.
- Date (long date): A Date object is generated based on the specified number of long integers.
- Boolean after (date): Determines whether the date is after the specified date when
- Boolean before (date when): Determines whether the date is before the specified date.
- int CompareTo (date antherdate): Compares two date sizes, followed by a time greater than the previous time return-1, otherwise returns 1.
- Boolean equals (Object obj): Two time means that the same time is returned true
- Long GetTime (): Returns the object that corresponds to a long integer
- void SetTime (long time): When the object is set.
Public classDatetest { Public Static voidMain (string[] args)throwsinterruptedexception {LongStart =System.currenttimemillis (); Date D1=NewDate (start); Thread.Sleep (3000); LongEnd =System.currenttimemillis (); Date D2=NewDate (end); System.out.println (D1.before (D2)); System.out.println (D1.after (D2)); System.out.println (D1.gettime ()); System.out.println (D1.equals (D2)); }}View Code
Calendar class
The calendar is an abstract class that is used to represent a calendar. Because the date class has some flaws in the design, Java provides the calendar class to better handle the date and time.
The Calendar class cannot be created directly, you can create a child class of the calendar through a class method, Java itself provides a GregorianCalendar subclass, and the getinstance () method returns a calendar for the default time zone and locale. The returned instance is the default subclass provided by Java.
Common methods provided by the Calendar class:
- void Add (int field,int amount): Adds or subtracts the specified amount of time for a given calendar field, based on the rules of the calendar.
- int get (int field): Returns the value of the specified calendar field.
- int getactualmaximum (int field): Returns the maximum possible value of the specified calendar field.
- int getactualminimum (int field): Returns the lowest possible value of the specified calendar field.
- void Roll (int field,int amout): Similar to the Add method, the difference is that it does not carry up a field when it exceeds the maximum range of the field.
- void set (int field,int value): Sets the given Calendar field to the given value.
- void set (int year,int month,int date): Sets the year, month, and Day 3 field values for the Calendar object.
- void set (int year,int month,int date,int hourofday,int minute,int Second): Sets the value of the calendar object's year, month, day, time, minute, and second 6 fields.
Public classCalendartest { Public Static voidMain (string[] args) {//obtaining an instance from a class methodCalendar Calendar =calendar.getinstance (); System.out.println (Calendar.get (calendar.year));//Calendar.add (calendar.date, 1);//System.out.println (Calendar.get (Calendar. DATE)); //Roll Usage (when the range does not go upward)Calendar.roll (calendar.month,10 ); System.out.println (Calendar.get (Calendar. year)); System.out.println (Calendar.get (Calendar. MONTH)); //gets the maximum value that the field may haveSystem.out.println (calendar.getmaximum (Calendar. MONTH)); //Set your own timeCalendar.set (2016, 06, 8); System.out.println (Calendar.get (Calendar. year)); //returns the time after setting//conversions between the date and calendar classesDate Date =Calendar.gettime (); //format output time with SimpleDateFormatSimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day hh point mm min ss sec"); System.out.println (Sdf.format (date)); }}View Code
format dates with DateFormat
DateFormat is an abstract class that also provides several factory methods for getting DateFormat objects. All returned are subclass instances of the DateFormat object and are instances of the same type.
Although all returned are instances of the same object type, depending on the factory method, the returned object handles different pieces of information when the time object is formatted.
- Getdateinstance (): Returns a date formatter that formats only the date.
- Gettimeinstance (): Returns a time formatter that formats only the time.
- Getdatetimeinstance (): Returns a date, time formatter that formats both the time and the date.
- getinstance (): Returns a default system-related date, time formatter.
Public Static voidMain (string[] args)throwsexception{Date Date=NewDate (); //default to Chinese common formatDateFormat format =dateformat.getinstance (); System.out.println (Format.format (date)); //returns the date, time formatter international uniform standardFormat =dateformat.getdatetimeinstance (); System.out.println (Format.format (date)); //returns the date formatter, specifying the type of format, as a compact type, specifying which country to displayFormat =dateformat.getdateinstance (Dateformat.short, Locale.china); System.out.println (Format.format (date)); //returns the date formatter, specifying the format type, medium type, specifying which country type to displayFormat =dateformat.getdateinstance (Dateformat.medium, Locale.china); System.out.println (Format.format (date)); //returns the date formatter, specifying the format type, for the full type, specifying which country type to displayFormat =dateformat.getdateinstance (Dateformat.long, Locale.china); System.out.println (Format.format (date)); //returns the date formatter, specifying the format type, for the complex type, specifying which country to displayFormat =dateformat.getdateinstance (Dateformat.full, Locale.china); System.out.println (Format.format (date)); }View Code
format dates with SimpleDateFormat
SimpleDateFormat is a subclass of DateFormat, a simpler date format that formats dates for the format we specify.
Note that when you use the new object, you specify the date format you want to see. (Specific code above example has)
Date-processing classes in Java