Dates and Times API Flashcards

1
Q

In which package is Dates and Times classes?§

A

Package java.time

To use Date Time API you have to import java.time.*

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

Which class contains just a date—no time and no time zone?

A

LocalDate - A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

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

Which class contains just a time—no date and no time zone?

A

LocalTime - A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.

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

Which class contains both a date and time but no time zone?

A

LocalDateTime - A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

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

How to create just a date with no time?

A

LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);

LocalDate date2 = LocalDate.of(2015, 1, 20);

Both pass in the year, month, and date. You can pass the int number of the month directly. January starts at 1.. December 12.

The method signatures are as follows:

public static LocalDate of(int year, int month, int dayOfMonth)

public static LocalDate of(int year, Month month, int dayOfMonth)

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

How do you create a time with java time

A

When creating a time, you can choose how detailed you want to be. You can specify just the hour and minute, or you can add the number of seconds, or optional nanoseconds.

LocalTime time1 = LocalTime.of(6, 15); // hour and minute
LocalTime time2 = LocalTime.of(6, 15, 30); // + seconds
LocalTime time3 = LocalTime.of(6, 15, 30, 200); // + nanos

The method signatures are as follows:

public static LocalTime of(int hour, int minute)
public static LocalTime of(int hour, int minute, int second)
public static LocalTime of(int hour, int minute, int second, int nanos)

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

How do we create LocalDate with Time?

A

We can pass the 2 objaects as arguments, or just the necessary strings.

LocalTime time1 = LocalTime.of(6, 15)
LocalDate date2 = LocalDate.of(2015, 1, 20);

LocalDateTime dateTime2 = LocalDateTime.of(date1, time1);

LocalDateTime dateTime1 = LocalDateTime.of(2015, Month.JANUARY, 20, 6, 15, 30);

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

Does LocalDate class have public constructors?
ex:
Would this work?

LocalDate d = new LocalDate()? 
with or without parameters?
A
The date and time classes have private constructors to force you to use the static methods. 
The exam creators may try to throw something like this at you:
LocalDate d = new LocalDate(); // DOES NOT COMPILE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Another trick in the exam is to see what happens when you pass invalid numbers to of()…

A

LocalDate.of(2015, Month.JANUARY, 32) // throws DateTimeException

java.time.DateTimeException:
Invalid value for DayOfMonth (valid values 1 - 28/31): 32

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

Manipulating Dates and Times

A

Manipulating Dates and Times

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

Which classes of Date and time API are covered in the exam?

A
LocalDate
LocalTime
LocalDateTime
Period
DateTimeFormatter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which format use Date an time API?

A

java.time package
This is the main package of the date/time framework.
The classes defined in this package are based on the ISO calendar system.

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

Are Date and Time API classes mutable?

Are Date and Time API classes thread-safe?

A

All classes in this package are immutable and thread-safe.

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

What represents a date-based amount of time in terms of Days, Months, and Years and it has no notion of hours, minutes, and seconds.

A

Period:
Period represents a date-based amount of time in terms of Days, Months, and Years. It has no notion of hours, minutes, and seconds.

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

What represents time-based amount of time in terms of Hours, Minutes, and Seconds and it has no notion of days.

A

Duration:

Duration represents time-based amount of time in terms of Hours, Minutes, and Seconds. It has no notion of days.

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

Which Enums does the time package contains?

A

This package also contains two enums -
Month and DayOfWeek.
Month contains values from JANUARY to DECEMBER.
DayOfWeek contains values from MONDAY to SUNDAY.

17
Q

java.time.format package

A

java.time.format package

This package contains helper classes for converting date/time objects to strings and for parsing strings into date/time classes.
- DateTimeFormatter
- FormatStyle
are the most used classes of this package.

18
Q

java.time.temporal package

A

This package contains the base interfaces such as:
Temporal,
TemporalAmount,
TemporalUnit,
TemporalField
TemporalAccessor of the Date/Time framework.
These interfaces are implemented by various classes of the java.time package.

19
Q

Date/Time API : which operations is possible there are only four types of operations that you can do with these classes:

A

The API is actually quite easy to understand once you realize that there are only four types of operations that you can do with these classes:

creating new objects,
converting them to strings,
comparing,
reading the state of the objects.

There are no methods for updating because the objects of these classes are immutable.

20
Q

Does these classes have constructors?

A

Instead of having constructors, these classes have static factory methods named now, of, and from.
All three methods are public.

If you see something like new LocalDate() anywhere in the code, go straight for the option that says, “compilation fails”!

21
Q

What are the no-args versions of the methods that return the current LocalDate, LocalTime, LocalDateTime?

A

The now method
The now method returns an instance of the object that represents the current date or time. For example, LocalDate.now(); returns a LocalDate object set to today’s date, LocalTime.now(); returns a LocalTime object set to current time, and LocalDateTime.now(); returns a Local- DateTime object set to today’s date and current time.

22
Q

Does Period Class have a now method?

A

Since Period denotes an amount of time and not a point in time, it doesn’t have the now() method.
Period has similar of methods.

23
Q

What points should you remember about the of method?

A

There are three points that you should remember about the of methods:

1. Indexing for month parameter starts from 1. In other words, to specify January, you must
pass 1 (or use the enum value Month.JANUARY).
  1. They use a 24 hour clock for the hour parameter. Thus, to specify 11 AM, you need to
    pass 11 and to specify 11 PM, you need to pass 23.
  2. They throw java.time.DateTimeException
    (which extends java.lang.RuntimeException and is, therefore, an unchecked exception),
    if the value passed for any field is out of range.

Will also throw a DateTimeException because the range of the hour parameter is only 0 to 23.

24
Q

Period class of methods signatures

A
static Period of(int years, int months, int days)
static Period ofDays(int days)
static Period ofMonths(int months)
static Period ofWeeks(int weeks)
static Period ofYears(int years)

Here is an example that shows two ways to create a Period with 10 days:

Period tenDays = Period.ofDays(10);
tenDays = Period.of(0, 0, 10);

25
Q

Given that Daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone, 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);

Note: This question refers to ZonedDateTime and Duration, which are not explicitly mentioned in the objectives.
However, a few candidates have reported getting a similar question and so we have included it in this question bank.

A

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.

Durations and periods differ in their treatment of daylight savings time 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).