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