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.

  1. Time indication
  2. – Appointment
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(timestamp.getTime());
  1. – Appointment
  2. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds