Java.time Time Date offset class
Java.time the primary class that handles the local date time offset is java.time.
Duration, Java.time
. Period;
Period-deals with the number of dates based on time.
Duration-deals with the amount of time spent on time.
Duration
Static Construction Method Summary |
Static Duration
Static Duration Static Duration
|
between (LocalTime startinclusive,localtime endexclusive)
of (Long amount,temporalunit Unit) ofhours (Long hours) ofminutes (Long minutes) Ofseconds (Long seconds)
|
Use two time difference to generate duration,temporal including localdatetime,zoneddatetime, etc.
Create duration with specified Temperalunit
|
Member Method Summary |
+long
+duration +long +list<temporalunit>
+duration
+boolean |
tohours ()/Tominutes ()/toseconds () Tomillis ()/Tonanos ()
Withseconds (Long seconds) Getseconds ()/Getnanos () Getunits ()
Plus(long amounttoadd,temporaunit unit) plushours (long)/Plusminute(long)/plusseconds(Long)
Iszero () |
Converts a duration to a value of the corresponding unit
Modify numeric values Get numeric values (effects same as Toseconds,tonanos) Get parameter table
To increase or decrease the duration
Check whether the value is 0 |
Period
Static Construction Method Summary |
Static Period
Static Period Static Period
|
between (Localdate startinclusive,localdate endexclusive)
of (int years,int months,int days) Ofyears (Long hours) ofmonths (Long minutes) Ofdayss (Long seconds)
|
Use two time difference to generate duration,temporal including localdatetime,zoneddatetime, etc.
Creates a period using the specified numeric value
|
Member Method Summary |
+long
+period +list<temporalunit> +long
+period
+boolean |
tototalmonths ()
Withdays(long seconds)/withmonths/withyears Getunits () Getyears ()/getmonths ()/GetDays ()
Plus(long amounttoadd,temporaunit unit) plusdays (long)/plusmonths(long)/plusyears(Long) Iszero () |
Converts a period to a value of the corresponding unit
Modify numeric values Get parameter table Get parameters
To increase or decrease the period
To determine if it is empty |
using the sample
Import java.time.*; ...//Calculate the time difference of two temporal objects localdate date1 = Localdate.of (1997,1,1); Localdate date2 = Localdate.now (); Period Period = Period.between (DATE1,DATE2); System.out.println ("Date1:" +date1+ "\ndate2:" +date2); System.out.println ("Period->years:" +period.getyears () + "months:" +period.getmonths () + "Days:" +period.getdays () ); LocalTime time1 = Localtime.of (0,0,0); LocalTime time2 = Localtime.now (); System.out.println ("time1:" +time1+ "\ntime2:" +time2); Duration Duration = Duration.between (time1,time2); System.out.print ("duration->"); Duration.getunits (). ForEach (Temporalunit-> System.out.print (temporalunit+ ":" +duration.get (temporalunit) + "")) ;
The results are as follows:
date1:1997-01-01 date2:2017-02-28 period->years:20 months:1 days:27 time1:00:00 time2:19:21:16.062 Duration->Se conds:69676 nanos:62000000