KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > DynamicDate


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11 import org.mmbase.util.dateparser.*;
12 import java.util.*;
13
14 /**
15  * A DynamicDate is a Date object that has no fixed value, like 'now'. It is unmodifiable, so all
16  * set-methods throw exceptions. There is no public constructor, but a public static {@link #getInstance}.
17  *
18  * Sadly, the Date object of Sun is implemented using private static methods which use private
19  * fields, of the Date object so not everything could be overridden perfectly. So, if e.g. a dynamic
20  * date could be an argument of a 'after' or 'before' method, it is better to wrap it with {@link
21  * DynamicDate#eval} first.
22  *
23  * @author Michiel Meeuwissen
24  * @since MMBase-1.8
25  */

26 public class DynamicDate extends Date {
27
28
29     /**
30      * Parses a format string and returns Date instance, possibly a 'dynamic one'. Not necessary a new one, which
31      * does not matter, because these objects are unmodifiable anyway.
32      *
33      * If the request date is not dynamic, but absolutely defined, a normal Date object is returned.
34      */

35     public static Date getInstance(final String JavaDoc format) throws ParseException {
36         if (format.equals("null")) return null;
37         DateParser parser = new DateParser(new java.io.StringReader JavaDoc(format));
38         try {
39             parser.start();
40             if (parser.dynamic()) {
41                 return new DynamicDate(format);
42             } else {
43                 return parser.toDate();
44             }
45         } catch (ParseException pe) {
46             ParseException p = new ParseException("In " + format + " " + pe.getMessage());
47             p.initCause(pe);
48             throw p;
49         }
50
51     }
52
53     /**
54      * Makes sure the argument 'date' is no DynamicDate any more. So this returns a fixed date
55      * object when the argument is a DynamicDate and simply the argument if it is not.
56      */

57     public static Date eval(final Date date) {
58         if (date instanceof DynamicDate) {
59             return ((DynamicDate) date).evalDate();
60         } else {
61             return date;
62         }
63     }
64
65     /**
66      * The original string by which this instance was gotten.
67      */

68     protected final String JavaDoc date;
69
70     protected DynamicDate(String JavaDoc d) {
71         date = d;
72     }
73
74     public String JavaDoc getFormat() {
75         return date;
76     }
77
78     /**
79      * This produces a normal Date object, and is called everytime when that is needed. Users can call it too, if they want to fixated
80      */

81     protected Date evalDate() {
82         DateParser parser = new DateParser(new java.io.StringReader JavaDoc(date));
83         try {
84             parser.start();
85             return parser.toDate();
86         } catch (org.mmbase.util.dateparser.ParseException pe) {
87             return new Date();
88         }
89     }
90
91
92     // all methods of Date itself are simply wrapped..
93

94     public boolean after(Date when) {
95         return evalDate().after(when);
96     }
97
98     public boolean before(Date when) {
99         return evalDate().before(when);
100     }
101
102     public Object JavaDoc clone() {
103         try {
104             return getInstance(date);
105         } catch (org.mmbase.util.dateparser.ParseException pe) {
106             return new Date();
107         }
108     }
109     public int compareTo(Date anotherDate) {
110         return evalDate().compareTo(anotherDate);
111     }
112
113     public boolean equals(Object JavaDoc obj) {
114         if (obj instanceof DynamicDate) {
115             return date.equals(((DynamicDate)obj).date);
116         } else {
117             return false;
118         }
119     }
120     public int getDate() {
121         return evalDate().getDate();
122     }
123     public int getDay() {
124         return evalDate().getDay();
125     }
126     public int getHours() {
127         return evalDate().getHours();
128     }
129     public int getMinutes() {
130         return evalDate().getMinutes();
131     }
132     public int getMonth() {
133         return evalDate().getMonth();
134     }
135
136     public int getSeconds() {
137         return evalDate().getSeconds();
138     }
139     public long getTime() {
140         return evalDate().getTime();
141     }
142     public int getTimezoneOffset() {
143         return evalDate().getTimezoneOffset();
144     }
145     public int getYear() {
146         return evalDate().getYear();
147     }
148     public int hashCode() {
149         return date.hashCode();
150     }
151     public void setDate(int date) {
152         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
153     }
154     public void setHours(int hours) {
155         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
156     }
157     public void setMinutes(int minutes) {
158         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
159     }
160     public void setMonth(int month) {
161         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
162     }
163
164     public void setSeconds(int seconds) {
165         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
166     }
167     public void setTime(long time) {
168         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
169     }
170     public void setYear(int year) {
171         throw new UnsupportedOperationException JavaDoc("Cannot set date in dynamic date");
172     }
173     public String JavaDoc toGMTString() {
174         return evalDate().toGMTString();
175     }
176     public String JavaDoc toLocaleString() {
177         return evalDate().toLocaleString();
178     }
179
180     public String JavaDoc toString() {
181         return date + ": " + evalDate().toString();
182     }
183
184     public static String JavaDoc[] getDemo() {
185         return new String JavaDoc[] {
186             "0", "10000", "-10000", "+1000", // just numbers a bit after 1970, a bit before
187
"1973-05-03", "2006-05-09", "-3-12-25", // absolute dates
188
"2000-01-01 16:00", "TZUTC 2001-01-01 16:00","today 12:34:56.789",
189             "now", "today", "tomorrow", "now + 10 minute", "today + 5 day",
190             "now this year", "next august", "today + 6 month next august", "tomonth", "borreltijd", "today + 5 dayish", "yesteryear", "mondayish",
191             "duration + 5 minute", "duration + 100 year",
192             "TZUTC today noon", "TZEurope/Amsterdam today noon", "TZUTC today", "TZEurope/Amsterdam today",
193             "TZ UTC today noon", "TZ Europe/Amsterdam today noon", "TZ UTC today", "TZ Europe/Amsterdam today",
194             "TZ Europe/Amsterdam -1000",
195             "today 6 oclock", "today 23 oclock", "today 43 oclock",
196             "tosecond", "tominute", "tohour", "today", "previous monday", "tomonth", "toyear", "tocentury", "tocentury_pedantic", "toera",
197             "now this second", "now this minute", "now this hour", "now this day", "today previous monday", "now this month", "now this year", "now this century", "now this era",
198             "now - 15 year this century", "now - 20 year this century_pedantic", "today + 2 century", "toera - 1 minute",
199             "this july", "previous july", "next july", "this sunday", "previous sunday", "next sunday",
200             "2009-W01-01", "2009-W53-7", "2006-123"
201         };
202     }
203
204
205     public static void main(String JavaDoc argv[]) throws java.text.ParseException JavaDoc, ParseException {
206
207         //System.out.println("" + Arrays.asList(TimeZone.getAvailableIDs()));
208
//System.out.println(TimeZone.getDefault());
209
java.text.DateFormat JavaDoc formatter = new java.text.SimpleDateFormat JavaDoc("GGGG yyyy-MM-dd HH:mm:ss.SSS zzz E");
210         if (argv.length == 0) {
211             String JavaDoc[] demo = getDemo();
212             for (int i = 0; i < demo.length; i++) {
213                 try {
214                     Date d1 = getInstance(demo[i]);
215                     System.out.print(formatter.format(d1) + "\t");
216                 } catch (Exception JavaDoc e) {
217                     System.out.println(e.getMessage());
218                 }
219                 System.out.println(demo[i]);
220
221             }
222             System.out.println("This was demo, you can also call with an argument, to try it yourself");
223             System.out.println("Also try with different values for -Duser.timezone=");
224         } else {
225             Date d1 = getInstance(argv[0]);
226             //Date d2 = Casting.ISO_8601_UTC.parse(argv[0]);
227
//Date d3 = new Date(Long.MIN_VALUE);
228
System.out.println(formatter.format(d1) + " " + d1.getTime());
229             //System.out.println("" + d2 + " " + d2.getTime());
230
//System.out.println("" + d3 + " " + d3.getTime());
231
}
232     }
233
234 }
235
236
237
238
Popular Tags