8. Java 8: Use Java SE8 Date/Time API Flashcards

1
Q

True/False

The new date/time API is not thread-safe because it resulted in performance problems in the past.

A

False

The new API is thread-safe and all objects are immutable (just like a String). This was the main problem with the old date / Calendar API.

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

What are the 4 ways a Instant object be created?

A

Instant.now()
Instant.ofEpochSecond(999999)
Instant.ofEpochMilli(99999L)
Instant.parse(“2015-07-10T12:00:00Z”)

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

How can you get the EPOCH in seconds from a instant object?

A

instant.getEpochSecond();

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

How can you get the EPOCH in milliseconds from a instant object?

A

instant.getEpochMilli();

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

What are the 3 ways to create a LocalDate object?

A

LocalDate.now();
LocalDate.of(1979, Month.FEBRUARY, 15);
LocalDate.ofYearDay(2015,256)

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

What are the 4 ways of creating a LocalTime object?

A

LocalTime.now()
LocalTime.of(12, 0) // 12:00
LocalTime.of(13,33,45) // 13:33:45
LocalTime.ofSecondOfDay(121) // 12:02:01

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

Name 2 ways of creating a LocalDateTime object?

A

LocalDateTime.now();

LocalDateTime.of(2015, 12, 31, 13, 45); //2015-12-31 13:45

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

What is the difference between a LocalDateTime object and a ZonedDateTime object?

A

ZonedDateTime contains timezone information.

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

How would one create a ZonedDateTime object using the current time and the timezone of Europe/Berlin? And a ZonedDateTime using a offset of 3 hours from GMT?

A

ZoneId zoneIdBerlin = ZoneId.of(“Europe/Berlin”);
ZoneId zoneIdOffset = ZoneId.offOffset(“GMT”, ZoneOffset.ofHours(“+3”));

LocalDateTime now = LocalDateTime.now();
ZonedDateTime berlin = ZonedDateTime.of(now, zoneIdBerlin);
ZonedDateTime plus3 = berlin.withZoneSameInstant(zonedIdOffset);

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

Use the Period class to print the amount of years, months and days that two dates are apart.

A

LocalDate d1 = LocalDate.of(2015,1,1)
LocalDate d2 = LocalDate.of(2017,2,15)

Period period = Period.between(d1,d2);
System.out.println(period.getYears()); // 2
System.out.println(period.getMonths()); // 1
System.out.println(period.getDays()); // 14

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

Use the ChronoUnit class to calculate the total amount of days between two dates.

A

LocalDate d1 = LocalDate.of(2017, 1, 1);
LocalDate d2 = LocalDate.of(2017,2,1);
ChronoUnit.DAYS.between(d1, d2);

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

How can you create Instant of the current time?

A

Instant.now();

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

What is the result of the following code:
Instant i1 = Instant.now();
Thread.sleep(1000);
Instant i2 = Instant.now();
System.out.println(Duration.between(i1,i2).getSeconds());
System.out.println(Duration.between(i2,i1).getSeconds());

A

1

-1

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

Given two Instant objects (i1, i2) how would one get the number of nano seconds between those Instants?

A

Duration.between(i1, i2).toNanos();

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

How to format a LocalDateTime object to the pattern “ yyyy-MM-dd”?

A

date.format(DateTimeFormatter.ofPattern(“yyyy-MM-dd”));

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

How to format a LocalDateTime object to the Short date pattern, with the nl-NL locale?

