TimeFormat 始终来自特定时区
TimeFormat always from specific timezone
我在创建应用程序时遇到日期格式问题,我无法从当前区域设置格式化时间,但总是从特定区域设置 UTC+1 或特定状态设置时间格式,但我不知道如何设置。
SimpleDateFormat("d.M.yyyy HH:mm", Locale.getDefault()).format(Date(date))
我需要设置语言环境或时区,如常量,不依赖于物理位置或 phone 设置。
我的数据始终采用 UTC-0,但我需要将其转换为 UTC+1(或其他)并显示给用户。
感谢您的帮助
对于时间同步,我使用 TrueTime 库
这是您要找的吗?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC+1"));
这是一个 java.time
示例,它使用从某个时刻创建的 ZonedDateTime
,即上述包中的 Instant
:
public static void main(String[] args) {
// get a representation of a moment in time (not a specific date or time)
Instant now = Instant.now();
// then use that in order to represent it in a specific zone using an offset of -1 hour
ZonedDateTime utcZdt = ZonedDateTime.ofInstant(now, ZoneOffset.ofHours(-1));
// and use it again in order to have another one defined by a specific time zone
ZonedDateTime laZdt = ZonedDateTime.ofInstant(now, ZoneId.of("America/Los_Angeles"));
// and print the representation as String
System.out.println(utcZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println(laZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
输出为
2020-02-18T14:31:21.714-01:00
2020-02-18T07:31:21.714-08:00[America/Los_Angeles]
您也可以使用同一包的 OffsetDateTime
。
关键是使用从纪元派生的Instant
。这些毫秒值也是时间时刻,独立于区域或偏移量。
您正在编写 Android 应用程序,因此您可能必须使用 ThreeTenABP,API 级别以下几乎所有 java.time
功能的向后移植 Android 26.
我认为,如今,使用 java.time
或它的后向移植是解决像您这样的任务最简单、最直接的方法。
我在创建应用程序时遇到日期格式问题,我无法从当前区域设置格式化时间,但总是从特定区域设置 UTC+1 或特定状态设置时间格式,但我不知道如何设置。
SimpleDateFormat("d.M.yyyy HH:mm", Locale.getDefault()).format(Date(date))
我需要设置语言环境或时区,如常量,不依赖于物理位置或 phone 设置。
我的数据始终采用 UTC-0,但我需要将其转换为 UTC+1(或其他)并显示给用户。
感谢您的帮助
对于时间同步,我使用 TrueTime 库
这是您要找的吗?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC+1"));
这是一个 java.time
示例,它使用从某个时刻创建的 ZonedDateTime
,即上述包中的 Instant
:
public static void main(String[] args) {
// get a representation of a moment in time (not a specific date or time)
Instant now = Instant.now();
// then use that in order to represent it in a specific zone using an offset of -1 hour
ZonedDateTime utcZdt = ZonedDateTime.ofInstant(now, ZoneOffset.ofHours(-1));
// and use it again in order to have another one defined by a specific time zone
ZonedDateTime laZdt = ZonedDateTime.ofInstant(now, ZoneId.of("America/Los_Angeles"));
// and print the representation as String
System.out.println(utcZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println(laZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
输出为
2020-02-18T14:31:21.714-01:00
2020-02-18T07:31:21.714-08:00[America/Los_Angeles]
您也可以使用同一包的 OffsetDateTime
。
关键是使用从纪元派生的Instant
。这些毫秒值也是时间时刻,独立于区域或偏移量。
您正在编写 Android 应用程序,因此您可能必须使用 ThreeTenABP,API 级别以下几乎所有 java.time
功能的向后移植 Android 26.
我认为,如今,使用 java.time
或它的后向移植是解决像您这样的任务最简单、最直接的方法。