Java Reference
In-Depth Information
Interoperability with Legacy Datetime Classes
The legacy datetime classes have been around for over 18 years. The new Date-Time API is not going to replace them
overnight. As a Java developer, you will be tasked to maintain applications that use the legacy classes. Java 8 provides
interoperability between the legacy classes and the new Date-Time API. New methods have been added to the legacy
classes to convert their objects to the new Date-Time classes and vice versa. Interoperability of the following legacy
classes will be discussed in this section:
java.util.Date
java.util.Calendar
java.util.GregorianCalendar
java.util.TimeZone
java.sql.Date
java.sql.Time
java.sql.Timestamp
java.nio.file.attribute.FileTime
Table 12-7 contains the list of legacy datetime classes and their new Date-Time counterparts. All legacy classes,
except the Calendar class, provides two-way conversion. The toXxx() methods are instance methods. They return an
object of the new datetime class. The other methods are static methods, which accept an object of the new datetime
class and return an object of the legacy class. For example, the from() method in the java.util.Date class is a static
method, which takes an Instant argument and returns a java.util.Date . The toInstant() method is an instance
method and converts a java.util.Date into an Instant .
Table 12-7. Conversion Between New Datetime and Legacy Datetime Classes
Legacy Class
New Methods in Legacy Class
Equivalent New Datetime Class
java.util.Date
from(), toInstant()
Instant
Calendar
toInstant()
None
GregorianCalendar
from(), toZonedDateTime()
ZonedDateTime
TimeZone
getTimeZone(), toZoneId()
ZoneId
java.sql.Date
valueOf(), toLocalDate()
LocalDate
Time
valueOf(), toLocalTime()
LocalTime
Timestamp
from(), toInstant()
Instant
valueOf(), toLocalDateTime()
LocalDateTime
FileTime
from(), toInstant()
Instant
Listing 12-29 shows how to convert a Date to an Instant and vice versa.
 
 
Search WWH ::




Custom Search