Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Yaml日期字符串中设置时区?_Java_Yaml_Simpledateformat - Fatal编程技术网

Java 如何在Yaml日期字符串中设置时区?

Java 如何在Yaml日期字符串中设置时区?,java,yaml,simpledateformat,Java,Yaml,Simpledateformat,我使用org.yaml.snakeyaml.yaml SimpleDataFormat使用系统时区(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 (Myan

我使用
org.yaml.snakeyaml.yaml

SimpleDataFormat使用系统时区(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

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

这是以编程方式计算时间差(例如:+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
为了解析YAML文件,所有没有时区的日期(在
.yml
中)都假定为UTC。因此,您必须确保
.yml
具有预期的时区,或者按照上面链接中的建议定制snakeyaml的解析器

对于转储数据,您需要使用snakeyaml自己的转储选项,如下所示:


我的代码根据以下代码解决此问题:

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

甚至我也像你说的那样使用
Yaml-Yaml=新的Yaml(选项)<代码>Yaml
仍然是UTC时区。如果你用你的答案测试我的程序,它仍然是不同的输出。谢谢。因此,您希望以缅甸的时区输出日期,但没有时区名称,例如“+6:30”?public static void main(String[]args)抛出异常{String dateString=“2015-11-17 15:30:30”;SimpleDateFormat format=new SimpleDateFormat(“yyy-MM-dd HH:MM:ss”);date sdfDate=format.parse(dateString);DumperOptions options=new DumperOptions();options.setTimeZone(TimeZone.getTimeZone(“GMT+6:30”);Yaml Yaml=new Yaml(options);Date yamlDate=(Date)Yaml.load(dateString);System.out.println(“Date 1”+format.format(sdfDate));System.out.println(“Date 2”+format.format(yamlDate));}不幸的是,我想不出一个办法让snakeyaml做你想要做的事情(快速)。但是,如果您转储的YAML文件的日期类似于
2015-11-17 15:30:30
,您会发现任何其他符合YAML的解析器(包括snakeyaml)都会假定该日期是UTC。
TimeZone.getTimeZone("UTC");
Yaml yaml = new Yaml();