Java Dates and Times Flashcards Preview

Java8 > Java Dates and Times > Flashcards

Flashcards in Java Dates and Times Deck (76)
Loading flashcards...
1

which package to import to work with date and time classes?

java.time.*

2

what are the four choices of Date and time usage?

LocalDate, LocalTime, LocalDateTime, ZonedDateTime

3

LocalDate

No Time, no timezone. Just Date. Eg.., My birthday

4

LocalTime

No date, no timezone. Just time. Eg..Midnight

5

LocalDateTime

Contains both date and time, no time zone. Eg.., Stroke of midnight on new years eve.

6

ZonedDateTime

contains date, time, timezone. Eg.., conference call at 9 a.m. EST

7

Example of different time zone offsets that can be listed

+02.00 or GMT+2 or UTC+2

8

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

9

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

10

What is the first thing to do to get the ZonedDateTime?

We need to get the desired time zone.

11

How to create a zoned date time?

ZoneId zone = ZoneId.of("US/Eastern");

12

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.
ZonedDateTime.of(LocalDateTimeObject, zone);

13

One important thing to consider for ZonedDateTime object Creation

There is no option to pass in the Month Enum

14

To find the System time zone

ZoneId.systemDefault()

15

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.

16

LocalDate date = new LocalDate(); What will happen

It will not compile

17

LocalDate date = LocalDate.of(2017, Month.JANUARY, 32);

It throws DateTimeException as it has invalid dates

18

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.

19

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);

20

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.

21

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

22

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

23

Period.ofMonths(3)

P3M

24

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

25

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.

26

What happens when we try to use a Period of Month to LocalTime?

UnsupportedTemporalTypeexception

27

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

28

How does the Period and Duration Outputs starts with?

Period: P
Duration: PT -> Period of Time

29

What are all the units that can be specified for Duration?

Days, hours, minutes, Seconds, milliseconds, nanoseconds

30

Different ways of creating the Duration

Duration.ofDays(1);
Duration.ofHours(1);
Duration.ofMinutes(1);
Duration.ofSeconds(1);
Duration.ofMillis(1);
Duration.ofNanos(1);