Java Reference
In-Depth Information
Current Datetime: 2014-01-03T22:47:47.375
4 days ago: 2013-12-30T22:47:47.375
4 days ago: 2013-12-30T22:47:47.375
4 hours after: 2014-01-04T02:47:47.375
4 hours after: 2014-01-04T02:47:47.375
Local Date, Time, and Datetime
An instance of the
LocalDate
class represents a date without a time or time zone. Several methods in the class let
you convert a
LocalDate
to other datetime objects and manipulate its fields (year, month, and day) to obtain another
LocalDate
. The following snippet of code creates some
LocalDate
objects:
// Get the current local date
LocalDate ldt1 = LocalDate.now();
// Create a local date May 10, 2012
LocalDate ldt2 = LocalDate.of(2012, Month.MAY, 10);
// Create a local date, which is 10 days after the epoch date 1970-01-01
LocalDate ldt3 = LocalDate.ofEpochDay(10); // 1970-01-11
The
LocalDate
class contains two constants,
MAX
and
MIN
, that are the maximum and minimum supported
LocalDate re
spectively. The value for
LocalDate.MAX
is +999999999-12-31 and
LocalDate.MIN is -999999999-01-01.
An instance of the
LocalTime
class represents a time without a date or time zone. Time is represented to a
nanosecond precision. It contains
MIN, MAX
,
MIDNIGHT
, and
NOON
constants that represent time constants of 00:00,
23:59:59.999999999, 00:00, and 12:00, respectively. Several methods in this class let you create, manipulate, and
compare times in different ways. The following snippet of code creates some
LocalTime
objects:
// Get the current local time
LocalTime lt1 = LocalTime.now();
// Create a local time 07:30
LocalTime lt2 = LocalTime.of(7, 30);
// Create a local time 07:30:50
LocalTime lt3 = LocalTime.of(7, 30, 50);
// Create a local time 07:30:50.000005678
LocalTime lt4 = LocalTime.of(7, 30, 50, 5678);
An instance of the
LocalDateTime
class represents a date and a time without a time zone. It provides several
methods to create, manipulate, and compare datetimes. You can think of a
LocalDateTime
as a combination of
LocalDate
and
LocalTime
.
LocalDateTime = LocalDate + LocalTime