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