Overview of the Date class
The class date represents a specific instantaneous, accurate to milliseconds. 1000 milliseconds = 1 seconds
The origin of Time: A.D. 1970 January 1, Midnight 0:xx: The corresponding millisecond value is 0
Note: The time and date calculations must depend on the millisecond value
1 long d =system.currenttimemillis (); System.currenttimemillis () Gets the current millisecond value of 2 System.out.println (d);
constructor function
1 Date D1 =new date (d); 2
Common methods for date classes
1 long d =system.currenttimemillis (); Gets the current number of milliseconds to D, using the GetTime method to turn the number of milliseconds to 2 Date D1 =new date (); 3 System.out.println (D1.gettime ());
DateFormat class
DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner. Date / time formatting subclasses (such as the SimpleDateFormat class ) allow formatting (that is, date- and text), Parsing (text- and-date) and normalization.
Date format
Construction method
DateFormat class method
1 SimpleDateFormat sim =new simpledateformat ("yyyy-mm-d HH:mm:ss"); Create date format type 2date d =new date (); Gets the current date 3 String str =Sim.format (d); Pass the current date in, call the Format method, and assign the value to str4 System.out.println (str);
L DATEFORMAT the function of a class: You can convert a Date object to a string that matches the specified format, or you can turn a string that conforms to the specified format into a Date object.
Specify the format of the specific rules we can refer to the description of the SimpleDateFormat class, here to do a brief introduction, the rule is in a string, the following letters will be replaced with the corresponding time components, the remaining content is output as:
When y is present, y is replaced by the adult
L Replace M with a month when M is present
L will replace D when D is present Rewardcash octopus
L when H is present, the H is replaced with
L when M is present, the M is replaced by the component
L replaces s with seconds when S is present
Common methods of DateFormat class
1 SimpleDateFormat sim =new simpledateformat ("Yyyy-mm-dd"); 2 String d = "2018-05-25"; Create date format for string type 3day dd =Sim.parse (d); Call the parse method to assign a value string to DD4 System.out.println (DD);
The format method, which is used to convert a Date object to a string
The parse method, which is used to convert a string to date (the string must conform to the specified format if it is converted, otherwise it cannot be converted).
Calendar class
The Calendar class, which appears after date, replaces many of the date's methods. This class encapsulates all possible time information into static member variables for easy access.
Calendar is an abstract class, because of language sensitivity,thecalendar class is not created directly when the object is created, but is created by a static method, processing the language-sensitive content, and then returning the child class object .
Calendar class static method
1 Calendar c =calendar.getinstance (); 2 System.out.println (c); Return to current calendar
Calenar Common methods
year
month, starting from 0, Max 11, 0 for January, 11 for December.
DATE Day
When HOUR
Minute min
Second sec
1 Calendar c =calendar.getinstance (); Gets the calendar, gets the month by the Get method 2 System.out.println (C.get ( Calendar.year));
1 Calendar c =calendar.getinstance (); 2 C.add (calendar.year, 3); Add offset, 3 years backwards. Print output 2021 3 System.out.println (C.get (calendar.year));
1 Calendar c =calendar.getinstance (); 2 c.set (calendar.year, 2022); Can be a property of the change, you can also change a number of properties 3 c.set (2020, 7, 7, 7, 7, 7); 4
1 Calendar c =calendar.getinstance (); Turn Calendar objects into date objects 2 System.out.println (C.gettime ());
1.1
Precautions
The beginning of the Western week is Sunday, China is Monday.
in the In the Calendar class, the month representation is represented by 0-11 for 1-12 months.
The date is the size of the relationship, the time depends on, the greater the time.
Little Practice
find out how many days you've been born
1 long a=system.currenttimemillis (); Gets the current number of milliseconds 2 Calendar CA =calendar.getinstance (); Get the current calendar and change it to the date of birth 3 ca.set (1996, 8,20); 4 Date b =ca.gettime (); Turn this calendar into a date format and turn it into milliseconds 5long c =6long d = (a-c)/24/60/60/ ; The number of milliseconds to subtract, the current number of milliseconds before the birth of 7 System.out.println (d);
Java Dated DateFormat Calendar