[New features] java8 date and time

Java 8 (also known as jdk 1.8) is a major release of the Java language. Oracle Corporation on March 18, 2014 release Java 8, which supports functional programming, a new JavaScript engine, the new date API, the new Stream API and so on.

Java 8 to further enhance the processing of the date and time by issuing new Date-Time API (JSR 310).

In older versions of Java, we are using SimpleDateFormat for date format, there are many problems date and time API (Date, calendar), including:

  • Non-thread-safe  - java.util.Date non-thread-safe, all the dates are variable class, which is one of the biggest problems date Java classes.

  • Poor design  - defined Java date / time is not the same class, and in java.util package java.sql class includes the date, in addition to formatting and parsing the class definition java.text package. java.util.Date contains the date and time, and contains only java.sql.Date date will be incorporated into the package java.sql unreasonable. In addition these two classes have the same name, which itself is a very bad design.

  • Time zone handling trouble  - Date class does not offer international, no time zone support, so the introduction of Java and java.util.TimeZone java.util.Calendar class, but they are all above problems also exist.

Java 8 in  java.time  offers many new API under the package. The following are two of the more important API:

  • Local (Local)  - simplifies the handling of date and time, no problem time zone.

  • Zoned (time zone)  - the processing date and time established by time zone.

Date and time of Java class comprises 8 date (LocalDate), time (LocalTime), time (Instant), process (Duration) and a clock (clock), these classes are included in the package java.time, 8 new time Java API the use, including creating, formatting, parsing, computing, modify, let's look at how to use.

LocalDate will only get date

// create LocalDate
 // Get the current date 
LocalDate localDate = LocalDate.now ();
 // constructor specified date 
LocalDate localDate1 = LocalDate.of (2019, 9, 12 );
 // get year, month, day , day of the week 
int year = localDate.getYear ();
 int YEAR1 = localDate.get (ChronoField.YEAR); 
Month month The = localDate.getMonth ();
 int MONTH1 = localDate.get (ChronoField.MONTH_OF_YEAR);
 int Day = localDate. getDayOfMonth ();
 int DAY1 = localDate.get (ChronoField.DAY_OF_MONTH); 
dayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);

LocalTime will only get minutes and seconds

// 创建 LocalTime
LocalTime localTime = LocalTime.of(14, 14, 14);
LocalTime localTime1 = LocalTime.now();
// 获取小时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
// 获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
// 获取秒
int second = localTime.getMinute();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);

When LocalDateTime acquisition date, day, hour, equivalent to LocalDate + LocalTime

// 创建localdateti to 
localdateti to localdateti to = localdatetime.now (); 
Localdateti to localdatetime1 = localdatetime.of (2019 month.septemb is, 10, 14, 46, 56 ); 
Localdateti to localdatetime2 = localdatetime.of (LOCALDATE LOCALTIME); 
Localdateti to localdatetime3 = localdate.atti to (localtime); 
Localdateti to localdatetime4 = localtime.atdat A (LOCALDATE);
// 获取LOCALDATE 
LOCALDATE localdate2 = localdatetime.tolocaldat A ();
// 获取localtime 
localtime localdatetime.tolocalti to localtime2 = ();

Instant acquiring seconds, for indicating a time stamp (accurate to nanoseconds)

// create objects Instant 
Instant the Instant = Instant.now ();
 // get the number of seconds 
Long currentSecond = instant.getEpochSecond ();
 // get the number of milliseconds 
Long currentMilli = instant.toEpochMilli ();

If only to obtain the number of seconds or milliseconds, may be used

System.currentTimeMillis()

Duration represents a period of time

// Duration.between () method creates Duration object 
the LocalDateTime from = LocalDateTime.of (2017, Month.JANUARY, 1, 00, 0, 0);     // 2017-01-01 00:00:00 
the LocalDateTime to = the LocalDateTime. of (2019, Month.SEPTEMBER, 12 is, 14, 28, 0);      // 2019-09-15 14:28:00 
the Duration DURATION = Duration.between (from, to);      // represents the period from to to time 
Long days duration.toDays = ();               // total number of days during this time 
Long hours duration.toHours = ();             // hour period during which the number of 
Long minutes duration.toMinutes = ();         // this time minutes 
Long seconds The duration.getSeconds = ();       // the number of seconds of the period 
Long MILLISECONDS duration.toMillis = ();     // number of milliseconds this time 
Long nanoseconds duration.toNanos = ();       // number of nanoseconds during this time

修改 LOCALDATE, localtime, localdateti to Instant.

LocalDate, LocalTime, LocalDateTime, Instant immutable objects, modify these objects will return a copy of the object.

Increase, decrease the number of years, the number of months, days, etc., in order to LocalDateTime example:

LocalDateTime = LocalDateTime.of the LocalDateTime (2019, Month.SEPTEMBER, 12 is, 14, 32, 0 );
 // increase year 
LocalDateTime localDateTime.plusYears = (. 1 ); 
LocalDateTime = localDateTime.plus (. 1 , ChronoUnit.YEARS);
 / / decrease month 
LocalDateTime localDateTime.minusMonths = (. 1 ); 
LocalDateTime = localDateTime.minus (. 1 , ChronoUnit.MONTHS);  
 // modify certain values with
 // modifying year 2020 
LocalDateTime = localDateTime.withYear (2020 ); 
LocalDateTime localDateTime.with = (ChronoField.YEAR, 2020 );
 // time calculation
 // Get the first day of the year
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = localDate.with(firstDayOfYear());

TemporalAdjusters contains many static methods can be called directly, the following are some examples:

Method name description
dayOfWeekInMonth Returns the day of the week in the same month
firstDayOfMonth Return to the first day of the month
firstDayOfNextMonth Returns the first day of next month
firstDayOfNextYear Return next year on the first day
firstDayOfYear The first day of this year's return
firstInMonth Return to the same month in the first day of the week
lastDayOfMonth Returns the last day of the month
lastDayOfNextMonth The last day to return next month
lastDayOfNextYear The last day to return next year
lastDayOfYear Returns the last day of the year
lastInMonth Return to the same month in the last week
next / previous After a return / before a given day of the week
nextOrSame / previousOrSame After a return / before a given day of the week, if the value meets the conditions for direct return

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Formatting time

LocalDate localDate = LocalDate.of(2019, 9, 12);
String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("s1:"+ s1);
System.out.println("s2:"+ s2);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("获取当前时间:"+localDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:MM:SS");
String s = localDateTime.format(formatter);
System.out.println("Current time format: "+ s);
S1: 20,190,912 
S2: 2019-09-12 
Get Current Time: 2019-09-16T14: 54 is: 36.520 
format the current time: 2019-09-16 14:09:52

Resolution Time

LocalDate localDate1 = LocalDate.parse("20190912", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate2 = LocalDate.parse("2019-09-12", DateTimeFormatter.ISO_LOCAL_DATE);

to sum up:

SimpleDateFormat and compared, DateTimeFormatter is thread-safe.

Instant higher accuracy, accurate to the nanosecond range.

Duration can easily obtain the number of days, number of hours period.

LocalDateTime able to quickly obtain year, month, day, for the next month and so on.

TemporalAdjusters class contains many useful static methods, avoid writing your own tools.

Reprinted from: https: //www.cnblogs.com/wupeixuan/p/11511915.html

 

Guess you like

Origin www.cnblogs.com/mrjade/p/11527326.html