Java中时间戳与UTC时间的转换可以使用java.time
包中的类来实现。下面是一些常见的技巧:
Instant instant = Instant.ofEpochMilli(timestamp);
ZonedDateTime utcTime = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println("UTC时间:" + utcTime);
ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
Instant instant = utcTime.toInstant();
long timestamp = instant.toEpochMilli();
System.out.println("时间戳:" + timestamp);
ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedUtcTime = utcTime.format(formatter);
System.out.println("格式化的UTC时间:" + formattedUtcTime);
通过这些技巧,可以方便地在Java中进行时间戳与UTC时间之间的转换。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c++中unix时间戳与GMT/UTC时间的关系是什么