1 56 57 59 package org.jdom.contrib.beans; 60 61 import java.util.*; 62 import org.apache.regexp.*; 63 import java.text.*; 64 import java.io.PrintStream ; 65 66 69 public class DateUtils { 70 71 public static boolean debug; 72 73 80 public static Date parseDate(String s) { 81 Date date = null; 82 83 try { 85 date = new Date(s); 88 return date; 89 } 90 catch (IllegalArgumentException dfe) { 91 } 92 93 try { 95 date = DateFormat.getDateInstance().parse(s); 96 return date; 97 } 98 catch (ParseException pe) { 99 } 100 101 try { 103 long secs = Long.parseLong(s); 104 date = new Date(s); 105 return date; 106 } 107 catch (NumberFormatException nfe) { 108 } 109 110 ISO8601 iso = parseISO8601(s); 111 if (iso != null) { 112 TimeZone tz = null; 113 124 125 Calendar cal; 126 if (tz == null) 127 cal = Calendar.getInstance(); 128 else 129 cal = Calendar.getInstance(tz); 130 131 cal.set(Calendar.YEAR, iso.year); 132 cal.set(Calendar.MONTH, iso.month - 1); 133 cal.set(Calendar.DAY_OF_MONTH, iso.day); 134 cal.set(Calendar.HOUR, iso.hour + 12); cal.set(Calendar.MINUTE, iso.min); 136 cal.set(Calendar.SECOND, iso.sec); 137 138 return cal.getTime(); 140 141 } 143 return null; 144 } 146 public static class ISO8601 { 147 public int year; 148 public int month; 149 public int day; 150 public int hour; 151 public int min; 152 public int sec; 153 public int frac; 154 public String tz; 155 } 156 157 protected static String reISO8601 = 158 "(\\d\\d\\d\\d)(-(\\d\\d)(-(\\d\\d))?)?" + 159 "([T| ]?" + 160 "(\\d\\d):(\\d\\d)(:((\\d\\d)(\\.(\\d+))?)?)?" + 161 "(Z|([+-]\\d\\d:\\d\\d)|([A-Z]{3}))?)?"; 162 163 public static ISO8601 parseISO8601(String s) { 164 try { 168 RE re = new RE(reISO8601); 169 if (re.match(s)) { 170 if (debug) 171 showParens(re); 172 173 ISO8601 iso = new ISO8601(); 174 iso.year = toInt(re.getParen(1)); 175 iso.month = toInt(re.getParen(3)); 176 iso.day = toInt(re.getParen(5)); 177 iso.hour = toInt(re.getParen(7)); 178 iso.min = toInt(re.getParen(8)); 179 iso.sec = toInt(re.getParen(11)); 180 iso.frac = toInt(re.getParen(13)); 181 iso.tz = re.getParen(14); 182 183 if (debug) { 184 System.out.println("year='" + iso.year + "'"); 185 System.out.println("month='" + iso.month + "'"); 186 System.out.println("day='" + iso.day + "'"); 187 System.out.println("hour='" + iso.hour + "'"); 188 System.out.println("min='" + iso.min + "'"); 189 System.out.println("sec='" + iso.sec + "'"); 190 System.out.println("frac='" + iso.frac + "'"); 191 System.out.println("tz='" + iso.tz + "'"); 192 } 193 194 return iso; 195 } 196 } catch (RESyntaxException ree) { 198 ree.printStackTrace(); 199 } 200 return null; 201 } 202 203 public static int toInt(String x) { 204 if (x == null) return 0; 205 try { 206 return Integer.parseInt(x); 207 } 208 catch (NumberFormatException e) { 209 return 0; 210 } 211 } 212 213 217 static void showParens(RE r) 218 { 219 for (int i = 0; i < r.getParenCount(); i++) 221 { 222 System.out.println("$" + i + " = " + r.getParen(i)); 224 } 225 } 226 227 public static void main(String [] args) { 228 debug = true; 229 for (int i=0; i<args.length; ++i) { 230 System.out.println( parseDate(args[i]) ); 231 } 232 } 233 } 234 | Popular Tags |