JDK Tutorials - Herong's Tutorial Examples - v6.32, by Herong Yang
Converting java.util.Date to java.time.Instant
This section provides a tutorial example how to migrate Java code from the old java.util.Date class to the new java.time.Instant class.
If you are currently using the old java.util.Date class to manipulate time measured as Epoch milliseconds, you should migrate your code to use the new java.time.Instant class.
If you are using both classes in your code, you can use two new methods provided on java.util.Date class to convert objects between classes:
The following sample program provides some comparison between java.util.Date and java.time.Instant that might be helpful to you in migrating your code:
/* DateClassMigration.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.util.Date; import java.time.Instant; class DateClassMigration { public static void main(String[] a) { java.io.PrintStream out = System.out; Date oDate = new Date(); Instant oInstant = Instant.now(); out.println(); out.println("Epoch milliseconds:"); out.println("Date class: " + oDate.getTime()); out.println("Instant class: " + oInstant.toEpochMilli()); out.println(); out.println("Epoch seconds:"); out.println("Date class: " + (long) oDate.getTime()/1000); out.println("Instant class: " + oInstant.getEpochSecond()); out.println(); out.println("Nanoseconds-of-Second:"); out.println("Date class: Not supported"); out.println("Instant class: " + oInstant.getNano()); out.println(); out.println("String presentation:"); out.println("Date class: " + oDate.toString()); out.println("Instant class: " + oInstant.toString()); out.println(); out.println("Converting Date to Instant:"); Instant cInstant = oDate.toInstant(); out.println("Date class: " + oDate.toString()); out.println("Instant class: " + cInstant.toString()); out.println(); out.println("Converting Date from Instant:"); Date cDate = Date.from(oInstant); out.println("Date class: " + cDate.toString()); out.println("Instant class: " + oInstant.toString()); } }
When running DateClassMigration.java, I got the following output:
herong> java DateClassMigration.java Epoch milliseconds: Date class: 1708372209690 Instant class: 1708372209690 Epoch seconds: Date class: 1708372209 Instant class: 1708372209 Nanoseconds-of-Second: Date class: Not supported Instant class: 690177000 String presentation: Date class: Mon Feb 19 16:50:09 ART 2024 Instant class: 2024-02-19T19:50:09.690177Z Converting Date to Instant: Date class: Mon Feb 19 16:50:09 ART 2024 Instant class: 2024-02-19T19:50:09.690Z Converting Date from Instant: Date class: Mon Feb 19 16:50:09 ART 2024 Instant class: 2024-02-19T19:50:09.690177Z
Table of Contents
java.time.Instant - Representing a Moment of Time
java.time.Instant Usage Examples
java.time.Instant - get(INSTANT_SECONDS) Error
►Converting java.util.Date to java.time.Instant
java.time.ZonedDateTime - Calendar and Timezone
java.time.ZonedDateTime Usage Examples
Converting java.util.GregorianCalendar to java.time.ZonedDateTime
java.time.OffsetDateTime - Calendar and UTC Offset
java.time.OffsetDateTime Usage Examples
java.time.LocalDateTime - Local Date and time without Timezone
Partial Date and Time Objects and Classes
java.time.format.DateTimeFormatter - Date-Time Strings
java.time.Duration - Time Durations
java.time.Duration Usage Examples
java.time.Period - Periods in Days and Months
java.time.Period Usage Examples
Date, Time and Calendar Classes
Date and Time Object and String Conversion
Number Object and Numeric String Conversion
Locales, Localization Methods and Resource Bundles
Calling and Importing Classes Defined in Unnamed Packages
HashSet, Vector, HashMap and Collection Classes
Character Set Encoding Classes and Methods
Encoding Conversion Programs for Encoded Text Files
Datagram Network Communication
DOM (Document Object Model) - API for XML Files
DTD (Document Type Definition) - XML Validation
XSD (XML Schema Definition) - XML Validation
XSL (Extensible Stylesheet Language)
Message Digest Algorithm Implementations in JDK
Private key and Public Key Pair Generation
PKCS#8/X.509 Private/Public Encoding Standards
Digital Signature Algorithm and Sample Program
"keytool" Commands and "keystore" Files
KeyStore and Certificate Classes
Secret Key Generation and Management
Cipher - Encryption and Decryption
The SSL (Secure Socket Layer) Protocol
SSL Socket Communication Testing Programs