New date and time APIs in Java8

Source: Internet
Author: User
Tags string format

1. Calculate the time interval

Before Java8, we wanted to determine the length of a method's run time, which could be:

Long start = System.currenttimemillis ();d osomething (); Long end = System.currenttimemillis (); System.out.println (End-start);


Java8, you can do this.

Instant start = Instant.now ();d osomething () Instant end = Instant.now ();D uration time = Duration.between (start, end); Long seconds = time.getseconds ();//seconds means long Millis = Time.tomillis ();//milliseconds means System.out.println (seconds); System.out.println (Millis);

You can easily select units that represent time intervals in nanoseconds, milliseconds, seconds, minutes, hours, or days.


It is possible to compare the first method to a faster execution than the second:

Instant start = Instant.now ();d osomething () Instant end = Instant.now ();D uration time = Duration.between (start, end); I Nstant Start2 = Instant.now ();d oSomething2 () Instant end2 = Instant.now ();D uration time2 = Duration.between (Start2, End2 ); Boolean fast = Time.minus (time2). Isnegative (); SYSTEM.OUT.PRINTLN (FAST);


2. Local date and time
Localdate now = Localdate.now (); Localdate date = Localdate.of (2005, 5, 10); Localdate date2 = Localdate.of (2003, Month.february, 5); System.out.println (now); SYSTEM.OUT.PRINTLN (date); System.out.println (DATE2); LocalTime now2 = Localtime.now (); LocalTime time = Localtime.of (22, 50, 56); System.out.println (NOW2); System.out.println (time);


3. Date formatting
Formats string format = DateTimeFormatter.ISO_LOCAL_DATE.format (Localdate.now ()) in different ways built-in; String format2 = DateTimeFormatter.ISO_LOCAL_TIME.format (Localtime.now ()); String FORMAT3 = DateTimeFormatter.ISO_DATE.format (Localdatetime.now ()); String format4 = DateTimeFormatter.ISO_INSTANT.format (Instant.now ()); SYSTEM.OUT.PRINTLN (format); System.out.println (FORMAT2); System.out.println (FORMAT3); System.out.println (FORMAT4);//format DateTimeFormatter formatter = datetimeformatter.oflocalizeddate in standard format ( Formatstyle.full); String format5 = Formatter.format (Localdatetime.now ()); System.out.println (FORMAT5);//format DateTimeFormatter pattern = Datetimeformatter.ofpattern as specified ("Yyyy-mm-dd E hh:mm: SS "); String Format6 = Pattern.format (Localdatetime.now ()); System.out.println (FORMAT6);


New date and time APIs in Java8

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.