When you write code to specify an amount of time, you should use the class or method that best meets your needs. In this lecture, you’ll see three different classes: Period, Duration, and ChronoUnit.
A Duration measures an amount of time using time-based values like seconds and nanoseconds. The Duration class is measured in seconds or nanoseconds, and it doesn’t provide date-based properties like years, months, and days. However, it offers methods that convert to days, hours, and minutes. You should use Duration to measure the difference in time with machine-based time, such as the Instant object.
A Period uses date-based values such as years, months, and days to define an amount of time. The period class provides get methods like getDays, getMonths and getYears, so that you can extract the amount of time from the period. You need to put all of these three units together to get the complete date. You should use Period to calculate the difference between dates.
The most important difference between Duration and Period is that Duration doesn’t take into consideration any date-based event like a leap year of the daylight saving time. On the other hand, both classes provide utility methods to add and subtract time units, like days or seconds.
ChronoUnit is an enum that defines the time units such as HOURS, DAYS, and MONTHS. This class defines the method between that returns the difference in time between two temporal-based objects in a single unit of time.
To calculate the difference between two Duration objects, let’s write:
val betweenWithDuration = Duration.between(Instant.EPOCH, Instant.now())
Here Instant.EPOCH represents the 1st of January 1970.
We can convert betweenWithDuration to the number of days. So let’s write:
val daysInBetweenWithDuration = betweenWithDuration.toDays()
And print it.
Now we can compare two Periods, and specifically, we want to know how many days, months, and years we’ve lived until this moment. Let’s say I was born on the 29th of December 2000. So let’s write:
val birthDay = LocalDate.of(1999, Month.DECEMBER, 29)
We can compare this date with the current date. To get the current date write:
val today = LocalDate.now()
To get the total duration of my life, we can use the method between. So
val ageWithPeriod = Period.between(birthDay, today)
And finally, we can print the period in days, months, and years. Remember that you need to put all of these three units together to get the complete date.
println(
"You are ${ageWithPeriod.years} years, " +
"${ageWithPeriod.months} moths " +
"and ${ageWithPeriod.days} days old"
)
We can do the same type of calculation using ChronoUnit. But this time, we want to calculate the amount of time I lived until now, only using days instead of having a full period of years, months, and days. Okay, so to make this calculation, we need first to specify in which TermpoalUnit we want to calculate the difference. Since we said days, let’s write:
val ageWithChrono = ChronoUnit.DAYS.between(birthDay, today)
And we can print it:
println("ageWithChrono: $ageWithChrono days")
You can notice the difference. From now on, instead of saying I’m 21 years old, I can say I’m 7888 days old, or as young mothers like to say, I’m 259 months old.