解析时间戳为日期字符串

huangapple go评论74阅读模式
英文:

Parse timestamp to this date string

问题

如何将此时间戳解析为字符串类型的日期,格式为 2020-10-26T15:21:47.758+01:00

英文:

How can I parse this timestamp

1594361788215

to data in String type in this format 2020-10-26T15:21:47.758+01:00

答案1

得分: 2

你可以使用java.time库将一个时代时间戳轻松地格式化为字符串。

首先,您需要将您拥有的时代时间戳转换为ZonedDateTime

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
    Instant.ofEpochMilli(1594361788215L), 
    ZoneId.of("Europe/London"));

而您描述的格式对应于ISO_OFFSET_DATE_TIME

String formatted = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
英文:

You can easily format an epoch timestamp to a string with the library java.time.

First, you'll have to convert the epoch timestamp you have into a ZonedDateTime:

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
    Instant.ofEpochMilli(1594361788215L), 
    ZoneId.of("Europe/London"));

And the format you described corresponds to ISO_OFFSET_DATE_TIME

String formatted = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

huangapple
  • 本文由 发表于 2020年10月26日 23:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64539718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定