Time package common API for Java 8

Source: Internet
Author: User
Tags diff

The Date, Canlender, and SimpleDateFormat classes have little advantage in front of the new time packet

Date localdate, time localtime, date time localdatetime.

Time zone ZoneID, point Instant, Time unit chronounit, formatting DateTimeFormatter

Localdate

Current date

    Localdate localdate = Localdate.now ();

Specified date

    //  (Mode 1)    Localdate localDate1 = Localdate.of ();     // (Mode 2)    Localdate LocalDate2 = Localdate.parse ("2017-07-25");

Date Plus/minus how many units of time

    //  plus 1 months (Mode 1)    Localdate localdate = localdate.plus (1, chronounit.months);     // plus 1 days (Mode 2)    Localdate localdate = localdate.plusdays (1);

Parse Date

    int dayofmonth = Localdate.getdayofmonth ();

Whether a leap year

    boolean b = localdate.isleapyear ();

Whether it is a date before/after

    boolean b = Localdate1.isbefore (localDate2);     Boolean b = Localdate1.isafter (LOCALDATE2);

Two dates how many time units are different

    How many days difference
Long diff = ChronoUnit.DAYS.between (localDate1, localDate2); //difference in how many seconds
long diff = ChronoUnit.SECONDS.between (localDate1, LocalDate2);

Formatting

    DateTimeFormatter DateTimeFormatter = Datetimeformatter.ofpattern ("Yyyy-mm-dd");     = Datetimeformatter.format (localdate);

Two dates are equal

    Boolean b = localdate1.equals (LOCALDATE2);

LocalTime

Most APIs are the same as localdate, where the difference is

1, cannot judge whether leap year

2.Localdate Object ToString () returns "2017-11-09", LocalTime object ToString () returns "11:16:50.144" (if the number of milliseconds is 0, the last digit is not displayed), If you want to return the HH:MM:SS format, you need to format it.

LocalDateTime

Most APIs are the same as localdate, where the difference is

1, can not directly judge whether leap years, need to first converted to Localdate

Boolean B = localdatetime.tolocaldate (). Isleapyear ();

2,LocalDateTime object ToString () returns "2017-11-09t11:16:50.144", if you want to return the HH:MM:SS format, you need to format.

Working with Java.util.Date objects in legacy code

The date is converted to a Instant object, and then the Localdate, LocalTime, and LocalDateTime respective from factory methods are used as needed to generate the object

    // Legacy Code    Date birthday = user.getbirthday ();     = order.getordertime ();     // Conversion    Localdate Birthdayex = localdate.from (birthday.toinstant ());     = Localdatetime.from (Ordertime.toinstant ());

Working with Java.util.Calendar objects in legacy code

As with java.util.Date objects, the Calendar object also has a Toinstant method.

Time package common API for 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.