1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.jodatime;
17
18 import java.util.Date;
19
20 import junit.framework.TestCase;
21
22 import org.joda.time.DateMidnight;
23 import org.joda.time.DateTime;
24 import org.joda.time.DateTimeZone;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.constructor.AbstractConstruct;
27 import org.yaml.snakeyaml.constructor.Construct;
28 import org.yaml.snakeyaml.constructor.Constructor;
29 import org.yaml.snakeyaml.nodes.Node;
30 import org.yaml.snakeyaml.nodes.NodeId;
31 import org.yaml.snakeyaml.nodes.Tag;
32
33 public class JodaTimeExampleTest extends TestCase {
34 private static final long timestamp = 1000000000000L;
35
36 public void testDump() {
37 DateTime time = new DateTime(timestamp, DateTimeZone.UTC);
38 Yaml yaml = new Yaml(new JodaTimeRepresenter());
39 String joda = yaml.dump(time);
40 String date = new Yaml().dump(new Date(timestamp));
41 assertEquals(date, joda);
42 assertEquals("2001-09-09T01:46:40Z\n", joda);
43 }
44
45 public void testLoad() {
46 Yaml yaml = new Yaml(new JodaTimeImplicitContructor());
47 DateTime time = (DateTime) yaml.load("2001-09-09T01:46:40Z");
48 assertEquals(new DateTime(timestamp, DateTimeZone.UTC), time);
49 }
50
51
52
53
54 public void test109() {
55 Date someDate = new DateMidnight(9, 2, 21, DateTimeZone.forID("Europe/Amsterdam")).toDate();
56 Yaml yaml = new Yaml();
57 String timestamp = yaml.dump(someDate);
58 assertEquals("0009-02-22T23:40:28Z\n", timestamp);
59
60 Object o = yaml.load(timestamp);
61 assertEquals(someDate, o);
62 }
63
64 class JodaPropertyConstructor extends Constructor {
65 public JodaPropertyConstructor() {
66 yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
67 }
68
69 class TimeStampConstruct extends Constructor.ConstructScalar {
70 @Override
71 public Object construct(Node nnode) {
72 if (nnode.getTag().equals("tag:yaml.org,2002:timestamp")) {
73 Construct dateConstructor = yamlConstructors.get(Tag.TIMESTAMP);
74 Date date = (Date) dateConstructor.construct(nnode);
75 return new DateTime(date, DateTimeZone.UTC);
76 } else {
77 return super.construct(nnode);
78 }
79 }
80 }
81 }
82
83
84
85
86
87 public class JodaTimeConstructor extends Constructor {
88 private final Construct javaDateConstruct;
89 private final Construct jodaDateConstruct;
90
91 public JodaTimeConstructor() {
92 javaDateConstruct = new ConstructYamlTimestamp();
93 jodaDateConstruct = new ConstructJodaTimestamp();
94
95
96 yamlConstructors.put(Tag.TIMESTAMP, jodaDateConstruct);
97
98
99 yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
100 }
101
102 public class ConstructJodaTimestamp extends AbstractConstruct {
103 public Object construct(Node node) {
104 Date date = (Date) javaDateConstruct.construct(node);
105 return new DateTime(date, DateTimeZone.UTC);
106 }
107 }
108
109 class TimeStampConstruct extends Constructor.ConstructScalar {
110 @Override
111 public Object construct(Node nnode) {
112 if (nnode.getTag().equals(Tag.TIMESTAMP)) {
113 return jodaDateConstruct.construct(nnode);
114 } else {
115 return super.construct(nnode);
116 }
117 }
118 }
119 }
120 }