Java SE 8 Date/Time API Flashcards
What does java.time.LocalDate represents?
represents a date without time or time zone. (YYYY-MM-DD)
What does java.time.LocalTime represents?
time without dates or time zones (HH:MM:SS.nanosecond)
What does java.time.LocalDateTime represents?
represents both date and time without time zones (YYYY-MM-DD HH:MM:SS.nanosecond)
What is the difference between LocalDateTime and Instant?
LocalDateTime uses the default time zone, but Instant doesn’t.(Use UTC as timezone)
What is the java.time.Period used for?
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);
What is the Duration class used fr?
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);
What is TemporalUnit interface used for?
It represents date or time units such as seconds, hours, days, months, years and son on. The ChronoUnit implements this interface
What are the three import classed related to time zones?
ZoneId, ZoneOffSet, ZonedDateTime
What does ZoneId class represents?
represent time zones. Time zones are typically idfentified as using offset from Greenwich Mean Time (GTM, UTC/Greenwhich)
ZoneId AsiaKolkataZoneId = ZoneId.of(“Asia/Kolkata”);
What does ZoneOffset class represents?
represents the time-zone offset from UTC/Greenwich.
What does ZonedDateTime represents?
date, time and timezone
What class do you use to format date and time?
DateTimeFormatter
ex.
LocalTime wakeupTime = LocalTime.of(6, 0, 0);
System.out.println(“Wake up time: “ + DateTimeFormatter.ISO_TIME.format(wakeupTime));
What are the pre-defined constant for formatting date and time values?
- 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])
What to do if you want a custom format of DateTimeFormatter?
you may use the ofPattern() method
DateTimeFormatter customFormat = DateTimeFormatter.ofPattern(“dd MMM yyyy”);
System.out.println(customFormat.format(LocalDate.of(2016, Month.JANUARY, 01)));
What is the representation of these Periods toString()?
a) Period.ofMonths(3)
b) Period.of(0, 20, 47)
c) Period.ofWeeks(3)
a) P3M
b) P20M47D
c) P21D
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) PT24H
b) PT1H
c) PT1M
d) PT10S
e) PT0.001S
f) PT0.000000001S
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
- format()
- parse()
How are Period and Duration different when considering daylight savings when added to ZonedDateTime
- 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).