New date and Time APIs in Java 8

Source: Internet
Author: User
Tags string format

New date and Time APIs in Java 8 use localdate and localtime

An instance of Localdate is an immutable object that provides only a simple date and does not contain time information for the day. Also, it does not come with any time zone-related information

Localdate instances provide several ways to read commonly used values such as year, month, day of the week, etc.

LocalDate date = LocalDate.of(2014,3,18);int year = date.getYear();Month month = date.getMonth();int day = date.getDayOfMonth();DayOfWeek dow = date.getDayOfWeek();int len = date.lengthOfMonth();boolean leap = date.isLeapYear();//获取当前的日期LocalDate today = LocalDate.now();

You can also pass a Temporalfield parameter to the Get method to get the information for your nursery rhymes.

int year = date.get(ChronoField.YEAR);int month = date.get(ChronoField.MONTH_OF_YEAR);int day = date.get(ChronoField.DAY_OF_MONTH);

Similarly, the time of day, for example: 13:45:20, you can use the LocalTime class to represent

LocalTime time = LocalTime.of(13,45,20);int hour = time.getHour();int minute = time.getMinute();int second = time.getSecond();

Both can be created by parsing the strings that represent them;

LocalDate date = LocalDate.parse("2014-03-18");LocalTime time = LocalTime.parse("13:45:20");
Merge Date and time

This compound is called localdatetime, which is the localdate and localtime. It also represents the date and time, but not the time zone information

//2014-03-18T13:45:20LocalDateTime dt1 = LocalDateTime.of(2014,Month.MARCH,18,13,45,20);LocalDateTime dt2 = LocalDateTime.of(date,time);LocalDateTime dt3 = date.atTime(13,45,20);LocalDateTime dt4 = date.atTime(time);LocalDateTime dt5 = date.atTime(date);

You can extract localdate or localtime from LocalDateTime.

LocalDate date1 = dt1.toLocalDate();LocalTime time1 = dt1.toLocalTime();
Date and time format of the machine

The way the Java.time.Instant class models time is basically based on the number of seconds that the Unix first time (midnight of January 1, 1970) experienced.

Instant instant = Instant.ofEpochSecond(3);//1970-01-01T00:00:03ZInstant.ofEpochSecond(3, 1_000_000_000);//1970-01-01T00:00:04Z 第二参数以纳秒为单位的参数值
Duration or period

Static Factory method for duration class between

 Duration.between(time, time2); Duration.between(dateTime1, dateTime2); Duration.between(instant1, instant12);

Since LocalDateTime and instant are designed for different purposes, one is for easy reading and the other is to facilitate machine handling. So you can't mix the two.

In addition, since the duration class is primarily used to measure time in seconds and nanoseconds, it is not possible to pass only one Localdate object to the between method for parameters

Period class of between, can be used to get the length between two localdate

Period between = Period.between(LocalDate.of(201438), LocalDate.of(2014318));

Creating duration and Period objects

Duration.ofMillis(3);Duration.of(3, ChronoUnit.MINUTES);Period.ofDays(10);Period.ofWeeks(3);Period.of(261);
Manipulate, parse, and format dates

If you already have a Localdate object and want to create a modified version of it, the most straightforward and simplest way is to use the Withattribute method.

localdate of = Localdate. (2014 , 3 , 18 ); Localdate localdate = of. withyear  (2011 ); Localdate localDate1 = localdate. withdayofmonth  (25 ); //2011-03-25  localDate1. with  (Chronofield. month_of_year , 9 ); //2011-09-25  Localdate LocalDate2 = of. plusweeks  (1 ); //2014-03-25  Localdate localDate3 = LocalDate2. minusyears  (3 ); //2011-03-25  LocalDate3. plus  (6 , Chronounit. months ); //2011-09-25   
Using Temporaladjuster
LocalDate of = LocalDate.of(2014318);LocalDate with = of.with(nextOrSame(DayOfWeek.SUNDAY));//2014-03-23LocalDate with1 = with.with(lastDayOfMonth());//2014-03-31
Method Name Description
Dayofweekinmonth Creates a new date whose value is the day ordinal of the week in the same one month
Firstdayofmonth First day of the month
Firstdayofnextmonth The first day of next month
Firstdayofnextyear The first day of next year
Firstdayofyear
Lastdayofmonth
Lastdayofnextmonth
Lastdayofnextyear
Lastdayofyear
Lastinmonth Its value is the same as one months, the last one that meets the requirements of the day of the week
Next/previous Set its value to the date after the date adjustment, or before the adjustment, the first one that meets the specified day of the week
Nextorsame/previousorsame Set its value to date adjusted or before adjustment, the first date that meets the specified day of the week, and if the date is already met, return the object directly
Print output and parse date-time object
LocalDate of1 = LocalDate.of(2014318);String format = of1.format(DateTimeFormatter.BASIC_ISO_DATE);//20140318String format1 = of1.format(DateTimeFormatter.ISO_LOCAL_DATE);//2014-03-18

You can use the factory method parse to reach a hit date object

LocalDate parse = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);//2014-03-18LocalDate parse1 = LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);//2014-03-18

Create datetimeformatter by touching a pattern

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");LocalDate of1 = LocalDate.of(2014318);String format = of1.format(formatter);LocalDate parse = LocalDate.parse(format, formatter);

New date and time APIs in Java 8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.