Flashcards in Java Dates and Times Deck (76)
which package to import to work with date and time classes?
what are the four choices of Date and time usage?
LocalDate, LocalTime, LocalDateTime, ZonedDateTime
No Time, no timezone. Just Date. Eg.., My birthday
No date, no timezone. Just time. Eg..Midnight
Contains both date and time, no time zone. Eg.., Stroke of midnight on new years eve.
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 b2= month==Month.APRIL;
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
1. Pass all the values individually.
ZonedDateTime zoneTime = ZonedDateTime.of(2017, 8, 6, 11, 27, 30,200, zone);
2. Pass the LocalDate, LocaltTme and Zone objects
ZonedDateTime zoneTime = ZonedDateTime.of(LocalDateObject, LocalTimeObject, zone);
3. Pass the LocalDateTime and zone object.
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
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)
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
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
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?
What is Duration? why we use duration when we have Period?
1. 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?
Duration: PT -> Period of Time
What are all the units that can be specified for Duration?
Days, hours, minutes, Seconds, milliseconds, nanoseconds