Spit Groove
Java 8 provides a new set of date-time APIs, why do you do so? Because in the old Java, the date time API has many problems, such as thread safety issues , java.util.Date is not thread-safe, all date classes are variable; and the design is messy , You go to see the Java.util.Date class will find that many of its methods are marked out of date, know that the sun company itself can not see the past, and the class used to format and parse the date class under the Java.text package, is not the lottery to be casually subcontract; inconvenient to use , Take Java.util.Calendar class, add a few days and subtract days are using Add method, meaning is very not obvious. When I first started to learn, I was a big face, all kinds of chaos in the wind, from now on I can completely forget them ... Because the Java 8来 saved me. new Date Time API
The date-time APIs provided by Java 8 are under the Java.time package, which covers all processing dates (date), Time (times), Date/Time (datetime), timezone (zone), time (instant), Interval (duration) with the clock (clock) operation, can say a pack in the hand, the world I have.
Of course, I will not introduce all of the classes and methods, just the approximate use, just remember a little, you want the function (not too biased door), others have helped you achieve, go to the API document date, time and date time The date (year/month) corresponds to the java.time.LocalDate time (time and minutes) corresponding to the Java.time.LocalTime date time (month and hour) corresponding to the Java.time.LocalDateTime
These three classes are used in the same way, and I'm going to use LocalDateTime to write an example.
@Test public
void Test () {
//Get the current date time
LocalDateTime now = Localdatetime.now ();
System.out.println (now);
Subtract the current date time by two days
LocalDateTime dateTime2 = now.minusdays (2);
System.out.println (dateTime2);
Add the current date time five days
LocalDateTime DateTime3 = now.plusdays (5);
System.out.println (DateTime3);
The year
System.out.println (Now.getyear ()) that outputs the current datetime;
Constructs an object of a specified datetime
LocalDateTime dateTime = Localdatetime.of (2016, 8,);
System.out.println (dateTime);
Output results
2017-05-07t13:39:32.220
2017-05-05t13:39:32.220
2017-05-12t13:39:32.220
2017
2016-10-23t08 : 20
time Stamp
The timestamp corresponds to Java.time.Instant, and the following is a timestamp example
@Test public
void Test1 () {
//Gets the timestamp of the current time
Instant Instant = Instant.now ();
Because China is in the East eight area, so this output time with my computer time is different
System.out.println (instant);
Since China is in the East eight, it will be offset 8 hours, this kind of acquisition time is the time of their own computer
offsetdatetime dateTime = Instant.atoffset (zoneoffset.ofhours (8));
System.out.println (dateTime);
Converted to milliseconds, if the timestamp of the current time, the result is the same as the System.currenttimemillis ()
Long milli = Instant.toepochmilli ();
System.out.println (milli);
}
Output results
2017-05-07t05:40:18.630z
2017-05-07t13:40:18.630+08:00
1494135618630
interval
There are two class java.time.Duration for the interval to compute two "time" intervals java.time.Period used to calculate two "date" intervals
Here is an example of a time interval
@Test public
void Test2 () {
localtime start = Localtime.now ();
try {
//Let thread sleep 3s
thread.sleep (3000);
} catch (Exception e) {
}
localtime end = Localtime.now ();
Gets end and start intervals
Duration Duration = Duration.between (start, end);
May output pt3s or output pt3.001s, as for the extra 0.001 seconds is actually to remove the thread sleep time to perform the calculation time interval that code consumption time
System.out.println (duration);
}
Output results
pt3.001s
The following is an example of a date interval
@Test public
void Test3 () {
//start time specified as March 4, 2015
localdate start = Localdate.of (2015, 3, 4);
The termination time is specified as August 23, 2017
localdate end = Localdate.of (2017, 8);
Period Period = Period.between (start, end);
The output p2y5m19d,y represents the year, M represents the month, and D represents the day, indicating that the date interval between start and end is 2 years 5 months 19 days
System.out.println (period);
}
Output results
p2y5m19d
Format Conversion
The date format class we used before was Java.text.simpledateformat,java 8, the date format class provided is Java.time.format.DateTimeFormatter, and here's an example
@Test public
void Test5 () {
//Get a predefined format, DateTimeFormatter class has a number of predefined formats
datetimeformatter DTF = Datetimeformatter.basic_iso_date;
Gets the current date time
localdate now = Localdate.now ();
Specify formatter format date time
String Strnow = Now.format (DTF);
System.out.println (Strnow);
Custom Format
DateTimeFormatter formatter = Datetimeformatter.ofpattern ("YYYY year mm month DD day");
String StrNow2 = Now.format (formatter);
System.out.println (STRNOW2);
Converts a string to a date
localdate date = Localdate.parse (StrNow2, formatter);
SYSTEM.OUT.PRINTLN (date);
Output results
20170507
May 07, 2017
2017-05-07
Time Zone
The date time with the time zone is java.time.ZonedDateTime, and we can go through java.time.ZoneId to see what time zone is supported, such as
@Test public
void Test7 () {
set<string> Set = Zoneid.getavailablezoneids ();
Set.foreach (System.out::p rintln);
}
Output results, there are a lot of later
Asia/aden
America/cuiaba
etc/gmt+9
etc/gmt+8
africa/nairobi
america/marigot
Pacific/kwajalein
America/el_salvador
Asia/pontianak
africa/cairo
pacific/pago_pago
africa/mbabane
Asia /kuching
pacific/honolulu
pacific/rarotonga
america/guatemala
australia/hobart
Europe /london
america/belize
america/panama
asia/chungking
america/managua ...
.
And then there's the zoneddatetime example.
@Test public
void Test8 () {
//Gets the date time of the current time zone
zoneddatetime now = Zoneddatetime.now ();
System.out.println (now);
Gets the date time of the Los Angeles time zone
zoneddatetime usanow = Zoneddatetime.now (Zoneid.of ("America/los_angeles"));
System.out.println (Usanow);
}
Output results
2017-05-07t14:06:32.132+08:00[asia/singapore]
2017-05-06t23:06:32.134-07:00[america/los_angeles]
I don't know why my time zone is Singapore.