001: //==============================================================================
002: //===
003: //=== ISODate
004: //===
005: //==============================================================================
006: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
007: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
008: //=== and United Nations Environment Programme (UNEP)
009: //===
010: //=== This program is free software; you can redistribute it and/or modify
011: //=== it under the terms of the GNU General Public License as published by
012: //=== the Free Software Foundation; either version 2 of the License, or (at
013: //=== your option) any later version.
014: //===
015: //=== This program is distributed in the hope that it will be useful, but
016: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
017: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: //=== General Public License for more details.
019: //===
020: //=== You should have received a copy of the GNU General Public License
021: //=== along with this program; if not, write to the Free Software
022: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
023: //===
024: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
025: //=== Rome - Italy. email: geonetwork@osgeo.org
026: //==============================================================================
027:
028: package org.fao.geonet.util;
029:
030: import java.util.Calendar;
031: import java.util.Date;
032: import java.util.GregorianCalendar;
033: import java.util.StringTokenizer;
034: import java.util.TimeZone;
035:
036: //==============================================================================
037:
038: public class ISODate implements Cloneable {
039: public int year; //--- 4 digits
040: public int month; //--- 1..12
041: public int day; //--- 1..31
042: public int hour; //--- 0..23
043: public int min; //--- 0..59
044: public int sec; //--- 0..59
045:
046: public boolean isShort; //--- 'true' if the format is yyyy-mm-dd
047:
048: //---------------------------------------------------------------------------
049:
050: private static Calendar calendar = Calendar.getInstance();
051:
052: //---------------------------------------------------------------------------
053: //---
054: //--- Constructor
055: //---
056: //---------------------------------------------------------------------------
057:
058: public ISODate() {
059: this (System.currentTimeMillis());
060: }
061:
062: //---------------------------------------------------------------------------
063:
064: public ISODate(long time) {
065: synchronized (calendar) {
066: calendar.setTimeInMillis(time);
067:
068: year = calendar.get(Calendar.YEAR);
069: month = calendar.get(Calendar.MONTH) + 1;
070: day = calendar.get(Calendar.DAY_OF_MONTH);
071:
072: hour = calendar.get(Calendar.HOUR_OF_DAY);
073: min = calendar.get(Calendar.MINUTE);
074: sec = calendar.get(Calendar.SECOND);
075: }
076: }
077:
078: //---------------------------------------------------------------------------
079:
080: public ISODate(String isoDate) {
081: setDate(isoDate);
082: }
083:
084: //---------------------------------------------------------------------------
085:
086: private ISODate(int y, int m, int d, int h, int mi, int s) {
087: year = y;
088: month = m;
089: day = d;
090: hour = h;
091: min = mi;
092: sec = s;
093: }
094:
095: //---------------------------------------------------------------------------
096: //---
097: //--- API methods
098: //---
099: //---------------------------------------------------------------------------
100:
101: public ISODate clone() {
102: return new ISODate(year, month, day, hour, min, sec);
103: }
104:
105: //---------------------------------------------------------------------------
106:
107: public void setDate(String isoDate) {
108: if (isoDate == null)
109: throw new IllegalArgumentException("ISO date is null");
110:
111: if (isoDate.length() < 10)
112: throw new IllegalArgumentException("Invalid ISO date : "
113: + isoDate);
114:
115: try {
116: year = Integer.parseInt(isoDate.substring(0, 4));
117: month = Integer.parseInt(isoDate.substring(5, 7));
118: day = Integer.parseInt(isoDate.substring(8, 10));
119:
120: isShort = true;
121:
122: hour = 0;
123: min = 0;
124: sec = 0;
125:
126: //--- is the date in 'yyyy-mm-dd' or 'yyyy-mm-ddZ' format?
127:
128: if (isoDate.length() < 12)
129: return;
130:
131: isoDate = isoDate.substring(11);
132:
133: hour = Integer.parseInt(isoDate.substring(0, 2));
134: min = Integer.parseInt(isoDate.substring(3, 5));
135: sec = Integer.parseInt(isoDate.substring(6, 8));
136:
137: isShort = false;
138: } catch (Exception e) {
139: throw new IllegalArgumentException("Invalid ISO date : "
140: + isoDate);
141: }
142: }
143:
144: //---------------------------------------------------------------------------
145: /** Subtract a date from this date and return the seconds between them */
146:
147: public long sub(ISODate date) {
148: return getSeconds() - date.getSeconds();
149: }
150:
151: //--------------------------------------------------------------------------
152:
153: public String getDate() {
154: return year + "-" + pad(month) + "-" + pad(day);
155: }
156:
157: //--------------------------------------------------------------------------
158:
159: public String getTime() {
160: return pad(hour) + ":" + pad(min) + ":" + pad(sec);
161: }
162:
163: //--------------------------------------------------------------------------
164:
165: public String toString() {
166: return getDate() + "T" + getTime();
167: }
168:
169: //---------------------------------------------------------------------------
170:
171: public long getSeconds() {
172: synchronized (calendar) {
173: calendar.clear();
174: calendar.set(year, month - 1, day, hour, min, sec);
175:
176: return calendar.getTimeInMillis() / 1000;
177: }
178: }
179:
180: //---------------------------------------------------------------------------
181: //---
182: //--- Private methods
183: //---
184: //---------------------------------------------------------------------------
185:
186: private String pad(int value) {
187: if (value > 9)
188: return Integer.toString(value);
189:
190: return "0" + value;
191: }
192: }
193:
194: //==============================================================================
|