【发布时间】:2020-09-08 02:52:10
【问题描述】:
如何像 DD-mm-yyyy 一样从毫秒转换为日期?我的数据库中有一个包含公共假期日期的列,它的类型是日期。在我的实体和 DTO 类中,我的列是日期类型。当我尝试从数据库中检索表时,日期以毫秒为单位,如下所示:
{
"date" : 1577916000000,
"id" :1,
"description" : New Years Eve
}
【问题讨论】:
-
不要使用
Date和SimpleDateFormat,它们已经过时了。使用java.time包中的类。 -
在您的情况下,您可以使用
Instant::ofEpochMilli。然后您可以使用atOffset或atZone将其分别转换为OffsetDateTime或ZonedDateTime。为了格式化,使用DateTimeFormatter类来创建格式。更多详情请查看the documentation of theDateTimeFormatterclass。
标签: java date simpledateformat