1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.date.internal;
20 import static org.jclouds.date.internal.DateUtils.trimNanosToMillis;
21 import static org.jclouds.date.internal.DateUtils.trimTZ;
22
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Locale;
27 import java.util.SimpleTimeZone;
28
29 import org.jclouds.date.DateService;
30
31
32
33
34
35
36
37
38 public class SimpleDateFormatDateService implements DateService {
39
40
41
42
43
44
45 private static final SimpleDateFormat iso8601SecondsSimpleDateFormat = new SimpleDateFormat(
46 "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
47
48
49 private static final SimpleDateFormat iso8601SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
50 Locale.US);
51
52
53 private static final SimpleDateFormat rfc822SimpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
54 Locale.US);
55
56
57 private static final SimpleDateFormat cSimpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss '+0000' yyyy",
58 Locale.US);
59
60 static {
61 iso8601SimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
62 iso8601SecondsSimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
63 rfc822SimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
64 cSimpleDateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
65 }
66
67 public final Date fromSeconds(long seconds) {
68 return new Date(seconds * 1000);
69 }
70
71 public final String cDateFormat(Date date) {
72 synchronized (cSimpleDateFormat) {
73 return cSimpleDateFormat.format(date);
74 }
75 }
76
77 public final String cDateFormat() {
78 return cDateFormat(new Date());
79 }
80
81 public final Date cDateParse(String toParse) {
82 synchronized (cSimpleDateFormat) {
83 try {
84 return cSimpleDateFormat.parse(toParse);
85 } catch (ParseException e) {
86 throw new RuntimeException(e);
87 }
88 }
89 }
90
91 public final String rfc822DateFormat(Date date) {
92 synchronized (rfc822SimpleDateFormat) {
93 return rfc822SimpleDateFormat.format(date);
94 }
95 }
96
97 public final String rfc822DateFormat() {
98 return rfc822DateFormat(new Date());
99 }
100
101 public final Date rfc822DateParse(String toParse) {
102 synchronized (rfc822SimpleDateFormat) {
103 try {
104 return rfc822SimpleDateFormat.parse(toParse);
105 } catch (ParseException e) {
106 throw new RuntimeException(e);
107 }
108 }
109 }
110
111 public final String iso8601SecondsDateFormat() {
112 return iso8601SecondsDateFormat(new Date());
113 }
114
115 public final String iso8601DateFormat(Date date) {
116 synchronized (iso8601SimpleDateFormat) {
117 return iso8601SimpleDateFormat.format(date);
118 }
119 }
120
121 public final String iso8601DateFormat() {
122 return iso8601DateFormat(new Date());
123 }
124
125 public final Date iso8601DateParse(String toParse) {
126 toParse = trimTZ(toParse);
127 toParse = trimNanosToMillis(toParse);
128 synchronized (iso8601SimpleDateFormat) {
129 try {
130 return iso8601SimpleDateFormat.parse(toParse);
131 } catch (ParseException e) {
132 throw new RuntimeException(e);
133 }
134 }
135 }
136
137 public final Date iso8601SecondsDateParse(String toParse) {
138 toParse = trimTZ(toParse);
139 synchronized (iso8601SecondsSimpleDateFormat) {
140 try {
141 return iso8601SecondsSimpleDateFormat.parse(toParse);
142 } catch (ParseException e) {
143 throw new RuntimeException(e);
144 }
145 }
146 }
147
148 @Override
149 public String iso8601SecondsDateFormat(Date date) {
150 synchronized (iso8601SecondsSimpleDateFormat) {
151 return iso8601SecondsSimpleDateFormat.format(date);
152 }
153 }
154
155 }