The date class represents a specific instantaneous, accurate to milliseconds.
There are 2 ways to create a Date object (this is not considered obsolete constructors)
1, public date ()--allocates a Date object and initializes the object to indicate the time allotted it (accurate to milliseconds).
1 @Test 2 Public void test1 () {3 New Date (); 4 System.out.println (date); 5 }
Sun Oct 22:39:14 CST 2016
2, public date (long date)--Creates a Date object based on the given millisecond value.
1 @Test 2 Public void test2 () {3 long time = system.currenttimemillis (); 4 New Date (time); 5 System.out.println (date); 6 }
Sun Oct 22:41:42 CST 2016
After describing the constructor for date, let's look at the conversion between the date and millisecond values.
1, public long getTime ()--date to millisecond value
The GetTime method allows you to convert a date type to a long-type millisecond value
1 @Test 2 Public void test3 () {3 New Date (); 4 System.out.println (Date.gettime ()); 5 }
1477234414353
2, public void settime (long time)--millisecond value to date
1 @Test 2 Public void test4 () {3 long time = system.currenttimemillis (); 4 New Date (); 5 Date.settime (time); 6 System.out.println (date); 7 }
Sun Oct 22:53:05 CST 2016
Of course, you can also convert a millisecond value to a date type by using the constructor public date (long date).
Usually we compare the size of 2 dates, and the date class provides the following methods for comparing 2 date related operations
1, public boolean before (date time)--tests whether this date is before the specified date, returns True if and only if the moment represented by this date object is earlier than the instant represented by when, otherwise returns false.
1 @Test 2 Public void Test5 () {3 New Date (+); 4 New Date (+); 5 System.out.println (Date1.before (Date2)); 6 }
True
2, public boolean after (date time)--tests whether this date is after the specified date, returns True if and only if the moment represented by this date object is later than the instant represented by when, otherwise false is returned.
1 @Test 2 Public void Test6 () {3 New Date (+); 4 New Date (+); 5 System.out.println (Date1.after (Date2)); 6 }
False
3, public int compareTo (date anotherdate)--compares the order of two dates.
A value of 0 is returned if the parameter date equals this date, or a value less than 0 if this date precedes the date parameter, or a value greater than 0 if this date is after the date parameter.
1 @Test 2 Public void test7 () {3 New Date (+); 4 New Date (+); 5 System.out.println (Date1.compareto (Date2)); 6 }
-1
The date class for Java basics