001 package sale; 002 003 import java.util.*; 004 import java.text.SimpleDateFormat; 005 import data.*; 006 007 /** 008 * This class is an implementation of the {@link Time Time} interface. 009 * 010 * <p>The time is represented as a Gregorian Calendar. 011 * 012 * <p>Only a simple Adapterclass for java.util.GregorianCalendar 013 * 014 * @author Thomas Medack 015 * @version 2.0 29/11/2000 016 * @since v2.0 017 */ 018 public class CalendarTime extends Object implements Time { 019 020 /** 021 * The Calender containing the current date 022 * 023 * @serial 024 */ 025 private Calendar m_cCalendar; 026 027 /** 028 * The default interval for timesteps 029 * 030 * @serial 031 */ 032 private int m_iDefaultInterval = 1; 033 034 /** 035 * The field which {@link #goAhead} will increase. 036 * 037 * @see #YEAR 038 * @see #MONTH 039 * @see #DATE 040 * @see #HOUR 041 * @see #MINUTE 042 * @see #SECOND 043 * 044 * @serial 045 */ 046 private int m_iTimeToCount; 047 048 /** 049 * Creates a new CalendarTime with the current systemtime. 050 */ 051 public CalendarTime() { 052 this(System.currentTimeMillis()); 053 } 054 055 /** 056 * Creates a new CalendarTime with the given starting time. 057 * 058 * @param lTimeInMillis a long representing the time in milliseconds 059 */ 060 public CalendarTime(long lTimeInMillis) { 061 this(lTimeInMillis, DATE); 062 } 063 064 /** 065 * Creates a new CalendarTime with the given starting time and an int that defines the field which will 066 * be increased by {@link #goAhead}. 067 * @see #YEAR 068 * @see #MONTH 069 * @see #DATE 070 * @see #HOUR 071 * @see #MINUTE 072 * @see #SECOND 073 * 074 * @param lTimeInMillis a long representing the time in milliseconds 075 * @param iTimeToCount an int representing the field which will be increased by {@link #goAhead} 076 */ 077 public CalendarTime(long lTimeInMillis, int iTimeToCount) { 078 m_cCalendar = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"), Locale.GERMAN); 079 m_cCalendar.setTime(new java.util.Date(lTimeInMillis)); 080 081 m_iTimeToCount = iTimeToCount; 082 } 083 084 /** 085 * Creates a new CalendarTime with the given starting time 086 * 087 * @param year an int representing the Year you want to start with 088 * @param month an int representing the Month you want to start with 089 * @param date an int representing the Day you want to start with 090 * @param hours an int representing the Hour you want to start with 091 * @param minutes an int representing the Minute you want to start with 092 * @param seconds an int representing the Second you want to start with 093 */ 094 public CalendarTime(int year, int month, int date, int hours, int minutes, int seconds) { 095 this(year, month, date, hours, minutes, seconds, DATE); 096 } 097 098 /** 099 * Creates a new CalendarTime with the given starting time and an int that defines the field which will 100 * be increased by {@link #goAhead}. 101 * 102 * @see #YEAR 103 * @see #MONTH 104 * @see #DATE 105 * @see #HOUR 106 * @see #MINUTE 107 * @see #SECOND 108 * 109 * @param year an int representing the Year you want to start with 110 * @param month an int representing the Month you want to start with 111 * @param date an int representing the Day you want to start with 112 * @param hours an int representing the Hour you want to start with 113 * @param minutes an int representing the Minute you want to start with 114 * @param seconds an int representing the Second you want to start with 115 * @param iTimeToCount an int representing the field which will be increased by {@link #goAhead} 116 */ 117 public CalendarTime(int year, int month, int date, int hours, int minutes, int seconds, int iTimeToCount) { 118 m_cCalendar = new GregorianCalendar(year, month, date, hours, minutes, seconds); 119 120 // set the Timezone 121 m_cCalendar.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 122 123 // Timestep constant( SECOND, MINUTE, HOUR, DATE, MONTH, YEAR ) 124 m_iTimeToCount = iTimeToCount; 125 } 126 127 /** 128 * Set the given date. 129 * 130 * @override Never 131 * 132 * @param oTime a java.util.Date representing the date to be set 133 * 134 * @exception IllegalArgumentException if Parameter is not a java.util.Date 135 */ 136 public void setTime(Object oTime) throws IllegalArgumentException { 137 try { 138 // sets the Time as java.util.Date 139 m_cCalendar.setTime((java.util.Date)oTime); 140 } 141 catch (ClassCastException cce) { 142 throw new IllegalArgumentException("Parameter has to be of type java.util.Date."); 143 } 144 } 145 146 /** 147 * Set an int that will define which time field will be increased by {@link #goAhead}. 148 * 149 * @override Never 150 * 151 * @param iTime an int representing a constant 152 * 153 * @see #YEAR 154 * @see #MONTH 155 * @see #DATE 156 * @see #HOUR 157 * @see #MINUTE 158 * @see #SECOND 159 */ 160 public void setTimeToCount(int iTime) { 161 m_iTimeToCount = iTime; 162 } 163 164 /** 165 * Get the calendars date 166 * 167 * @override Never 168 * 169 * @return a java.util.Date representing the calendars date 170 */ 171 public Object getTime() { 172 return m_cCalendar.getTime(); 173 } 174 175 /** 176 * Increment the time by the given time interval. 177 * 178 * @override Never 179 * 180 * @param oInterval the interval by which to increment time. Must be an Integer object that gives the 181 * number of days by which to increment. 182 * 183 * @exception IllegalArgumentException if the given interval is no Integer or is null. 184 */ 185 public void goAhead(Object oInterval) throws IllegalArgumentException { 186 try { 187 m_cCalendar.add(m_iTimeToCount, ((Integer)oInterval).intValue()); 188 } 189 catch (NullPointerException npe) { 190 throw new IllegalArgumentException("Interval must be specified in calls to goAhead!"); 191 } 192 catch (ClassCastException cce) { 193 throw new IllegalArgumentException("Parameter has to be of type long"); 194 } 195 } 196 197 /** 198 * Get the default time interval. 199 * 200 * @override Never 201 * 202 * @return an Integer describing the default time step. 203 */ 204 public Object getDefaultInterval() { 205 return new Integer(m_iDefaultInterval); 206 } 207 208 /** 209 * Get the adapted Calendar. 210 * 211 * @override Never 212 * 213 * @return a Calendar object describing the calendar of this class. 214 */ 215 public Calendar getCalendar() { 216 return m_cCalendar; 217 } 218 219 /** 220 * Left-pad the given number with 0 to fill a two digit string. 221 * 222 * <p>Helper function used by {@link #getTimeStamp}.</p> 223 */ 224 private String getCorrectString(int iTime) { 225 if (Integer.toString(iTime).length() == 1) { 226 return ("0" + Integer.toString(iTime)); 227 } 228 229 return Integer.toString(iTime); 230 } 231 232 /** 233 * Create and return a time stamp. 234 * 235 * @override Never 236 * 237 * @param lStampNumber the number of the stamp 238 * 239 * @return a fresh time stamp. 240 */ 241 public Comparable getTimeStamp(long lStampNumber) { 242 String sReturn = ("0000000000000000000" + 243 Long.toString(lStampNumber)).substring(Long.toString(lStampNumber).length()); 244 245 sReturn = (getCorrectString(m_cCalendar.get(SECOND))) + sReturn; //second 246 sReturn = (getCorrectString(m_cCalendar.get(MINUTE))) + sReturn; //minute 247 sReturn = (getCorrectString(m_cCalendar.get(HOUR) + 248 m_cCalendar.get(Calendar.ZONE_OFFSET) / (1000 * 60 * 60))) + sReturn; //hour 249 sReturn = (getCorrectString(m_cCalendar.get(DATE))) + sReturn; //day 250 sReturn = (getCorrectString(m_cCalendar.get(MONTH) + 1)) + sReturn; //month 251 sReturn = (Integer.toString(m_cCalendar.get(YEAR))) + sReturn; //year 252 253 return sReturn; 254 } 255 256 /** 257 * @return a string representation of this date. 258 */ 259 public String toString() { 260 SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); 261 return sdf.format(m_cCalendar.getTime()); 262 } 263 264 /** 265 * Field number for {@link #setTimeToCount} indicates that the seconds will be increased by {@link #goAhead} 266 */ 267 public static int SECOND = Calendar.SECOND; 268 269 /** 270 * Field number for {@link #setTimeToCount} indicates that the minutes will be increased by {@link #goAhead} 271 */ 272 public static int MINUTE = Calendar.MINUTE; 273 274 /** 275 * Field number for {@link #setTimeToCount} indicates that the hours will be increased by {@link #goAhead} 276 */ 277 public static int HOUR = Calendar.HOUR_OF_DAY; 278 279 /** 280 * Field number for {@link #setTimeToCount} indicates that the days will be increased by {@link #goAhead} 281 */ 282 public static int DATE = Calendar.DATE; 283 284 /** 285 * Field number for {@link #setTimeToCount} indicates that the month will be increased by {@link #goAhead} 286 */ 287 public static int MONTH = Calendar.MONTH; 288 289 /** 290 * Field number for {@link #setTimeToCount} indicates that the years will be increased by {@link #goAhead} 291 */ 292 public static int YEAR = Calendar.YEAR; 293 }