DateAndTime API Flashcards
TemporalAdjusters
print the date that represents upcoming tuesday from now even if the current day is a tuesday
–>System.out.println(LocalDate.now()
.with(TemporalAdjusters.next(DayOfWeek.TUESDAY)));
or
–>
System.out.println(TemporalAdjusters.next(DayOfWeek.TUESDAY)
.adjustInto(LocalDate.now()));
The JavaDoc description of java.time.temporal.TemporalAdjusters is very helpful:
Adjusters are a key tool for modifying temporal objects. They exist to externalize the process of adjustment, permitting different approaches, as per the strategy design pattern. Examples might be an adjuster that sets the date avoiding weekends, or one that sets the date to the last day of the month.
There are two equivalent ways of using a TemporalAdjuster. The first is to invoke the method on the interface directly. The second is to use Temporal.with(TemporalAdjuster):
// these two lines are equivalent, but the second approach is recommended
temporal = thisAdjuster.adjustInto(temporal);
temporal = temporal.with(thisAdjuster);
It is recommended to use the second approach, with(TemporalAdjuster), as it is a lot clearer to read in code.
This class contains a standard set of adjusters, available as static methods. These include:
finding the first or last day of the month
finding the first day of next month
finding the first or last day of the year
finding the first day of next year
finding the first or last day-of-week within a month, such as “first Wednesday in June”
finding the next or previous day-of-week, such as “next Thursday”
LocalDate
You cannot create a LocalDate object using its constructor because it is private.
*is an immutable date-time object that represents a date, it does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays
Package java.time
java.time
The core of the API for representing date and time. It includes classes for date, time, date and time combined, time zones, instants, duration, and clocks. These classes are based on the calendar system defined in ISO-8601, and are immutable and thread-safe.
java.time.chrono
The API for representing calendar systems other than the default ISO-8601. You can also define your own calendar system. This tutorial does not cover this package in any detail.
java.time.format
Classes for formatting and parsing dates and times.
java.time.temporal
Extended API, primarily for framework and library writers, allowing interoperations between the date and time classes, querying, and adjustment. Fields (TemporalField and ChronoField) and units (TemporalUnit and ChronoUnit) are defined in this package.
java.time.zone
Classes that support time zones, offsets from time zones, and time zone rules. If working with time zones, most developers will need to use only ZonedDateTime, and ZoneId or ZoneOffset.
Most of these calsses are immutable and do not have any public constructors. You need to use their static factory methods to get their instances.
For example:
java.time.LocalDate d1 = java.time.LocalDate.of(2015, Month.JANUARY, 31);
java.time.LocalDateTime d3 = \
java.time.LocalDateTime.parse(“2015-01-02T17:13:50”);
//Note that this
//will throw a java.time.format.DateTimeParseException if the input string
//lacks the time component i.e.T17:13:50
Period and Duration
A Duration measures an amount of time using time-based values (seconds, nanoseconds).
–>A Duration is most suitable in situations that measure machine-based time, such as code that uses an Instant object. A Duration object is measured in seconds or nanoseconds and does not use date-based constructs such as years, months, and days, though the class provides methods that convert to days, hours, and minutes. A Duration can have a negative value, if it is created with an end point that occurs before the start point.
–>A Duration is not connected to the timeline, in that it does not track time zones or daylight saving time. Adding a Duration equivalent to 1 day to a ZonedDateTime results in exactly 24 hours being added, regardless of daylight saving time or other time differences that might result.
A Period uses date-based values
–>The total period of time is represented by all three units together: months, days, and years.
The Period class provides various get methods, such as getMonths, getDays, and getYears, so that you can extract the amount of time from the period.
To present the amount of time measured in a single unit of time, such as days, you can use the ChronoUnit.between method.
The ChronoUnit enum, discussed in the The Temporal Package, defines the units used to measure time. The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds.
EG:
import java.time.temporal.ChronoUnit;
Instant previous, current, gap;
…
current = Instant.now();
if (previous != null) {
gap = ChronoUnit.MILLIS.between(previous,current);
}
Daylight Savings Time
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).