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.