Domanda

Yaml file:

!!test.User
timestamp: 2012-11-22T01:02:03.567Z

Java class:

package test;

public class User {
    public Date timestamp;
}

Parse it with snakeyaml:

String str = "2012-11-22T01:02:03.567Z";

// parse it manually
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(str);
System.out.println("manually: " + date);

// parse it by snakeyaml
Yaml yaml = new Yaml();
yaml.setBeanAccess(BeanAccess.FIELD);
InputStream input = new FileInputStream("C:\\test.yaml");
User myUser = yaml.loadAs(input, User.class);
System.out.println("by Yaml: " + user.timestamp);

It prints:

manually: Thu Nov 22 01:02:03 CST 2012
by Yaml: Thu Nov 22 09:02:03 CST 2012

You can see they are different. Why?

È stato utile?

Soluzione

Your manual method gets the time in the current time zone, but it is actually expressed in UTC (as indicated by the Z time zone). So actually the value parsed by Yaml seems to be the correct one.

I am puzzled about the actual value though, CST (Central Standard Time USA) should be 6 hours behind UTC, but you get 8 hours ahead.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top