A constructor
1, the Date ()
Construct a Date object and initialize it to reflect the current time
Date date = new Date();
Copy the code
Date(long date)
Construct a Date object and initialize it based on the number of milliseconds relative to 00:00:00, January 1, 1970 GMT
Date date = new Date(290192019);
Copy the code
Commonly used method
long getTime( )
Returns the number of milliseconds represented by this Date object since 00:00:00 GMT, January 1, 1970.
Date date = new Date();
System.out.println(date.getTime());
Copy the code
void setTime(long time)
Set the time and date in milliseconds since 00:00:00 GMT, January 1, 1970.
String toString( )
Convert the Date object to String of the following form: dow mon DD hh:mm: SS ZZZ YYYY Where: dow is a day in a week (Sun, MON, Tue, Wed, Thu, Fri, Sat).
int compareTo(Date date)
Compares the Date object when this method is called with the specified Date. Return 0 if they are equal. The calling object returns a negative number until the specified date. The calling object returns a positive number after the specified date.
Date is
1. Use the getTime() method to get two dates (milliseconds experienced since January 1, 1970) and compare the two values.
Use methods before(), after(), and equals().
For example, the 12th of a month is earlier than the 18th new Date(99, 2, 12). Before (new Date(99, 2, 18)); Returns true.Copy the code
Use the compareTo() method, which is defined by the Comparable interface that the Date class implements.
Formatting date
SimpleDateFormat Formats the date
Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); System.out.println(ft.format(dNow));Copy the code
Printf prints the formatted date
Date Date = new Date(); CST 2012 system.out. printf(" %tc",date); CST 2012 system.out. printf(" %tc",date); Printf (" 2012-09-10 system.out. printf(" 2012-09-10 system.out. printf ",date); Printf (" month/day/year: %tD",date); System.out.printf("HH:MM:SS PM format (12 hours) : %tr",date); //HH:MM:SS PM format (12 hours) : 10:43:36 am Printf ("HH:MM:SS format (24 hour) : %tT",date); Printf ("HH:MM (24 hour) : %tR",date);Copy the code
Parse the string for time
The SimpleDateFormat class has some additional methods, especially parse(), which attempts to parse a string in the format storage of a given SimpleDateFormat object
Example: SimpleDateFormat ft = new SimpleDateFormat (" YYYY-MM-DD "); String input = "1818-11-11"; System.out.print(input + " Parses as "); Date t; t = ft.parse(input); System.out.println(t); //1818-11-11 Parses as Wed Nov 11 00:00:00 GMT 1818Copy the code
Sleep sleep
Example: system.out.println (new Date()); Thread.sleep(1000*3); System.out.println(new Date()); system.out.println (new Date());Copy the code
Time difference
Example: long start = system.currentTimemillis (); Thread.sleep(10000); long end = System.currentTimeMillis( ); long diff = end - start; System.out.println(diff);Copy the code
Calendar
Create a Calendar object that represents the current date of the system
Calendar c = Calendar.getInstance();
Create a Calendar object for a specific time
Calendar c1 = Calendar.getInstance();
c1.set(2009, 6, 12);
The set set up
Calendar c1 = calendar.getInstance (); public final void set(int year,int month,int date){... } c1.set(2009, 6, 12); Public void set(int field,int value) c1.set(calendar.date,10); c1.set(Calendar.YEAR,2008);Copy the code
Add Settings
Calendar c1 = calendar.getInstance (); c1.add(Calendar.DATE, 10); C1. add(calendar. DATE, -10); // Date minus 10 daysCopy the code