/
ISODate.java
275 lines (220 loc) · 7.62 KB
/
ISODate.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//==============================================================================
//===
//=== ISODate
//===
//==============================================================================
//=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
//=== and United Nations Environment Programme (UNEP)
//===
//=== This program is free software; you can redistribute it and/or modify
//=== it under the terms of the GNU General Public License as published by
//=== the Free Software Foundation; either version 2 of the License, or (at
//=== your option) any later version.
//===
//=== This program is distributed in the hope that it will be useful, but
//=== WITHOUT ANY WARRANTY; without even the implied warranty of
//=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//=== General Public License for more details.
//===
//=== You should have received a copy of the GNU General Public License
//=== along with this program; if not, write to the Free Software
//=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//===
//=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
//=== Rome - Italy. email: geonetwork@osgeo.org
//==============================================================================
package org.fao.geonet.util;
import java.util.Calendar;
//==============================================================================
public class ISODate implements Cloneable
{
public int year; //--- 4 digits
public int month; //--- 1..12
public int day; //--- 1..31
public int hour; //--- 0..23
public int min; //--- 0..59
public int sec; //--- 0..59
public static final String ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
public boolean isShort; //--- 'true' if the format is yyyy-mm-dd
//---------------------------------------------------------------------------
private static Calendar calendar = Calendar.getInstance();
//---------------------------------------------------------------------------
//---
//--- Constructor
//---
//---------------------------------------------------------------------------
public ISODate()
{
this(System.currentTimeMillis());
}
//---------------------------------------------------------------------------
public ISODate(long time)
{
synchronized(calendar)
{
calendar.setTimeInMillis(time);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) +1;
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
min = calendar.get(Calendar.MINUTE);
sec = calendar.get(Calendar.SECOND);
}
}
//---------------------------------------------------------------------------
public ISODate(String isoDate)
{
setDate(isoDate);
}
//---------------------------------------------------------------------------
private ISODate(int y, int m, int d, int h, int mi, int s)
{
year = y;
month = m;
day = d;
hour = h;
min = mi;
sec = s;
}
//---------------------------------------------------------------------------
//---
//--- API methods
//---
//---------------------------------------------------------------------------
public ISODate clone()
{
ISODate clone;
try {
clone = (ISODate) super.clone();
clone.year = year;
clone.month = month;
clone.day = day;
clone.hour = hour;
clone.min = min;
clone.sec = sec;
return clone;
} catch (CloneNotSupportedException e) {
return new ISODate(year, month, day, hour, min, sec);
}
}
//---------------------------------------------------------------------------
public void setDate(String isoDate)
{
if (isoDate == null)
throw new IllegalArgumentException("ISO date is null");
if (isoDate.length() < 10)
throw new IllegalArgumentException("Invalid ISO date : "+ isoDate);
try
{
if (isoDate.contains("T")) {
// Check if iso date contains time info and if using non UTC time zone to parse the date with
// JODAISODate. This class converts to UTC format to avoid timezones issues.
boolean timeZoneInfo = ((isoDate.split("T")[1].contains("+")) || (isoDate.split("T")[1].contains("-")));
if (timeZoneInfo) {
isoDate = JODAISODate.parseISODateTime(isoDate);
}
}
year = Integer.parseInt(isoDate.substring(0, 4));
month = Integer.parseInt(isoDate.substring(5, 7));
day = Integer.parseInt(isoDate.substring(8, 10));
isShort = true;
hour = 0;
min = 0;
sec = 0;
//--- is the date in 'yyyy-mm-dd' or 'yyyy-mm-ddZ' format?
if (isoDate.length() < 12)
return;
isoDate = isoDate.substring(11);
hour = Integer.parseInt(isoDate.substring(0,2));
min = Integer.parseInt(isoDate.substring(3,5));
sec = Integer.parseInt(isoDate.substring(6,8));
isShort = false;
}
catch(Exception e)
{
throw new IllegalArgumentException("Invalid ISO date : "+ isoDate);
}
}
//---------------------------------------------------------------------------
/** Subtract a date from this date and return the seconds between them */
public long sub(ISODate date)
{
return getSeconds() - date.getSeconds();
}
//--------------------------------------------------------------------------
public String getDate()
{
return year +"-"+ pad(month) +"-"+ pad(day);
}
//--------------------------------------------------------------------------
public String getTime()
{
return pad(hour) +":"+ pad(min) +":"+ pad(sec);
}
//--------------------------------------------------------------------------
public String toString()
{
return getDate() +"T"+ getTime();
}
//---------------------------------------------------------------------------
public long getSeconds()
{
synchronized(calendar)
{
calendar.clear();
calendar.set(year, month -1, day, hour, min, sec);
return calendar.getTimeInMillis() / 1000;
}
}
//---------------------------------------------------------------------------
//---
//--- Private methods
//---
//---------------------------------------------------------------------------
private String pad(int value)
{
if (value > 9)
return Integer.toString(value);
return "0"+ value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + hour;
result = prime * result + (isShort ? 1231 : 1237);
result = prime * result + min;
result = prime * result + month;
result = prime * result + sec;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ISODate other = (ISODate) obj;
if (day != other.day)
return false;
if (hour != other.hour)
return false;
if (isShort != other.isShort)
return false;
if (min != other.min)
return false;
if (month != other.month)
return false;
if (sec != other.sec)
return false;
if (year != other.year)
return false;
return true;
}
}
//==============================================================================