在Java中,可以使用java.util.Datejava.sql.Timestamp类来将时间戳转换为日期和时间。下面是一个将时间戳转换为java.sql.Timestamp对象的例子:

long timestamp = 1621785600000L; // 以毫秒为单位的时间戳

Timestamp datetime = new Timestamp(timestamp);

要将java.sql.Timestamp对象转换为java.util.Date对象,可以使用以下方法:

Date date = new Date(datetime.getTime());

请注意,在Java 8及以上版本中,推荐使用java.time包中的新日期和时间API来处理日期和时间。下面是一个将时间戳转换为java.time.LocalDateTime对象的例子:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

long timestamp = 1621785600000L; // 以毫秒为单位的时间戳

Instant instant = Instant.ofEpochMilli(timestamp);
LocalDateTime datetime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

要将java.time.LocalDateTime对象转换为java.util.Date对象,可以使用以下方法:

Date date = Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant());

请注意,在使用新的java.time日期和时间API时,建议使用java.time.Instant类来表示时间戳,因为它更加精确和易于处理

java时间戳怎么转datetime6

原文地址: http://www.cveoy.top/t/topic/iFjd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录