How can I convert a timestamp to a date in Java?
In Java, you can convert Timestamp and Date between java.util.Date and java.sql.Timestamp classes.
- Time indication
- – Appointment
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(timestamp.getTime());
- – Appointment
- Time stamp
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Note: java.sql.Timestamp is a subclass of java.util.Date, so you can directly create a Date object using Timestamp. The precision of the time in a Date object is only up to the second level, while Timestamp can go down to the nanosecond level.