[go: up one dir, main page]

0% found this document useful (0 votes)
36 views6 pages

Z 19

java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

Z 19

java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
oart2023 20:18, Period and Duration in Java | Baeldung “ Period and Duration in Java F (httos /adsfreestarcom/? Last updated: Soptombor 23 2023, | written by baeldung htips/ wanubaeldung.com/author/baeldung) Java (https://wwwibaoldung.com/category/java) + Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: > CHECK OUT THE COURSE (/ls-course-start) Fisret eee Webinar: Driving the future of industrial automation part 2 with a Oi Se Canonical’s Ubuntu and Bosch... Ca recta) 2) htipsifweww.bacldung,comjava-period-duration 6 Canonica oart2023 20:18 Period and Duration in Java | Baeldung EE : 0 {https:/adsfreestarcom/? tutm_campaign-brandinggutm .medium-bannergutm source-baeldung.coma&utm_content-baeldung_incontent 2 . Overview Inthis quick tutorial, were going to take a look at two new classes for working with dates introduced in Java 8: Period’and Duration. Both classes can be used to represent an amount of time or determine the difference between two dates. The ‘main distinction between the two classes is that Period uses date-based values, while Duration uses time-based values, 2. Period Class The Period/class uses the units year, month and day to represent a period of time. We can obtain a Period object as the difference between two dates by using the betweend method Lecoloate startate = Loetoate-of 2018, 2, 20); Cfeetbate enete" Locadutesof 927, 3, 253 0 Perioc period = Period.between(startoate, endbate); ‘Then, we can determine the date units of the period using the methods get¥ears(, getMonths), getDays0. o 5 bx ‘Tout Creative Cloud, Pour toutes vos créations. cp i ‘Apple IPhone xr woe W 4 (https //ads reestarcom/? In this case, the isNegatived method, which returns trueif any of the units are negative, can be used to determine if the endDate is higher than the startDate sesersFoteipriog tetas Oo If isNegatived returns false, then the startDate is earlier than the enaDate value. Another way to ereate a Period object is based on the number of days, months, weeks or years using dedicated methods: Perioe frowunits = Period.of(3, 19, 19); Oo Perioc frondays = Per‘od.ofDays(38) 5 Pariac fromonchs = Perios.oftonths($): Perioc fromYears = Period.ofvears(18); Perioc fromseks = Parsod.ofWeeks (48); assertequals(280, fronoeks.gecdays (0): IFonly one of the values is present, for example by using the ofDays) method, then the value of the gther units is 0. Inthe case of the of W/eeks( method. the parameter value is used to sot the number of days by multiplying it by 7. htipsifweww.bacldung,comava-period-duration 26 oart2023 20:18, Period and Duration in Java | Baeldung “ \We can also create a Period object by parsing a text sequence, which has to have the format "PrvnMnD assertEquals(2, fromCharYears.getYears()); 0 Perioc froscharUnits = Period.parse("P2¥34S0") ; assertequsls(s, froncharUnits, et3ays()): ‘The value of the period can be increased or decreased by using methods of the form plusx@and minusX0, where X represents the date unit. HUMES eminem, 0 3. Duration Class ‘The Duration class represents an interval of time in seconds or nanoseconds and is most sulted for handling shorter amounts of time, in cases that require more precision, \We can determine the difference between two instants as a Duration object using the between? method: Instant end = Instant.parse("2617-16-03710:16:30.002") 5 O Duration aurat‘or Duracion.between(start, end); ‘Then we can use the getSecondst) or getNanoseconds() methods to determine the value of the time units Alternatively, we can obtain a Duration instance from two LocalDateTime instances Lectsterien start» ientontarne para zee 0 0 {Sithaetiay an lttbuesminsnetns. onic) i Duration. between start, end) getseconds(); v htipsifweww.bacldung,comava-period-duration 36 oart2023 20:18, Period and Duration in Java | Baeldung Jerending an the startand ens joints, a Duration can be: Denending tartand encitjme points, a Dura ct «+ Negative: start < end +» Zoro: start - enc! + Positive: start > end Also, the standard Time AP offers convenient methods to check if Durations Zero or Negative, LecalWateTine therine = Localbaterine.parse("2823-09-09T28:00:60"); assertTrue(Duration.between(theT ine, theTine) 1szero()); aesertFalse (Duration. between theT ine, theT ine) skegative(); Its worth noting that before Java 18, the Duration class only provided isZero and isNegatived. The Duration isPositive) (https://devdocs.io/openidk-18 /java.base /java/time/duration#isPositive0) method has boon introduced since Java version 18: \We can also obtain a Duration object based on several time units, using the methods ofDays0, oftours@, ofMilisd, ofMinutes), ofNanos), ofSecondst Se Loe, Moa oy O Duration frosiinutes = Duration. ofMinutes (69); ‘To create a Duration object based on a text sequence, this has to be of the form *PnDTnHinMn nS’ Duration froschart Duration freacark « Duratfon.pars(“PLOTINAMUE.S57); Oo Duration.parse("PTi0M") ; ‘Aduration can be converted to other time units using foDays0, toHours0, toMillis0, foMinutest | assertEquats(1, framinutes.tatlours())5 Oo ‘A duration value can be increased or decreased boy using methods of the form plusX0)or minusxd. where Xan stand for days. hours, millis, minutes, nanos or seconds: ae G ry a @ Register today: Harness the F deni Data © Fereock > (https /ads reestarcom/? asserthquals(120, duration. plusseconds (60) .getSeconds()); We can also use the plus(land minus) methods with a parameter that specifies the TemporalUnitto add or subtract htipsifweww.bacldung,comava-period-duration 46 oart2023 20:18 Period and Duration in Java | Baeldung rerttas se mg hAEs, cron C008) etsconte 0 O Sirutgutoie,areion tnt, van stems) waren 4. Conclusion Inthis article, we've seen how we can use the Period and Duration classes, As always, the full source code of the examples can be found over on GitHub {https://githubscom/eugenp /utorials/tree/master/core-lava-modules/core-java-8-datetime) Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: >> CHECK OUT THE COURSE (/1s-course-ond) Learning to build your API with Spring? Download the E-book (/rest-api-spring-guide) Comments are closed on this article! htipsifweww.bacldung,comava-period-duration oart2023 20:18, Period and Duration in Java | Baeldung “ ‘couRSES SERIES ‘ABOUT htipsifweww.bacldung,comava-period-duration

You might also like