001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/util/TimeTools.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 043 ---------------------------------------------------------------------------*/ 044 package org.deegree.framework.util; 045 046 import java.text.SimpleDateFormat; 047 import java.util.Calendar; 048 import java.util.Date; 049 import java.util.GregorianCalendar; 050 import java.util.Locale; 051 052 /** 053 * The <code>TimeTools</code> class can be used to format Strings to timecodes and get Calenadars 054 * of a given Timecode. 055 * 056 * <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 057 * 058 * @author last edited by: $Author: apoth $ 059 * 060 * @version $Revision: 9339 $, $Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $ 061 */ 062 063 public class TimeTools { 064 065 /** 066 * A final Year representation 067 */ 068 public static final int YEAR = 0; 069 070 /** 071 * A final Month representation 072 */ 073 public static final int MONTH = 1; 074 075 /** 076 * A final Day representation 077 */ 078 public static final int DAY = 2; 079 080 /** 081 * A final Hour representation 082 */ 083 public static final int HOUR = 3; 084 085 /** 086 * A final Minute representation 087 */ 088 public static final int MINUTE = 4; 089 090 /** 091 * A final Second representation 092 */ 093 public static final int SECOND = 5; 094 095 private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", 096 Locale.GERMANY ); 097 098 /** 099 * @return the current timestamp in ISO format 100 */ 101 public static String getISOFormattedTime() { 102 return getISOFormattedTime( new Date( System.currentTimeMillis() ) ); 103 } 104 105 /** 106 * returns the date calendar in ISO format 107 * 108 * @param date 109 * @return the date calendar in ISO format 110 */ 111 public static String getISOFormattedTime( Date date ) { 112 return sdf.format( date ).replace( ' ', 'T' ); 113 } 114 115 /** 116 * @param date 117 * the date object to get the time values of 118 * @param locale 119 * the locale to convert to 120 * @return the date calendar in ISO format considering the passed locale 121 */ 122 public static String getISOFormattedTime( Date date, Locale locale ) { 123 SimpleDateFormat sdf_ = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", locale ); 124 return sdf_.format( date ).replace( ' ', 'T' ); 125 } 126 127 /** 128 * 129 * @param cal 130 * a Calendar to get the timevalues of 131 * @return the passed calendar in ISO format 132 */ 133 public static String getISOFormattedTime( Calendar cal ) { 134 return getISOFormattedTime( cal.getTime() ); 135 } 136 137 /** 138 * returns a part of the submitted iso-formatted timestamp. possible values 139 * 140 * @param value 141 * <ul> 142 * <li>YEAR 143 * <li>MONTH 144 * <li>DAY 145 * <li>HOUR 146 * <li>MINUTE 147 * <li>SECOND 148 * </ul> 149 * @param isoTimestamp 150 * an ISO timestamp-> year-mon-dayThours:min:sec 151 * @return the timevalue of the given value 152 */ 153 private static int get( int value, String[] isoTimestamp ) { 154 if( value > isoTimestamp.length -1 ){ 155 return 0; 156 } 157 return Integer.parseInt( isoTimestamp[value] ); 158 } 159 160 /** 161 * 162 * @param isoDate 163 * an ISO timestamp-> year-mon-dayThours:min:sec 164 * @return an instance of a <code>GregorianCalendar</tt> from an ISO timestamp 165 * @throws NumberFormatException if the parsted values of the given String are no proper numbers. 166 */ 167 public static GregorianCalendar createCalendar( String isoDate ) throws NumberFormatException { 168 String[] tmp = StringTools.toArray( isoDate.trim(), "-:T.", false ); 169 int y = TimeTools.get( TimeTools.YEAR, tmp ); 170 int m = TimeTools.get( TimeTools.MONTH, tmp ); 171 int d = TimeTools.get( TimeTools.DAY, tmp ); 172 int h = TimeTools.get( TimeTools.HOUR, tmp ); 173 int min = TimeTools.get( TimeTools.MINUTE, tmp ); 174 int sec = TimeTools.get( TimeTools.SECOND, tmp ); 175 return new GregorianCalendar( y, m - 1, d, h, min, sec ); 176 } 177 178 }