Dates and Times API Flashcards
In which package is Dates and Times classes?§
Package java.time
To use Date Time API you have to import java.time.*
Which class contains just a date—no time and no time zone?
LocalDate - A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
Which class contains just a time—no date and no time zone?
LocalTime - A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.
Which class contains both a date and time but no time zone?
LocalDateTime - A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
How to create just a date with no time?
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 do you create a time with java time
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 do we create LocalDate with Time?
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);
Does LocalDate class have public constructors?
ex:
Would this work?
LocalDate d = new LocalDate()? with or without parameters?
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
Another trick in the exam is to see what happens when you pass invalid numbers to of()…
LocalDate.of(2015, Month.JANUARY, 32) // throws DateTimeException
java.time.DateTimeException:
Invalid value for DayOfMonth (valid values 1 - 28/31): 32
Manipulating Dates and Times
Manipulating Dates and Times
Which classes of Date and time API are covered in the exam?
LocalDate LocalTime LocalDateTime Period DateTimeFormatter
Which format use Date an time API?
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.
Are Date and Time API classes mutable?
Are Date and Time API classes thread-safe?
All classes in this package are immutable and thread-safe.
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.
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.
What represents time-based amount of time in terms of Hours, Minutes, and Seconds and it has no notion of days.
Duration:
Duration represents time-based amount of time in terms of Hours, Minutes, and Seconds. It has no notion of days.
Which Enums does the time package contains?
This package also contains two enums -
Month and DayOfWeek.
Month contains values from JANUARY to DECEMBER.
DayOfWeek contains values from MONDAY to SUNDAY.
java.time.format package
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.
java.time.temporal package
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.
Date/Time API : which operations is possible there are only four types of operations that you can do with these classes:
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.
Does these classes have constructors?
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”!
What are the no-args versions of the methods that return the current LocalDate, LocalTime, LocalDateTime?
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.
Does Period Class have a now method?
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.
What points should you remember about the of method?
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).
- 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. - 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.
Period class of methods signatures
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);
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.
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).