Working with Date and Times Flashcards
What we need to import to work with the modern date and time classes?
import java.time.*; // import time classes
modern date and time classes
-
LocalDate
: Contains just a date—no time and no time zone.
A good example of LocalDate is your birthday this year. It is your birthday for a full day, regardless of what time it is. -
LocalTime
: Contains just a time—no date and no time zone.
A good example of LocalTime is midnight. It is midnight at the same time every day. -
LocalDateTime
: Contains both a date and time but no time zone.
A good example of LocalDateTime is “the stroke of midnight on New Year’s Eve.” Midnight on January 2 isn’t nearly as special, making the date elatively unimportant, and clearly an hour after midnight isn’t as special either. -
ZonedDateTime
: Contains a date, time, and time zone.
A good example of ZonedDateTime is “a conference call at 9:00 a.m. EST.” If you live in California, you’ll have to get up really early since the call is at 6:00 a.m. local time!
You obtain date and time instances using a static method now()
:
System.out.println(LocalDate.now()); // 2024-05-07 System.out.println(LocalTime.now()); // 16:55:57.880929400 System.out.println(LocalDateTime.now()); // 2024-05-07T16:56:25.920557500 System.out.println(ZonedDateTime.now()); // 2024-05-07T16:56:51.549554300+08:00[Asia/Kuala_Lumpur]
The output uses T to separate the date and time when converting LocalDateTime to a String.
> [!Note]
Greenwich Mean Time is a time zone in Europe that is used as time zone zero when discussing offsets. You might have also heard of Coordinated Universal Time, which is a time zone standard. It is abbreviated as UTC, as a compromise between the English and French names. (That’s not a typo. UTC isn’t actually the proper acronym in either language!) UTC uses the same time zone zero as GMT.
Covert to GMT
2022–06–20T06:50+05:30[Asia/Kolkata] 2022–06–20T07:50-05:00[US/Eastern]
Remember that you need to add when subtracting a negative number. After converting to GMT, you can see that the U.S. Eastern time is 11 and a half hours behind the Kolkata time.
2022–06–20T06:50+05:30[Asia/Kolkata] // GMT 2022–06–20 01:20 2022–06–20T07:50-05:00[US/Eastern] // GMT 2022–06–20 12:50
> [!NOTE]
Time Zone Rules
The time zone offset can be listed in different ways: +02:00, GMT+2, and UTC+2 all mean the same thing. You might see any of them on the exam.
LocalDate
of()
method signature.
public static LocalDate of(int year, int month, int dayOfMonth) public static LocalDate of(int year, Month month, int dayOfMonth)
EX :
var date1 = LocalDate.of(2022, Month.JANUARY, 20); var date2 = LocalDate.of(2022, 1, 20);
> [!Note]
Up to now, we’ve been continually telling you that Java counts starting with 0. Well, months are an exception. For months in the new date and time methods, Java counts starting from 1, just as we humans do.
LocalTime
of()
method signatures
public static LocalTime of(int hour, int minute) public static LocalTime of(int hour, int minute, int second) public static LocalTime of(int hour, int minute, int second, int nanos)
EX :
var time1 = LocalTime.of(6, 15); // hour and minute var time2 = LocalTime.of(6, 15, 30); // + seconds var time3 = LocalTime.of(6, 15, 30, 200); // + nanoseconds
LocalDateTime
of()
method signatures
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos)
take a Month
reference:
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime of(LocalDate date, LocalTime time)
EX :
var dateTime1 = LocalDateTime.of(2022, Month.JANUARY, 20, 6, 15, 30); var dateTime2 = LocalDateTime.of(date1, time1);
ZonedDateTime
In order to create a ZonedDateTime, we first need to get the desired time zone. We will use US/Eastern in our examples:
var zone = ZoneId.of("US/Eastern"); var zoned1 = ZonedDateTime.of(2022, 1, 20, 6, 15, 30, 200, zone); var zoned2 = ZonedDateTime.of(date1, time1, zone); var zoned3 = ZonedDateTime.of(dateTime1, zone);
Although there are other ways of creating a ZonedDateTime, you only need to know three for the exam:
public static ZonedDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos, ZoneId zone) public static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone) public static ZonedDateTime of(LocalDateTime dateTime, ZoneId zone)
Notice that there isn’t an option to pass in the Month enum.
The date and time classes have private constructors
along with static methods
that return instances. This is known as the factory pattern
. The exam creators may throw something like this at you:
var d = new LocalDate(); // DOES NOT COMPILE
You are not allowed to construct a date or time object directly.
var d = LocalDate.of(2022, Month.JANUARY, 32)
When you pass invalid numbers to of(), it will throw DateTimeException
java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1-28/31): 32
Manipulating Dates and Times
The date and time classes are immutable. Remember to assign the results of these methods to a reference variable so they are not lost.
12: var date = LocalDate.of(2022, Month.JANUARY, 20); 13: System.out.println(date); // 2022–01–20 14: date = date.plusDays(2); 15: System.out.println(date); // 2022–01–22 16: date = date.plusWeeks(1); 17: System.out.println(date); // 2022–01–29 18: date = date.plusMonths(1); 19: System.out.println(date); // 2022–02–28 20: date = date.plusYears(5); 21: System.out.println(date); // 2027–02–28 22: var date = LocalDate.of(2024, Month.JANUARY, 20); 23: var time = LocalTime.of(5, 15); 24: var dateTime = LocalDateTime.of(date, time); 25: System.out.println(dateTime); // 2024–01–20T05:15 26: dateTime = dateTime.minusDays(1); 27: System.out.println(dateTime); // 2024–01–19T05:15 28: dateTime = dateTime.minusHours(10); 29: System.out.println(dateTime); // 2024–01–18T19:15 30: dateTime = dateTime.minusSeconds(30); 31: System.out.println(dateTime); // 2024–01–18T19:14:30
var date = LocalDate.of(2024, Month.JANUARY, 20); var time = LocalTime.of(5, 15); var dateTime = LocalDateTime.of(date, time) .minusDays(1).minusHours(10).minusSeconds(30);
It is common for date and time methods to be chained.