Chr*_*ght 1 java-time threetenbp threetenabp
我有以下代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.UK);
Instant inst = DateTimeUtils.toInstant(sdf.parse("2019-08-13T18:00:00Z"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm").withLocale(Locale.UK).withZone(ZoneId.of("Europe/London"));
System.out.println(formatter.format(inst));
//prints 18:00
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶,因为我认为inst这将是 GMT/UTC 时间,并且formatter会将其格式化为伦敦时间(该日期为 BST(UTC+1:00)),生成19:00.
我在这里缺少什么?
我猜这是我的代码一个通用的问题,但如果它的确与众不同,这是利用org.threeten.bp.*从类ThreeTen-反向移植项目,在另外的适应Android的早期ThreeTenABP项目。
Instant // Represent a moment in UTC.
.parse( // Generate a `Instant` object from the content of text input.
"2019-08-13T18:00:00Z" // String in standard ISO 8601 format.
) // Returns a `Instant` object.
.atZone( // Adjust from UTC to the wall-clock time used by the people of a particular region (a time zone).
ZoneId.of( "Europe/London" ) // Specify a time zone using name in proper `Continent/Region` format. Never use 2-4 letter pseudo-zones such as `BST`.
) // Returns a `ZonedDateTime` object.
.toLocalTime() // Extract the time-of-day, without a date and without a time zone or offset. Returns a `LocalTime` object.
.format( // Generate text representing the content of this `LocalTime` object.
DateTimeFormatter
.ofLocalizedTime ( FormatStyle.SHORT ) // Automatically localize while generating a `String`.
.withLocale ( Locale.UK ) // Locale determines the human language and cultural norms to use in localizing.
) // Returns a `String` object.
Run Code Online (Sandbox Code Playgroud)
19:00
您正在将可怕的遗留类 ( SimpleDateFormat, Date) 与现代java.time类混合在一起。不要那样做。仅使用java.time。
Instant = UTC 时间跳过前两行代码。您的输入字符串"2019-08-13T18:00:00Z"采用标准 ISO 8601 格式。java.time类在解析/生成字符串时默认使用这些标准格式。所以不需要指定格式模式。
String input = "2019-08-13T18:00:00Z" ;
Instant instant = Instant.parse( input ) ;
Run Code Online (Sandbox Code Playgroud)
Instant.toString(): 2019-08-13T18:00:00Z
Instant 不灵活我怀疑您的问题在于您试图在Instant. 该Instant班是内基本构建块类java.time。它仅代表 UTC 中的一个时刻。它不适用于诸如灵活生成字符串之类的事情。
更灵活的类是OffsetDateTime&ZonedDateTime类。
ZonedDateTime将 aZoneId应用于您Instant以调整到时区,渲染ZonedDateTime对象。
ZoneId z = ZoneId.of( "Europe/London" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Run Code Online (Sandbox Code Playgroud)
zdt.toString(): 2019-08-13T19:00+01:00[欧洲/伦敦]
您似乎只想专注于一天中的时间。提取一个LocalTime对象。
LocalTime lt = zdt.toLocalTime ();
Run Code Online (Sandbox Code Playgroud)
lt.toString(): 19:00
对于该日期处于夏令时 (DST) 的伦敦地区,与 UTC 的偏移量提前一小时。所以我们看到一天中的时间是晚上 7 点,而不是 UTC 的下午 6 点。
顺便说一下BST不是时区。我建议您避免使用这些伪区域。
以、、 或等格式指定正确的时区名称。切勿使用 2-4 个字母的缩写,例如or或,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。Continent/RegionAmerica/MontrealAfrica/CasablancaPacific/AucklandBSTESTIST
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
Run Code Online (Sandbox Code Playgroud)
您的示例代码表明您过于关注字符串。使用智能对象,而不是哑线。
使用适当的类型,让您的对象直截了当。生成字符串应该是最后一步,类似于本地化的附带工作。您的业务逻辑应该通过使用适当的对象来完成,而不是通过操作字符串来完成。
说到本地化:
Locale locale = Locale.UK;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime ( FormatStyle.MEDIUM ).withLocale ( locale );
String output = lt.format ( f );
Run Code Online (Sandbox Code Playgroud)
19:00:00
将语言环境切换Locale.US为不同类型的结果:
晚上 7:00:00
上面的所有代码都在 Java 13 早期访问中运行,并根据问题中所述的需求使用ThreeTen-Backport库。
import org.threeten.bp.* ;
import org.threeten.bp.format.* ;
Run Code Online (Sandbox Code Playgroud)
读者注意事项:ThreeTen-Backport库进一步适用于ThreeTenABP库中的早期 Android 。请参阅如何在 Android 中使用 ThreeTenABP。如果使用 Android 26 及更高版本,则捆绑了java.time类,因此您根本不需要后向端口。
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |