forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.java
More file actions
152 lines (140 loc) · 4.93 KB
/
DateUtils.java
File metadata and controls
152 lines (140 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
* for applicable license terms and NOTICE.txt for applicable notices.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
/**
* Utilities for parsing and formatting dates.
* <p>
* Note that this class doesn't use static methods because of the
* synchronization issues with SimpleDateFormat. This lets synchronization be
* done on a per-object level, instead of on a per-class level.
*/
public class DateUtils {
/** ISO 8601 format */
protected final SimpleDateFormat iso8601DateFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
/** Alternate ISO 8601 format without fractional seconds */
protected final SimpleDateFormat alternateIso8601DateFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
/** RFC 822 format */
protected final SimpleDateFormat rfc822DateFormat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
/**
* This is another ISO 8601 format that's used in clock skew error response
*/
protected final SimpleDateFormat compressedIso8601DateFormat =
new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
/**
* Constructs a new DateUtils object, ready to parse/format dates.
*/
public DateUtils() {
iso8601DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
rfc822DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
alternateIso8601DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
compressedIso8601DateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
}
/**
* Parses the specified date string as an ISO 8601 date and returns the Date
* object.
*
* @param dateString
* The date string to parse.
*
* @return The parsed Date object.
*
* @throws ParseException
* If the date string could not be parsed.
*/
public Date parseIso8601Date(String dateString) throws ParseException {
try {
synchronized (iso8601DateFormat) {
return iso8601DateFormat.parse(dateString);
}
} catch (ParseException e) {
// If the first ISO 8601 parser didn't work, try the alternate
// version which doesn't include fractional seconds
synchronized (alternateIso8601DateFormat) {
return alternateIso8601DateFormat.parse(dateString);
}
}
}
/**
* Formats the specified date as an ISO 8601 string.
*
* @param date
* The date to format.
*
* @return The ISO 8601 string representing the specified date.
*/
public String formatIso8601Date(Date date) {
synchronized (iso8601DateFormat) {
return iso8601DateFormat.format(date);
}
}
/**
* Parses the specified date string as an RFC 822 date and returns the Date
* object.
*
* @param dateString
* The date string to parse.
*
* @return The parsed Date object.
*
* @throws ParseException
* If the date string could not be parsed.
*/
public Date parseRfc822Date(String dateString) throws ParseException {
synchronized (rfc822DateFormat) {
return rfc822DateFormat.parse(dateString);
}
}
/**
* Formats the specified date as an RFC 822 string.
*
* @param date
* The date to format.
*
* @return The RFC 822 string representing the specified date.
*/
public String formatRfc822Date(Date date) {
synchronized (rfc822DateFormat) {
return rfc822DateFormat.format(date);
}
}
/**
* Parses the specified date string as a compressedIso8601DateFormat ("yyyyMMdd'T'HHmmss'Z'") and returns the Date
* object.
*
* @param dateString
* The date string to parse.
*
* @return The parsed Date object.
*
* @throws ParseException
* If the date string could not be parsed.
*/
public Date parseCompressedIso8601Date(String dateString) throws ParseException {
synchronized (compressedIso8601DateFormat) {
return compressedIso8601DateFormat.parse(dateString);
}
}
}