001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/framework/util/TimeTools.java $
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036 package org.deegree.framework.util;
037
038 import static org.deegree.framework.log.LoggerFactory.getLogger;
039
040 import java.io.IOException;
041 import java.text.SimpleDateFormat;
042 import java.util.Calendar;
043 import java.util.Date;
044 import java.util.GregorianCalendar;
045 import java.util.Locale;
046 import java.util.Properties;
047
048 import org.deegree.framework.log.ILogger;
049
050 /**
051 * The <code>TimeTools</code> class can be used to format Strings to timecodes and get Calenadars of a given Timecode.
052 *
053 * <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
054 *
055 * @author last edited by: $Author: mschneider $
056 *
057 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
058 */
059
060 public class TimeTools {
061
062 private static final ILogger LOG = getLogger( TimeTools.class );
063
064 /**
065 * A final Year representation
066 */
067 public static final int YEAR = 0;
068
069 /**
070 * A final Month representation
071 */
072 public static final int MONTH = 1;
073
074 /**
075 * A final Day representation
076 */
077 public static final int DAY = 2;
078
079 /**
080 * A final Hour representation
081 */
082 public static final int HOUR = 3;
083
084 /**
085 * A final Minute representation
086 */
087 public static final int MINUTE = 4;
088
089 /**
090 * A final Second representation
091 */
092 public static final int SECOND = 5;
093
094 /**
095 * A final MilliSecond representation
096 */
097 public static final int MILLISECOND = 6;
098
099 private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.GERMANY );
100
101 private static boolean CORRECT_TIMEZONES = false;
102
103 static {
104 Properties props = new Properties();
105 try {
106 props.load( TimeTools.class.getResourceAsStream( "timetools.properties" ) );
107 Object prop = props.get( "usecorrecttimezones" );
108 CORRECT_TIMEZONES = prop != null && prop.toString().equalsIgnoreCase( "true" );
109 } catch ( IOException e ) {
110 LOG.logError( "Unknown error", e );
111 }
112 }
113
114 /**
115 * @return the current timestamp in ISO format
116 */
117 public static String getISOFormattedTime() {
118 return getISOFormattedTime( new Date( System.currentTimeMillis() ) );
119 }
120
121 /**
122 * returns the date calendar in ISO format
123 *
124 * @param date
125 * @return the date calendar in ISO format
126 */
127 public static String getISOFormattedTime( Date date ) {
128 if ( CORRECT_TIMEZONES ) {
129 return TimeTools2.getISOFormattedTime( date );
130 }
131 return sdf.format( date ).replace( ' ', 'T' );
132 }
133
134 /**
135 * @param date
136 * the date object to get the time values of
137 * @param locale
138 * the locale to convert to
139 * @return the date calendar in ISO format considering the passed locale
140 */
141 public static String getISOFormattedTime( Date date, Locale locale ) {
142 SimpleDateFormat sdf_ = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS", locale );
143 return sdf_.format( date );
144 }
145
146 /**
147 *
148 * @param cal
149 * a Calendar to get the timevalues of
150 * @return the passed calendar in ISO format
151 */
152 public static String getISOFormattedTime( Calendar cal ) {
153 return getISOFormattedTime( cal.getTime() );
154 }
155
156 /**
157 * returns a part of the submitted iso-formatted timestamp. possible values
158 *
159 * @param value
160 * <ul>
161 * <li>YEAR
162 * <li>MONTH
163 * <li>DAY
164 * <li>HOUR
165 * <li>MINUTE
166 * <li>SECOND
167 * </ul>
168 * @param isoTimestamp
169 * an ISO timestamp-> year-mon-dayThours:min:sec
170 * @return the timevalue of the given value
171 */
172 private static int get( int value, String[] isoTimestamp ) {
173 if ( value > isoTimestamp.length - 1 ) {
174 return 0;
175 }
176 return Integer.parseInt( isoTimestamp[value] );
177 }
178
179 /**
180 * Notice that a Calendar does not consider milli seconds. This means 2008-09-22T12:31:00.999Z and
181 * 2008-09-22T12:31:00.111Z will return the same Calendar value!
182 *
183 * @param isoDate
184 * an ISO timestamp-> year-mon-dayThours:min:sec
185 * @return an instance of a <code>GregorianCalendar</tt> from an ISO timestamp
186 * @throws NumberFormatException
187 * if the parsted values of the given String are no proper numbers.
188 */
189 public static GregorianCalendar createCalendar( String isoDate )
190 throws NumberFormatException {
191 if ( CORRECT_TIMEZONES ) {
192 return (GregorianCalendar) TimeTools2.createCalendar( isoDate );
193 }
194
195 String s = isoDate.trim();
196 if ( s.endsWith( "Z" ) ) {
197 s = s.substring( 0, s.length() - 1 );
198 }
199 String[] tmp = StringTools.toArray( s, "-:T. ", false );
200 int y = TimeTools.get( TimeTools.YEAR, tmp );
201 int m = TimeTools.get( TimeTools.MONTH, tmp );
202 int d = TimeTools.get( TimeTools.DAY, tmp );
203 int h = TimeTools.get( TimeTools.HOUR, tmp );
204 int min = TimeTools.get( TimeTools.MINUTE, tmp );
205 int sec = TimeTools.get( TimeTools.SECOND, tmp );
206 return new GregorianCalendar( y, m - 1, d, h, min, sec );
207 }
208
209 /**
210 *
211 * @param isoDate
212 * an ISO timestamp-> year-mon-dayThours:min:sec.millis
213 * @return an instance of a <code>java.util.Date</tt> from an ISO timestamp
214 * @throws NumberFormatException
215 * if the parsted values of the given String are no proper numbers.
216 */
217 public static Date createDate( String isoDate ) {
218 String s = isoDate.trim();
219 if ( s.endsWith( "Z" ) ) {
220 s = s.substring( 0, s.length() - 1 );
221 }
222 String[] tmp = StringTools.toArray( s, "-:T. ", false );
223 int y = TimeTools.get( TimeTools.YEAR, tmp );
224 int m = TimeTools.get( TimeTools.MONTH, tmp );
225 int d = TimeTools.get( TimeTools.DAY, tmp );
226 int h = TimeTools.get( TimeTools.HOUR, tmp );
227 int min = TimeTools.get( TimeTools.MINUTE, tmp );
228 int sec = TimeTools.get( TimeTools.SECOND, tmp );
229 int millis = TimeTools.get( TimeTools.MILLISECOND, tmp );
230 long l = new GregorianCalendar( y, m - 1, d, h, min, sec ).getTimeInMillis();
231 l += millis;
232 return new Date( l );
233
234 }
235
236 }