1 10 package org.mmbase.util; 11 import org.mmbase.util.dateparser.*; 12 import java.util.*; 13 14 26 public class DynamicDate extends Date { 27 28 29 35 public static Date getInstance(final String format) throws ParseException { 36 if (format.equals("null")) return null; 37 DateParser parser = new DateParser(new java.io.StringReader (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 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 68 protected final String date; 69 70 protected DynamicDate(String d) { 71 date = d; 72 } 73 74 public String getFormat() { 75 return date; 76 } 77 78 81 protected Date evalDate() { 82 DateParser parser = new DateParser(new java.io.StringReader (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 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 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 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 ("Cannot set date in dynamic date"); 153 } 154 public void setHours(int hours) { 155 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 156 } 157 public void setMinutes(int minutes) { 158 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 159 } 160 public void setMonth(int month) { 161 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 162 } 163 164 public void setSeconds(int seconds) { 165 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 166 } 167 public void setTime(long time) { 168 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 169 } 170 public void setYear(int year) { 171 throw new UnsupportedOperationException ("Cannot set date in dynamic date"); 172 } 173 public String toGMTString() { 174 return evalDate().toGMTString(); 175 } 176 public String toLocaleString() { 177 return evalDate().toLocaleString(); 178 } 179 180 public String toString() { 181 return date + ": " + evalDate().toString(); 182 } 183 184 public static String [] getDemo() { 185 return new String [] { 186 "0", "10000", "-10000", "+1000", "1973-05-03", "2006-05-09", "-3-12-25", "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 argv[]) throws java.text.ParseException , ParseException { 206 207 java.text.DateFormat formatter = new java.text.SimpleDateFormat ("GGGG yyyy-MM-dd HH:mm:ss.SSS zzz E"); 210 if (argv.length == 0) { 211 String [] 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 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 System.out.println(formatter.format(d1) + " " + d1.getTime()); 229 } 232 } 233 234 } 235 236 237 238 | Popular Tags |