In the Java Date-Time library, there’s several classes you can use to store a date and time. The most important ones are LocalDate, LocalTime, and one more class that combines these two classes into one, called LocalDateTime.
These classes are just containers. You should only use them to store date and a time-of-day. They cannot represent a moment since they don’t have a time zone or an offset-from-UTC. In other words, are not tied to the timeline. So the word “local”, used in the name of the class, indicates a general locality, but not a particular locality. It seems like these classes have many disadvantages, so do we really need them? Well, yes. And the cases where we need them are:
- When a specific date needs to work for every location.
- When an event doesn’t have a determinate time zone.
For example, “New year starts at midnight on the 1st of January 2022”. It doesn’t matter where you are in the world. It’ll always start at the same date and time according to your time zone. You can see that not having a date and time connected to a specific timezone, in this case, is really practical. This example teaches us that the LocalDateTime class, which represents midnight on the 1st of January 2022”, is not a moment on the timeline. That’s because we don’t have a unique date for every place in the world. But rather a date that will change according to the timezone of a specific place in the world.
Let’s jump into the code.
We already used the LocalDate class in the previous video. Let’s say I was born on the 29th of December 1999.
val myBirthDay = LocalDate.of(1999, Month.DECEMBER, 29)
and print it.
println("myBirthDay: $myBirthDay")
Nothing new so far, but it’s important to know that the method of() is part of the standardization of methods that returns an instance of a class after validating the input.
The LocalDate defines utility methods and properties. For example, we can use dayOfWeek to get my birthday:
val birthDayOfTheWeek = myBirthDay.dayOfWeek
Now we can print it.
println("You're born on: $birthDayOfTheWeek")
By executing it, we’ll get the current day printed in letters.
LocalTime works as the LocalDate class does, but you can only store time. For example, you might want to store the time for your alarm, which has to ring at 7.30 am. You can say:
val alarm = LocalTime.of(7, 30)
and print it:
println("meetingTime: $alarm")
The class you’d probably use the most is LocalDateTime which lets you store both a time and date. If you want to set a reminder to get your toothbrush to avoid bad breath, well, this is the class for you. So let’s say my flight for Hawaii is leaving on the 10th of September 2022 at 2 pm. Of course, the road to get to the airport is gonna take some time. So I’ll set the remainder at 9am. Let’s write:
val reminder = LocalDateTime.of(2022, Month.SEPTEMBER, 10, 9, 0)
and print:
println("reminder: $reminder")
Now you are sure you’ll always have a good breath while on holiday.
There’s other useful data classes, like MonthDay, that allow you to store or get a specific month and day. To get the current month and day we can write:
val monthDay = MonthDay.now()
and print it:
println("monthDay: $monthDay")
You can useYearMonth, which does the same thing as MonthDay but handles a specific year and month. We can get the current year and month saying:
val yearMonth = YearMonth.now()
and print it:
println("yearMonth: $yearMonth")
And then you can use classic classes like Year, which lets you work with only one value. As might have guessed, to get the current year, we need to write:
val year = Year.now()
and by printing it, we’ll get the current year.
println("year: $year")