Java Dates and Times Flashcards
which package to import to work with date and time classes?
java.time.*
what are the four choices of Date and time usage?
LocalDate, LocalTime, LocalDateTime, ZonedDateTime
LocalDate
No Time, no timezone. Just Date. Eg.., My birthday
LocalTime
No date, no timezone. Just time. Eg..Midnight
LocalDateTime
Contains both date and time, no time zone. Eg.., Stroke of midnight on new years eve.
ZonedDateTime
contains date, time, timezone. Eg.., conference call at 9 a.m. EST
Example of different time zone offsets that can be listed
+02.00 or GMT+2 or UTC+2
Different ways to pass the month value for a date
Either MONTH ENUM(eg.. Month.JANUARY) or pass an int number.
The int starts with 1. Eg.. 1 for January
Month month = Month.JANUARY;
boolean b1=month==1;
boolean b2= month==Month.APRIL;
Output?
b1 does not compile. MONTH is an enum and cannot be compared with int.
b2 returns false as month is JANUARY
What is the first thing to do to get the ZonedDateTime?
We need to get the desired time zone.
How to create a zoned date time?
ZoneId zone = ZoneId.of(“US/Eastern”);
Three approaches to get the ZonedDateTime
- Pass all the values individually.
ZonedDateTime zoneTime = ZonedDateTime.of(2017, 8, 6, 11, 27, 30,200, zone); - Pass the LocalDate, LocaltTme and Zone objects
ZonedDateTime zoneTime = ZonedDateTime.of(LocalDateObject, LocalTimeObject, zone); - Pass the LocalDateTime and zone object.
ZonedDateTime.of(LocalDateTimeObject, zone);
One important thing to consider for ZonedDateTime object Creation
There is no option to pass in the Month Enum
To find the System time zone
ZoneId.systemDefault()
Why we cant invoke a constructor for Date and Time classes
Dates and Time classes have private constructors. We cannot invoke them. We have to use their static methods for any operations.
LocalDate date = new LocalDate(); What will happen
It will not compile
LocalDate date = LocalDate.of(2017, Month.JANUARY, 32);
It throws DateTimeException as it has invalid dates
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date = date.plusMinutes(1);
It does not compile.
We are trying to add the time to the date object.
What are the different approaches to create a period Object?
Period p1 = Period.of(years, months, days); Period p2 = Period.ofDays(1); Period p3 = Period.ofWeeks(weeks); Period p4 = Period.ofMonths(months); Period p5 = Period.ofYears(years);
What is one important catch about Period?
You cannot chain the period methods. If you do it returns the last period operation value with a compile warning, remember that Period is immutable.
If we print the period what is the format it is printed?
Eg,,. Period.of(1, 2, 3)
P1Y2M3D
P -> To show that it is a period measure
1Y -> No of Years with Y indicating the year
2M-> No of Months with M indicating the month
3D-> No of days with D indicating the Days
An important point to remember on displaying the Period?
If any of the values (either Year or month or days) are not available, then they are omitted
Period.ofMonths(3)
P3M
Period.of(0,20,47)
P20M47D. No issues in Period with Days greater than the no of days in month OR MONTH GREATER THAN THE NO OF MONTHS IN A YEAR
Period.ofWeeks(3)
P21D :)
Weeks are not one of the units a Period stores. So, it converts into a number of days and stores them.
What happens when we try to use a Period of Month to LocalTime?
UnsupportedTemporalTypeexception
What is Duration? why we use duration when we have Period?
- Duration is intended for smaller units of time.
2. Period is for Days and more of a time
How does the Period and Duration Outputs starts with?
Period: P
Duration: PT -> Period of Time
What are all the units that can be specified for Duration?
Days, hours, minutes, Seconds, milliseconds, nanoseconds
Different ways of creating the Duration
Duration.ofDays(1); Duration.ofHours(1); Duration.ofMinutes(1); Duration.ofSeconds(1); Duration.ofMillis(1); Duration.ofNanos(1);