Java Reference
In-Depth Information
LocalDate: 2012-05-31
LocalDateTime: 2012-05-31T16:30:12
OffsetDateTime: 2012-05-31T16:30:12-05:00
Text '2012-05-31Hello' could not be parsed, unparsed text found at index 10
Legacy Datetime Classes
I'll refer to the datetime-related classes that were available before Java 8 as legacy datetime classes. The main legacy
classes are
Date
,
Calendar
, and
GregorianCalendar
. They are in the
java.util
package. Please refer to the section
“Interoperability with Legacy Datetime Classes” for how to convert
Date
and
Calendar
objects to datetime objects of
the new Date-Time API and vice versa.
The Date Class
An object of the
Date
class represents an instant in time. A
Date
object stores the number of milliseconds elapsed
since the epoch, midnight January 1, 1970 UTC.
■
the
Date
class in the legacy Date-time api is similar to the instant class in the new Date-time api. they have
the precision of millisecond and nanosecond, respectively.
Tip
Most of the constructors and methods of the
Date
class have been deprecated since JDK1.1. The default
constructor of the
Date
class is used to create a
Date
object with the current system datetime. Listing 12-27 illustrates
the use of the
Date
class. You may get a different output because it prints the current date and time.
Listing 12-27.
Using the Date Class
// CurrentLegacyDate.java
package com.jdojo.datetime;
import java.util.Date;
public class CurrentLegacyDate {
public static void main (String[] args) {
// Create a new Date object
Date currentDate = new Date();
System.out.println("Current date: " + currentDate);
// Get the milliseconds value of the current date
long millis = currentDate.getTime();
System.out.println("Current datetime in millis: " + millis);
}
}
Current date: Sat Jan 11 11:19:55 CST 2014
Current datetime in millis: 1389460795979