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