Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.
Copyright © Cay S. Horstmann 2016
Instant
is a point on the time line.Instant.MIN
is a billion years before January 1, 1970, and Instant.MAX
is a billion years later.Instant.now()
yields the current instant.Duration
is the difference between two instants.Instant start = Instant.now(); runAlgorithm(); Instant end = Instant.now(); Duration timeElapsed = Duration.between(start, end); long millis = timeElapsed.toMillis();
Instant
by a Duration
:
Instant start = Instant.now(); Instant later = start.plus(Duration.ofHours(8));
Instant
and Duration
are immutable.
plus
method returns a new Instant
object.boolean overTenTimesFaster = timeElapsed.multipliedBy(10).minus(timeElapsed2).isNegative();
boolean overTenTimesFaster = timeElapsed.toNanos() * 10 < timeElapsed2.toNanos()
LocalDate
is a calendar date without a time zone.
LocalDate
with one of the factory methods:
LocalDate today = LocalDate.now(); // Today’s date LocalDate alonzosBirthday = LocalDate.of(1903, Month.JUNE, 14);
plusDays
, plusWeeks
, plusMonths
, plusYears
(and minus
Xxx):
LocalDate programmersDay = LocalDate.of(2016, 1, 1).plusDays(255);
int days = independenceDay.until(christmas, ChronoUnit.DAYS); // 174 days
plusMonths
can yield implausible results.
LocalDate.of(2016, 1, 31).plusMonths(1)
yields February 29, 2016.import java.time.* LocalDate today = LocalDate.now() today.plusDays(100) today.plusDays(1000) LocalDate birthday = LocalDate.of(1961, 8, 4) import java.time.temporal.* birthday.until(today, ChronoUnit.DAYS)
getDayOfWeek
method yields the weekday of a LocalDate
:
DayOfWeek weekday = LocalDate.of(1900, Month.JANUARY, 1).getDayOfWeek();
DayOfWeek.MONDAY
has numerical value 1, and DayOfWeek.SUNDAY
has value 7:
int value = weekday.getValue();
DayOfWeek
can do arithmetic modulo 7:
DayOfWeek.SATURDAY.plus(3)
yields
DayOfWeek.TUESDAY
TemporalAdjusters
class provides a number of static
methods for common adjustments.LocalDate firstTuesday = LocalDate.of(year, month, 1).with( TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
TemporalAdjuster NEXT_WORKDAY = TemporalAdjusters.ofDateAdjuster(w -> { LocalDate result = w; do { result = result.plusDays(1); } while (result.getDayOfWeek().getValue() >= 6); return result; });
next
or nextOrSame
method:
LocalDate firstWorkday = LocalDate.of(year, 1, 1).with(NEXT_WORKDAY);
LocalTime
represents a time of day, such as 22:30:00LocalTime rightNow = LocalTime.now(); LocalTime bedtime = LocalTime.of(22, 30, 0);
LocalTime wakeup = bedtime.plusHours(8);
LocalTime
doesn't concern itself with AM/PM—that is left to formatters.LocalDateTime
for a timezone-independent date and time.ZonedDateTime
class handles the messiness.ZoneId.getAvailableIds()
lists them all.ZonedDateTime
like this:
ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime apollo11launch = ZonedDateTime.of(1969, 7, 16, 9, 32, 0, 0, ZoneId.of("America/New_York"));
ZonedDateTime
is a specific point in time. Can convert to/from Instant
:
Instant launchInstant = apollo11launch.toInstant(); ZonedDateTime launchInUTC = instant.atZone(ZoneId.of("UTC"));
ZonedDateTime
handles the rules, as published in the volunteer-maintained “tz database”ZonedDateTime skipped = ZonedDateTime.of( 2013, 3, 31, 2, 30, 0, 0, ZoneId.of("Europe/Berlin")); // Constructs March 31 3:30—Central Europe switched to DST at 2:00
ZonedDateTime nextMeeting = meeting.plus(Duration.ofDays(7)); // Caution! Won’t work with daylight savings time
Period
class:
ZonedDateTime nextMeeting = meeting.plus(Period.ofDays(7)); // OK
ZonedDateTime.now() ZonedDateTime.now(ZoneId.of("UTC")) ZonedDateTime.now(ZoneId.of("Europe/Rome")) ZonedDateTime.now().plus(Duration.ofDays(90)) ZonedDateTime.now().plus(Period.ofDays(90))
java.time
is the third attempt at a date/time library for Java.java.util.Date
class.
java.time.Instant
class.Date
has mutators and deprecated methods for the Gregorian calendar. java.util.Calendar
class and a subclass GregorianCalendar
.
java.time.ZonedDateTime
class.Calendar
has mutators and a cumbersome API.java.util.Date
and Instant
with the static method Date.from(Instant)
and the method Date.toInstant()
.GregorianCalendar
and ZonedDateTime
with the static method GregorianCalendar.from(ZonedDateTime)
and the method GregorianCalendar.toZonedDateTime()
.java.sql
package has Date
, Time
, and Timestamp
classes that are equivalent to the LocalDate
, LocalTime
, and Instant
classes.
valueOf
methods and the methods Date.toLocalDate
, Time.toLocalTime
for conversions between SQLDate
/Time
and LocalDate
/LocalTime
.Timestamp.from
method and the Timestamp.toInstant
methods to convert between the Timestamp
and Instant
classes.