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