001    package sale;
002    
003    /**
004     * This class is an implementation of the {@link Time Time} interface.
005     *
006     * <p>The time is represented in the form of a simple date. The format is &quot;dd.mm.yy&quot;.</p>
007     *
008     * <p><strong>Note:</strong> This is a very simple implementation and it is <b>not</b> Y2K safe!</p>
009     *
010     * @author Sven Matznick
011     * @version 2.0 15/06/1999
012     * @since v2.0
013     *
014     * @deprecated As this class is not Y2K safe, it is of no use any more. Use CalendarTime instead.
015     */
016    public class Date extends Object implements Time {
017    
018        /**
019             * ID for serialization.
020             */
021            private static final long serialVersionUID = 3597919751511519481L;
022    
023            /**
024         * The current date.
025         *
026         * @serial
027         */
028        private String m_sDate;
029    
030        /**
031         * The default interval for Dates.
032         *
033         * @serial
034         */
035        private String m_sDefaultInterval = new String("1");
036    
037        /**
038         * Is this a special year?
039         *
040         * @serial
041         */
042        private boolean m_fSpecialYear = true;
043    
044        /**
045         * Creates a new date with standard starting time &quot;01.01.00&quot;.
046         */
047        public Date() {
048            this("01.01.00");
049        }
050    
051        /**
052         * Creates a new date with the given starting time.
053         *
054         * @param sNewDate a String representing the date to be set, format: dd.mm.yy (day.month.year)
055         *
056         * @exception IllegalArgumentException if the given String has an invalid format
057         */
058        public Date(String sNewDate) throws IllegalArgumentException {
059            super();
060            setTime(sNewDate);
061        }
062    
063        /**
064         * Set the given date.
065         *
066         * @override Never
067         *
068         * @param oTime a String representing the date to be set, format: dd.mm.yy (day.month.year)
069         *
070         * @exception IllegalArgumentException if the given String has an invalid format
071         */
072        public void setTime(Object oTime) throws IllegalArgumentException {
073            validate(oTime);
074            m_sDate = (String)oTime;
075            long lHelp = Long.parseLong(m_sDate.substring(6, 8));
076            if (lHelp % 4 == 0) {
077                m_fSpecialYear = true;
078            } else {
079                m_fSpecialYear = false;
080            }
081        }
082    
083        /**
084         * Get the current date.
085         *
086         * @override Never
087         *
088         * @return a String representing the current date
089         */
090        public Object getTime() {
091            return (m_sDate);
092        }
093    
094        /**
095         * Increment the time by the given time interval.
096         *
097         * @override Never
098         *
099         * @param oInterval the interval to increment time. Must be a String that gives the number of days by which
100         * to increment.
101         *
102         * @exception IllegalArgumentException if the given interval is no String or cannot be transformed into a
103         * long value.
104         */
105        public void goAhead(Object oInterval) throws IllegalArgumentException {
106            if (!(oInterval instanceof String)) {
107                throw new IllegalArgumentException("Parameter has to be of type String.");
108            }
109            long lInterval = Long.parseLong((String)oInterval); // transform String to long
110            long lDay = Long.parseLong(m_sDate.substring(0, 2));
111            long lMonth = Long.parseLong(m_sDate.substring(3, 5)); //months count from 0
112            long lYear = Long.parseLong(m_sDate.substring(6, 8));
113    
114            lDay += lInterval; // increase days
115    
116            // now correct overflown days
117            while ((((lMonth == 1) || (lMonth == 3) || (lMonth == 5) || (lMonth == 7) || (lMonth == 8) ||
118                    (lMonth == 10) || (lMonth == 12)) && (lDay > 31)) || (((lMonth == 4) || (lMonth == 6) ||
119                    (lMonth == 9) || (lMonth == 11)) && (lDay > 30)) ||
120                    ((lMonth == 2) && ((lDay > 28) && (!m_fSpecialYear))) ||
121                    ((lMonth == 2) && ((lDay > 29) && (m_fSpecialYear)))) { // still more days than this month has ?
122    
123                if (((lMonth == 1) || (lMonth == 3) || (lMonth == 5) || (lMonth == 7) || (lMonth == 8) ||
124                        (lMonth == 10) || (lMonth == 12)) && (lDay > 31)) { // 31 day month ?
125                    lDay -= 31; // okay, decrease
126                    lMonth++; // next month
127                }
128    
129                if (((lMonth == 4) || (lMonth == 6) || (lMonth == 9) || (lMonth == 11)) && (lDay > 30)) { // 30 day month ?
130                    lDay -= 30; // okay, decrease
131                    lMonth++; // next month
132                }
133    
134                if ((lMonth == 2) && (lDay > 28)) { // february ?
135                    if (!m_fSpecialYear) {
136                        lDay -= 28; // okay, descrease
137                        lMonth++; // next month
138                    } else {
139                        if (lDay > 29) {
140                            lDay -= 29;
141                            lMonth++; // next month
142                        }
143                    }
144                }
145    
146                if (lMonth > 12) { // more than 12 month ?
147                    long i = 0;
148                    while (lMonth > 12) {
149                        lMonth -= 12;
150                        i++; // how many years will pass ?
151                    }
152                    lYear += i; ; // increase years
153                    if (lYear > 99) {
154                        lYear = 0;
155                    } // y2k bug ? no ! well, yes, but we don't care.
156                    if (lYear % 4 == 0) {
157                        m_fSpecialYear = true;
158                    } else {
159                        m_fSpecialYear = false;
160                    }
161                }
162            }
163    
164            // Build new String representation:
165            String sHelp = "";
166            if (lDay < 10) {
167                sHelp = "0" + Long.toString(lDay) + ".";
168            } else {
169                sHelp = Long.toString(lDay) + ".";
170            }
171            if (lMonth < 10) {
172                sHelp = sHelp + "0" + Long.toString(lMonth) + ".";
173            } else {
174                sHelp = sHelp + Long.toString(lMonth) + ".";
175            }
176            if (lYear < 10) {
177                sHelp = sHelp + "0" + Long.toString(lYear);
178            } else {
179                sHelp = sHelp + Long.toString(lYear);
180            }
181    
182            m_sDate = new String(sHelp); // set new time
183        }
184    
185        /**
186         * Get the default time interval.
187         *
188         * @override Never
189         *
190         * @return a String describing the default time step in days (standard: &quot;1&quot;).
191         */
192        public Object getDefaultInterval() {
193            return (m_sDefaultInterval);
194        }
195    
196        /**
197         * Create and return a time stamp.
198         *
199         * @override Never
200         *
201         * @param lStampNumber the number of the stamp
202         *
203         * @return a fresh time stamp.
204         */
205        public Comparable<String> getTimeStamp(long lStampNumber) {
206            java.util.StringTokenizer stDate = new java.util.StringTokenizer(m_sDate, ".");
207    
208            String sReturn = ("000000000" +
209                    Long.toString(lStampNumber)).substring(Long.toString(lStampNumber).length());
210            //number of stamp in this intervall
211    
212            sReturn = stDate.nextToken() + sReturn; //day
213            sReturn = stDate.nextToken() + sReturn; //month
214            sReturn = stDate.nextToken() + sReturn; //year
215    
216            return sReturn;
217        }
218    
219        /**
220         * Returns the current date.
221         *
222         * @override Sometimes
223         *
224         * @return a String describing the current date
225         */
226        public String toString() {
227            return m_sDate;
228        }
229    
230        /**
231         * Validate a given String, i.e. check it represents a date.
232         *
233         * @override Never
234         */
235        private void validate(Object oToValidate) throws IllegalArgumentException {
236            if (!(oToValidate instanceof String)) {
237                throw new IllegalArgumentException("Parameter has to be of type String.");
238            }
239            if (!(((String)oToValidate).length() == 8)) {
240                throw new IllegalArgumentException("Parameter has to consist of 8 chars: dd.mm.yy");
241            }
242            java.util.StringTokenizer stToValidate = new java.util.StringTokenizer((String)oToValidate, ".");
243    
244            String sDay = stToValidate.nextToken();
245            if (sDay.length() != 2) {
246                throw new IllegalArgumentException("Parameter has to consist of 8 chars: dd.mm.yy");
247            }
248    
249            String sMonth = stToValidate.nextToken();
250            if (sMonth.length() != 2) {
251                throw new IllegalArgumentException("Parameter has to consist of 8 chars: dd.mm.yy");
252            }
253    
254            String sYear = stToValidate.nextToken();
255            if (sYear.length() != 2) {
256                throw new IllegalArgumentException("Parameter has to consist of 8 chars: dd.mm.yy");
257            }
258    
259            long lDay = Long.parseLong(sDay); // throws IllegalArgumentException if neccessary
260            long lMonth = Long.parseLong(sMonth); // throws IllegalArgumentException if neccessary
261            long lYear = Long.parseLong(sYear); // throws IllegalArgumentException if neccessary
262    
263            if ((lMonth > 12) || (lMonth < 1)) {
264                throw new IllegalArgumentException("Month has to be between 1 and 12");
265            }
266            switch ((int)lMonth) {
267                case 1:
268                case 3:
269                case 5:
270                case 7:
271                case 8:
272                case 10:
273                case 12:
274                    if ((lDay > 31) || (lDay < 1)) {
275                        throw new IllegalArgumentException("Day has to be between 1 and 31");
276                    }
277                    break;
278                case 4:
279                case 6:
280                case 9:
281                case 11:
282                    if ((lDay > 30) || (lDay < 1)) {
283                        throw new IllegalArgumentException("Day has to be between 1 and 30");
284                    }
285                    break;
286                default: // case 2
287                    if (lYear % 4 == 0 && (lDay > 29 || lDay < 1)) {
288                        throw new IllegalArgumentException("Day has to be between 1 and 29");
289                    }
290                    if (lYear % 4 != 0 && (lDay > 28 || lDay < 1)) {
291                        throw new IllegalArgumentException("Day has to be between 1 and 28");
292                    }
293            }
294        }
295    
296        /** Convert the date to a {@link java.util.Date} object.
297         * <B>Note:</B> As the entire sale.Date class is not Y2K compliant, this method also is not.
298         *
299         * @return The converted date.
300         * @since v2.0 (06/07/2000)
301         */
302        public java.util.Date getDateValue() {
303            try {
304                java.util.StringTokenizer stToValidate = new java.util.StringTokenizer(m_sDate, ".");
305    
306                String sDay = stToValidate.nextToken();
307                String sMonth = stToValidate.nextToken();
308                String sYear = stToValidate.nextToken();
309    
310                long lDay = Long.parseLong(sDay); // throws IllegalArgumentException if neccessary
311                long lMonth = Long.parseLong(sMonth) - 1; // throws IllegalArgumentException if neccessary
312                long lYear = Long.parseLong(sYear); // throws IllegalArgumentException if neccessary
313    
314                java.util.Calendar c = java.util.Calendar.getInstance();
315                c.clear();
316                c.set((int)lYear + 1900, (int)lMonth, (int)lDay);
317    
318                return c.getTime();
319            }
320            catch (IllegalArgumentException iae) {
321                return null;
322            }
323        }
324    }