Java 如何解析未指定的偏移量?
我的时间是12:00:00,格式是HH:mm:ss。Java 如何解析未指定的偏移量?,java,time,timezone-offset,datetime-parsing,Java,Time,Timezone Offset,Datetime Parsing,我的时间是12:00:00,格式是HH:mm:ss。 我知道这段时间来自设置为+3偏移量的服务器。 如果我使用SimpleDateFormat df=newsimpledateformat(“HH:mm:ss”),它解析设备的时间,设备可以位于不同的时区。 除了将+3偏移量添加到原始字符串之外,还有其他方法可以解析它吗?在SimpleDataFormat对象上设置时区: SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss"); fmt.se
我知道这段时间来自设置为+3偏移量的服务器。
如果我使用
SimpleDateFormat df=newsimpledateformat(“HH:mm:ss”)代码>,它解析设备的时间,设备可以位于不同的时区。
除了将+3偏移量添加到原始字符串之外,还有其他方法可以解析它吗?在SimpleDataFormat
对象上设置时区:
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT+03:00"));
我建议您使用Java 8日期和时间API(packageJava.time
),而不是旧的API,后者是SimpleDataFormat
的一部分。在SimpleDataFormat
对象上设置时区:
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT+03:00"));
我建议您使用Java 8日期和时间API(packageJava.time
),而不是旧API,旧API的一部分是SimpleDataFormat
。使用Java 8日期时间API:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("HH:mm:ss");
LocalTime clientLocalTime = LocalTime
.parse("12:00:00", formatter)
// Create an OffsetTime object set to the server's +3 offset zone
.atOffset(ZoneOffset.ofHours(3))
// Convert the time from the server timezone to the client's local timezone.
// This expects the time value to be from the same day,
// otherwise the local timezone offset may be incorrect.
.withOffsetSameInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now()))
// Drop the timezone info - not necessary
.toLocalTime();
使用Java 8 DateTime API:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("HH:mm:ss");
LocalTime clientLocalTime = LocalTime
.parse("12:00:00", formatter)
// Create an OffsetTime object set to the server's +3 offset zone
.atOffset(ZoneOffset.ofHours(3))
// Convert the time from the server timezone to the client's local timezone.
// This expects the time value to be from the same day,
// otherwise the local timezone offset may be incorrect.
.withOffsetSameInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now()))
// Drop the timezone info - not necessary
.toLocalTime();
首先,您的服务器是否应该以UTC格式发送时间?如果客户无处不在,这似乎更为时区中立和标准化。然而,在代码中处理它的方式不会有太大的不同。在任何情况下,UTC的服务器偏移量可以是常数:
private static final ZoneOffset serverOffset = ZoneOffset.ofHours(3);
但是,在实际代码中,您可能希望以某种方式使其可配置。要分析:
OffsetTime serverTime = LocalTime.parse("12:00:00").atOffset(serverOffset);
System.out.println(serverTime);
这张照片
12:00+03:00
由于您的时间格式符合LocalTime
的默认值(ISO 8601),因此我们不需要显式格式化程序。若你们只需要用偏移量表示时间,那个么我们就完成了。如果需要转换为用户的本地时间,要可靠地转换,需要同时确定时区和日期:
LocalTime clientTime = serverTime.atDate(LocalDate.of(2018, Month.JANUARY, 25))
.atZoneSameInstant(ZoneId.of("Indian/Maldives"))
.toLocalTime();
System.out.println(clientTime);
在我们选择的日子和区域
14:00
请替换您想要的时区和日期
假设,如果您知道用户与UTC的偏移量,您可以使用:
LocalTime clientTime = serverTime.withOffsetSameInstant(ZoneOffset.of("-08:45"))
.toLocalTime();
该示例生成00:15
。然而,没有人知道政客们何时会在用户时区引入夏令时(DST)或其他异常情况,因此我不鼓励仅依靠补偿
是的,我也在使用java.time
SimpleDataFormat
不仅已经过时很久了,而且还出了名的麻烦,因此我强烈推荐使用java.time
。首先,您的服务器是否应该以UTC格式发送时间?如果客户无处不在,这似乎更为时区中立和标准化。然而,在代码中处理它的方式不会有太大的不同。在任何情况下,UTC的服务器偏移量可以是常数:
private static final ZoneOffset serverOffset = ZoneOffset.ofHours(3);
但是,在实际代码中,您可能希望以某种方式使其可配置。要分析:
OffsetTime serverTime = LocalTime.parse("12:00:00").atOffset(serverOffset);
System.out.println(serverTime);
这张照片
12:00+03:00
由于您的时间格式符合LocalTime
的默认值(ISO 8601),因此我们不需要显式格式化程序。若你们只需要用偏移量表示时间,那个么我们就完成了。如果需要转换为用户的本地时间,要可靠地转换,需要同时确定时区和日期:
LocalTime clientTime = serverTime.atDate(LocalDate.of(2018, Month.JANUARY, 25))
.atZoneSameInstant(ZoneId.of("Indian/Maldives"))
.toLocalTime();
System.out.println(clientTime);
在我们选择的日子和区域
14:00
请替换您想要的时区和日期
假设,如果您知道用户与UTC的偏移量,您可以使用:
LocalTime clientTime = serverTime.withOffsetSameInstant(ZoneOffset.of("-08:45"))
.toLocalTime();
该示例生成00:15
。然而,没有人知道政客们何时会在用户时区引入夏令时(DST)或其他异常情况,因此我不鼓励仅依靠补偿
是的,我也在使用java.time
SimpleDateFormat
不仅早已过时,而且也出了名的麻烦,所以我强烈推荐java.time
。FYI,java.time类,特别是DateTimeFormat
,已经过时多年了。你有日期吗?在没有日期上下文的情况下,将一天中的某个时间与UTC的偏移量相结合是没有意义的。这是。按照链接看一看。仅供参考,java.time类已经过时多年了,特别是DateTimeFormatter
。你有日期吗?在没有日期上下文的情况下,将一天中的某个时间与UTC的偏移量相结合是没有意义的。这是。按照链接看一看。啊,是的,谢谢。我切换了客户端/服务器时区并调整了评论。啊,是的,谢谢。我切换了客户端/服务器时区并调整了注释。