<> concept
What kind of data is the nature of storage time in a computer ??? Time is in the form of integer in the computer , It's stored when = How many milliseconds has the current time passed far away
Although time is essentially an integer , However, as an integer, it is not convenient to operate and read the time and date information
<> usage
Date data = new Date() ; Get current time date object directly ----- This is the most used Date past = new
Date(123456789L); Constructed according to the specified number of milliseconds Date object ------ Low frequency of use If the specified time is set , Please use Calendar This class /* *
Although time is essentially an integer , However, it is not easy to operate and read the time and date information as an integer , * So in Java Class is provided to encapsulate it . */ Date date =
new Date();// Get the current time and Date object directly --- This is the most used Date past = new
Date(12346579846L);// Constructed according to the specified number of milliseconds Date object ---- Very low frequency of use
// Now for Date Maximum use , It's cooperation SimpleDateFormat Class of , Custom formatting output for date objects SimpleDateFormat sdf =
new SimpleDateFormat("yy year MM month dd day HH:mm:ss E D"); String msg = sdf.format(date);
// If you want to set the specified time and date , Please use Calendar This class Calendar cal =
Calendar.getInstance();// We don't need to generate subclass objects ourselves , Superclass Calendar Provide a getInstance Method directly returns a calendar object to us
// cal.set(year, month, date); past = cal.getTime(); msg = sdf.format(past);
System.out.println(msg);
1,public Date()—— distribution Date Object and initialize this object , To indicate when it was allocated ( Accurate to milliseconds ).
1 @Test 2 public void test1() { 3 Date date = new Date(); 4
System.out.println(date); 5 } Sun Oct 23 22:39:14 CST 2016
2,public Date(long date)—— Create a date object based on the given MS value .
1 @Test 2 public void test2() { 3 long time = System.currentTimeMillis(); 4
Date date = new Date(time); 5 System.out.println(date); 6 } Sun Oct 23 22:41:42
CST 2016
End of introduction Date After the constructor of , Let's look at the conversion between date and millisecond values

1,public long getTime()—— Date to MS value

adopt getTime Method to convert a date type to long MS value of type
1 @Test 2 public void test3() { 3 Date date = 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
Date date = new Date(); 5 date.setTime(time); 6 System.out.println(date); 7 }
Sun Oct 23 22:53:05 CST 2016
Of course, you can also use the constructor public Date(long date) Convert milliseconds to date type .

We usually compare 2 Size of days ,Date Class provides the following methods for comparison 2 Date related operations

1,public boolean before(Date
when)—— Test whether this date is before the specified date , If and only if Date Instantaneous ratio of object representation when Express the moment early , To return true; Otherwise return false.
1 @Test 2 public void test5() { 3 Date date1 = new Date(1000); 4 Date date2 =
new Date(2000); 5 System.out.println(date1.before(date2)); 6 } true
2,public boolean after(Date
when)—— Test whether this date is after the specified date , If and only if Date Instantaneous ratio of object representation when Show the moment later , To return true; Otherwise return false.
1 @Test 2 public void test6() { 3 Date date1 = new Date(1000); 4 Date date2 =
new Date(2000); 5 System.out.println(date1.after(date2)); 6 } false
3,public int compareTo(Date anotherDate)—— Compare the order of two dates .

If parameter Date Equal to this Date, Return value 0; If this Date stay Date Before parameter , Return less than 0 Value of ; If this Date stay Date After parameter , Return greater than 0 Value of .
1 @Test 2 public void test7() { 3 Date date1 = new Date(1000); 4 Date date2 =
new Date(2000); 5 System.out.println(date1.compareTo(date2)); 6 }

Technology
©2019-2020 Toolsou All rights reserved,
Bitcoin in ten years ,VDS Opportunity or fraud Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood sort ( one ) bubble sort Random forest R Language implementation java Realize the function of grabbing red packets Python Basic knowledge and notes python To solve the problem of dictionary writing list in CSS architecture design Vue Common features ( one )2021 year 2 Monthly programmer salary statistics , average 15144 element