Java:将TimeZone添加到DateTimeFormatter [英] Java: Adding TimeZone to DateTimeFormatter

查看:181
本文介绍了Java:将TimeZone添加到DateTimeFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LocalDateTime API允许通过使用格式化程序中的键 z来添加TimeZone名称。添加此键后,我异常,不明白为什么。我正在寻找类似此示例的示例,例如' 11:59:22 PM GMT ',而不是' ** ... UMT + 2 **

The LocalDateTime API gives the possibility to add the TimeZone Name by using the key "z" in the formatter. I get an exception adding this key and don't understand why. I'm looking for something like this example '11:59:22 PM GMT' and not '**... UMT+2**'.

我的代码:

public class TimeZone
{
    public static void main(String[] args)
    {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a z");
        System.out.println(now.format(formatter));
    }
}

例外:

Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime
at java.time.format.DateTimePrintContext.getValue(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(Unknown Source)
at java.time.format.DateTimeFormatter.formatTo(Unknown Source)
at java.time.format.DateTimeFormatter.format(Unknown Source)
at java.time.LocalDateTime.format(Unknown Source)
at tz.TimeZone.main(TimeZone.java:12)

这里是DateTimeFormatter API部分:

Here is the DateTimeFormatter API part:

All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The following pattern letters are defined: 
  Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   G       era                         text              AD; Anno Domini; A
   u       year                        year              2004; 04
   y       year-of-era                 year              2004; 04
   D       day-of-year                 number            189
   M/L     month-of-year               number/text       7; 07; Jul; July; J
   d       day-of-month                number            10

   Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
   Y       week-based-year             year              1996; 96
   w       week-of-week-based-year     number            27
   W       week-of-month               number            4
   E       day-of-week                 text              Tue; Tuesday; T
   e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
   F       week-of-month               number            3

   a       am-pm-of-day                text              PM
   h       clock-hour-of-am-pm (1-12)  number            12
   K       hour-of-am-pm (0-11)        number            0
   k       clock-hour-of-am-pm (1-24)  number            0

   H       hour-of-day (0-23)          number            0
   m       minute-of-hour              number            30
   s       second-of-minute            number            55
   S       fraction-of-second          fraction          978
   A       milli-of-day                number            1234
   n       nano-of-second              number            987654321
   N       nano-of-day                 number            1234000000

   V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
   z       time-zone name              zone-name         Pacific Standard Time; PST
   O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
   x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
   Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

有人可以看到问题所在吗?

Can anyone see what the problem is?

推荐答案

LocalDateTime 具有两个类型为 LocalDate LocalTime 。

LocalDate 具有字段 day month year

LocalTime 具有字段 hour minute nano

LocalDateTime has two fields of type LocalDate and LocalTime.
LocalDate has fields day, month, and year.
LocalTime has fields hour, minute, second, and nano.

在那个地方都没有给出时区。这是自然的,因为 LocalDateTime 的javadoc说:

Nowhere in that is a time zone given. Which is by nature, since the javadoc of LocalDateTime says:


日期-没有时区的时间

A date-time without a time-zone

因此,如果本地日期/时间值已经以UTC表示时间,则您想要这样格式化,您有多个选择:

So, if the "local" date/time value is already representing a time in UTC, and you want it formatted saying so, you have multiple options:

System.out.println(time.atZone(ZoneOffset.UTC)
                       .format(DateTimeFormatter.ofPattern("hh:mm:ss a z")));


  • 通过调用 withZone()

    System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss a z")
                                                    .withZone(ZoneOffset.UTC)));
    


  • 使用文字 Z 的格式字符:

    System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss a 'Z'")));
    


  • 以上所有三个输出:

    11:59:22 PM Z

    现在,如果本地日期/时间确实在不同的时区中,则可以使用前两个时区中的任一个,只需指定实际时区即可。

    Now, if the "local" date/time is really in a different time zone, you can use either of the first two, and just specify the actual zone.

    例如如果时区为 -04:00 ,请使用 ZoneOffset.ofHours(-4),您将获得:

    E.g. if time zone is -04:00, use ZoneOffset.ofHours(-4), and you'll get:

    11:59:22 PM -04:00

    或者如果您在纽约,请使用 ZoneId.of( America / New_York),您将获得:

    Or if you are in New York, use ZoneId.of("America/New_York"), and you'll get:

    11:59:22 PM EDT

    如果本地日期/时间是针对纽约的,但您希望格式化的文本为UTC,请同时使用两者,即

    If the "local" date/time is for New York, but you want formatted text to be UTC, use both at the same time, i.e.

    System.out.println(time.atZone(ZoneId.of("America/New_York"))
                           .format(DateTimeFormatter.ofPattern("hh:mm:ss a z")
                                                    .withZone(ZoneOffset.UTC)));
    

    这样,您就可以将时间转换为:

    With that, you get the time converted to:

    03:59:22 Z

    这篇关于Java:将TimeZone添加到DateTimeFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