Java Reference
In-Depth Information
public
public class
class
LegacyDates
LegacyDates
{
public
public static
static
void
void
main
(
String
[]
args
) {
// There and back again, via Date
Date legacyDate
=
new
new
Date
();
System
.
out
.
println
(
legacyDate
);
LocalDateTime newDate
=
LocalDateTime
.
ofInstant
(
legacyDate
.
toInstant
(),
ZoneId
.
systemDefault
());
System
.
out
.
println
(
newDate
);
// And via Calendar
Calendar c
=
Calendar
.
getInstance
();
System
.
out
.
println
(
c
);
LocalDateTime newCal
=
LocalDateTime
.
ofInstant
(
c
.
toInstant
(),
ZoneId
.
systemDefault
());
System
.
out
.
println
(
newCal
);
}
}
Of course you do not have to use these legacy converters; you are free to write your own.
The file
LegacyDatesDIY.java
in the
javasrc
repository explores this option in the unlikely
event you wish to pursue it.
Given the amount of code written before Java 8, it is likely that the legacy
Date
and
Calen-
dar
will be around until the end of Java time.
The new Date/Time API has many capabilities that we have not explored. Almost enough for