【问题标题】:How to set timezone in Yaml date string?如何在 Yaml 日期字符串中设置时区?
【发布时间】:2016-02-20 06:38:18
【问题描述】:

我使用org.yaml.snakeyaml.Yaml

SimpleDateFormat 使用系统时区(UTC +6:30)。

我想要像 SimpleDateFormat 这样的 yaml 日期输出。

public static void main(String[] args) throws Exception {
    String dateString = "2015-11-17 15:30:30"; 
    /*
        SimpleDateFormat will UTC +6:30 (Myanmar Timezone)
    */
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date sdfDate = format.parse(dateString);
    System.out.println("Date 1 " + format.format(sdfDate));

    /*
        Yaml will not use.
    */
    Yaml yaml = new Yaml();
    //yaml.setTimeZone(xxx) --> Is there way to set timezone?
    Date yamlDate = (Date) yaml.load(dateString);

    System.out.println("Date 2" + format.format(yamlDate));
}

输出

Date 1 2015-11-17 15:30:30
Date 2 2015-11-17 22:00:30

【问题讨论】:

    标签: java yaml simpledateformat


    【解决方案1】:

    我不确定这是解决此问题的最佳方法。
    暂时,我必须像下面这样解决。

    以编程方式计算不同的时间(例如:+6:30)并附加到日期字符串。

    示例日期字符串:2015-11-17 15:30:30 +6:30.

        String sdfSt = "2015-11-17 15:30:30";
    
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sdfDate = format.parse(sdfSt);
        System.out.println("Date 1 " + format.format(sdfDate));
    
        Yaml yaml = new Yaml();
        String itmeDiff = "+6:30"; --> for my timezone
        Date yamlDate = (Date) yaml.load(sdfSt + itmeDiff);
        System.out.println("Date 2 " + format.format(yamlDate));
    

    输出

    Date 1 2015-11-17 15:30:30
    Date 2 2015-11-17 15:30:30
    

    【讨论】:

      【解决方案2】:

      对于解析 YAML 文件,apparently the spec is clear 认为所有没有时区的日期(在.yml 中)都假定为 UTC。因此,您要么必须确保.yml 具有预期的时区,要么按照上面链接中的建议自定义snakeyaml 的解析器。

      对于转储数据,你想要的是使用snakeyaml自己的DumperOptions,根据this test code

      DumperOptions options = new DumperOptions();
      options.setTimeZone(TimeZone.getTimeZone("GMT+6:30"));
      Yaml yaml = new Yaml(options);
      

      【讨论】:

      • 即使我像你所说的那样使用Yaml yaml = new Yaml(options);Yaml 仍然是 UTC 时区。如果你用你的答案测试我的程序,它仍然是不同的输出。谢谢。
      • 所以你想输出缅甸时区的日期,但没有指定时区,例如“+6:30”?
      • public static void main(String[] args) throws Exception { String dateString = "2015-11-17 15:30:30"; SimpleDateFormat 格式 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");日期 sdfDate = format.parse(dateString); DumperOptions 选项 = 新的 DumperOptions(); options.setTimeZone(TimeZone.getTimeZone("GMT+6:30")); Yaml yaml = 新 Yaml(选项);日期 yamlDate = (日期) yaml.load(dateString); System.out.println("日期 1" + format.format(sdfDate)); System.out.println("日期 2" + format.format(yamlDate)); }
      • 不幸的是,我想不出一种方法让snakeyaml 做你所追求的(快速)。但是,如果您转储一个日期为 2015-11-17 15:30:30 的 YAML 文件,您会发现任何其他符合 YAML 的解析器(包括 snakeyaml)都会假定该日期为 UTC。
      【解决方案3】:

      我的代码按照以下代码解决了这个问题:

      TimeZone.getTimeZone("UTC");
      Yaml yaml = new Yaml();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        相关资源
        最近更新 更多