Java SE 8 Date/Time API Flashcards

1
Q

What does java.time.LocalDate represents?

A

represents a date without time or time zone. (YYYY-MM-DD)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does java.time.LocalTime represents?

A

time without dates or time zones (HH:MM:SS.nanosecond)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does java.time.LocalDateTime represents?

A

represents both date and time without time zones (YYYY-MM-DD HH:MM:SS.nanosecond)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between LocalDateTime and Instant?

A

LocalDateTime uses the default time zone, but Instant doesn’t.(Use UTC as timezone)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the java.time.Period used for?

A

Used to measure an amount of time in terms of year, months and days

ex:

LocalDate manufacturingDate = LocalDate.of(2016, Month.JANUARY, 1);
LocalDate expiryDate = LocalDate.of(2018, Month.JULY, 18);
Period expiry = Period.between(manufacturingDate, expiryDate);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Duration class used fr?

A

Represents time in terms of hour, minutes, seconds and so on ex:

LocalDateTime comingMidnight =
LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MIDNIGHT);
LocalDateTime now = LocalDateTime.now();

Duration between = Duration.between(now, comingMidnight);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is TemporalUnit interface used for?

A

It represents date or time units such as seconds, hours, days, months, years and son on. The ChronoUnit implements this interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the three import classed related to time zones?

A

ZoneId, ZoneOffSet, ZonedDateTime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does ZoneId class represents?

A

represent time zones. Time zones are typically idfentified as using offset from Greenwich Mean Time (GTM, UTC/Greenwhich)

ZoneId AsiaKolkataZoneId = ZoneId.of(“Asia/Kolkata”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does ZoneOffset class represents?

A

represents the time-zone offset from UTC/Greenwich.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does ZonedDateTime represents?

A

date, time and timezone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What class do you use to format date and time?

A

DateTimeFormatter

ex.

LocalTime wakeupTime = LocalTime.of(6, 0, 0);
System.out.println(“Wake up time: “ + DateTimeFormatter.ISO_TIME.format(wakeupTime));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the pre-defined constant for formatting date and time values?

A
  • ISO_DATE (2015-11-05)
  • ISO_TIME (11:25:47.624)
  • RFC_1123_DATE_TIME (Thu, 5 Nov 2015 11:27:22 +0530)
  • ISO_ZONED_DATE_TIME (2015-11-05T11:30:33.49+05:30[Asia/Kolkata])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What to do if you want a custom format of DateTimeFormatter?

A

you may use the ofPattern() method

DateTimeFormatter customFormat = DateTimeFormatter.ofPattern(“dd MMM yyyy”);
System.out.println(customFormat.format(LocalDate.of(2016, Month.JANUARY, 01)));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the representation of these Periods toString()?

a) Period.ofMonths(3)
b) Period.of(0, 20, 47)
c) Period.ofWeeks(3)

A

a) P3M
b) P20M47D
c) P21D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the representation of these Duration(time) to string?

a) Duration.ofDays(1)
b) Duration.ofHours(1)
c) Duration.ofMinutes(1)
d) Duration.ofSeconds(10)
e) Duration.ofMillis(1)
f) Duration.ofNanos(1)

A

a) PT24H
b) PT1H
c) PT1M
d) PT10S
e) PT0.001S
f) PT0.000000001S

17
Q

Fill in the blank:

Once you hate the NumberFormatInstance you can call ____ to turn a number into a String and ____ to return a string into a number

A
  • format()

- parse()

18
Q

How are Period and Duration different when considering daylight savings when added to ZonedDateTime

A
  • A Duration will add an exact number of seconds, thus a duration of one day is always exactly 24 hours. By contrast, a Period will add a conceptual day, trying to maintain the local time.
  • For example, consider adding a period of one day and a duration of one day to 18:00 on the evening before a daylight savings gap. The Period will add the conceptual day and result in a ZonedDateTime at 18:00 the following day. By contrast, the Duration will add exactly 24 hours, resulting in a ZonedDateTime at 19:00 the following day (assuming a one hour DST gap).