A
DateTimeFormmater f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(new Locale("nl-NL");
date.format(f);
17
Q

How to format a LocalDateTime using the predefined BASIC_ISO_DATE format?

A

date.format(DateTimeFormatter.BASIC_ISO_DATE);

18
Q

How to parse the date string 2017/05/01 to a LocalDate object?

A

LocalDate d = LocalDate.parse(“2017-05-01”, DateTimeFormatter.ofPattern(“yyyy/MM/dd”));

19
Q

How to parse the date string 2017/05/01 to a LocalDate object?

A
LocalDate d = LocalDate.parse("2017-05-01");
// No pattern needed because it's de default ISO pattern.
20
Q

How to get the EPOCH from a instant?

A

Instant.EPOCH; // 0 (1970-1-1 00:00:00)

21
Q

How to create a Instant object of 10 seconds after EPOCH?

A

Instant.ofEpochSecond(10);

22
Q

How can create a Instant object based on a certain timezone?

A

Clock clock = Clock.system(ZoneId.of(“Europe/Berlin”));

Instant i = Instant.now(clock);

23
Q

Given a Instant add 2 hours.

A

Instant i = Instant.now();
Duration d = Duration.ofHours(2);
Instant i2 = i1.plus(d);

24
Q

What is the difference between Duration and Period?

A

Duration should be used for machine timings (using the Instant object). Period should be used for detemining date (or human) timings (use LocalDate, LocalDateTime etc.). It will take in account for example that a month can have 31, 30 or even less days.

25
Q

Using the ChronoUnit enumeration, add 2 weeks to the LocalDate object.

A

LocalDate d = LocalDate.now();

d.plus(2, ChronoUnit.WEEKS);

26
Q

Given: Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. As a result, 2 AM becomes 1 AM. What will the following code print ?
LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of(“US/Eastern”));
date = date.plus(Duration.ofDays(1)); System.out.println(date);

date = ZonedDateTime.of(ld, ZoneId.of(“US/Eastern”)); date = date.plus(Period.ofDays(1)); System.out.println(date);

A

2015-11-01T09:00-05:00[US/Eastern]
2015-11-01T10:00-05:00[US/Eastern]

Important thing to remember here is that Period is used to manipulate dates in terms of days, months, and years, while Duration is used to manipulate dates in terms of hours, minutes, and seconds. Therefore, Period doesn’t mess with the time component of the date while Duration may change the time component if the date is close to the DST boundary.

27
Q

What is the result of the following:
LocalDateTime greatDay = LocalDateTime.parse(“2015-01-01”);
String greatDayStr = greatDay.format(DateTimeFormatter.ISO_DATE_TIME); System.out.println(greatDayStr);

A

It will throw a DateTimeException because it doesn’t have time component.

28
Q

What is the result of the following?
LocalDate d1 = LocalDate.parse(“2015-02-05”, DateTimeFormatter.ISO_DATE);//T17:13:50”);
LocalDate d2 = LocalDate.of(2015, 2, 5);
LocalDate d3 = LocalDate.now();
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);

A

2015-02-05
2015-02-05
2015-02-05

29
Q

What is the result of the following?
Period p = Period.between(LocalDate.now(), LocalDate.of(2015, Month.SEPTEMBER, 1)); System.out.println(p); Duration d = Duration.between(LocalDateTime.now(), LocalDateTime.of(2015, Month.SEPTEMBER, 2, 10, 10)); System.out.println(d);

A

P-1D
PT9H10M
Note that if the second date is before the first date, a minus sign is included in the output.

30
Q

What is the result of the following?

System.out.println(LocalDate.of(2015, Month.JANUARY, 31).format(DateTimeFormatter.ISO_DATE_TIME));

A

Exception at run time.

Observe that you are creating a LocalDate and not a LocalDateTime. LocalDate doesn’t have time component and therefore, you cannot format it with a formatter that expects time component such as DateTimeFormatter.ISO_DATE_TIME.

31
Q

What is the result of the following?
Period p = Period.between(LocalDate.now(),
LocalDate.of(2015, Month.SEPTEMBER, 1)); System.out.println(p);
Duration d = Duration.between(LocalDate.now(), LocalDate.of(2015, Month.SEPTEMBER, 1)); System.out.println(d);

A

It will throw an exception at run time.

The call to Duration.between will throw java.time.temporal.UnsupportedTemporalTypeException because LocalDate.now() does not have a time component, while Duration.between method needs Temporal arguments that have a time component.