gpt4 book ai didi

java - 使用时区信息转换日期时间(以毫秒为单位)

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:54 26 4
gpt4 key购买 nike

我有一个简单的问题。如何实现这种格式的日期1438117140000+0300。第一部分1438117140000它的时间以毫秒为单位,我转换没有问题,第二部分时区信息是我的头痛,如何获取它?

最佳答案

您可以使用String.format来实现此目的:

Date now = new Date();
System.out.println(String.format("%tQ%tz", now, now));

执行刚刚打印的代码:

1438635740416+0300

日期/时间的转换在documentation中指定。 。在这里,我使用了以下两个转换字符:

'Q': Milliseconds since the beginning of the epoch starting at 1January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.

'z': RFC 822 style numeric time zone offset from GMT, e.g. -0800. Thisvalue will be adjusted as necessary for Daylight Saving Time. Forlong, Long, and Date the time zone used is the default time zone forthis instance of the Java virtual machine.

关于java - 使用时区信息转换日期时间(以毫秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31796427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com