1 36 package org.columba.ristretto.parser; 37 38 import java.util.Date ; 39 import java.util.TimeZone ; 40 import java.util.regex.Matcher ; 41 import java.util.regex.Pattern ; 42 43 48 public class DateParser { 49 private static final long[] monthOffset = { 0l, 2678400000l, 5097600000l, 7776000000l, 10368000000l, 13046400000l, 15638400000l, 18316800000l, 20995200000l, 23587200000l, 26265600000l, 28857600000l }; 62 63 private static final Pattern stringPattern = Pattern 64 .compile("(\\w{3})\\w*"); 65 66 private static final Pattern numberPattern = Pattern 67 .compile("(\\d{1,4})"); 68 69 private static final Pattern timePattern = Pattern 70 .compile("(\\d\\d):(\\d\\d)(:(\\d\\d))?"); 71 72 private static final Pattern timezonePattern = Pattern 73 .compile("((\\+|-)(\\d\\d)(\\d\\d))|(\\\"?(\\w+)\\\"?)"); 74 75 private DateParser() { 76 } 77 78 private static int getMonth(String month) { 79 String loweredMonth = month.toLowerCase(); 80 char startChar = loweredMonth.charAt(0); 81 82 switch (startChar) { 83 case 'j': { 84 if (loweredMonth.equals("jan")) 85 return 0; 86 if (loweredMonth.equals("jun")) 87 return 5; 88 if (loweredMonth.equals("jul")) 89 return 6; 90 } 91 case 'f': { 92 if (loweredMonth.equals("feb")) 93 return 1; 94 } 95 case 'm': { 96 if (loweredMonth.equals("mar")) 97 return 2; 98 if (loweredMonth.equals("may")) 99 return 4; 100 } 101 case 'a': { 102 if (loweredMonth.equals("apr")) 103 return 3; 104 if (loweredMonth.equals("aug")) 105 return 7; 106 } 107 case 's': { 108 if (loweredMonth.equals("sep")) 109 return 8; 110 } 111 case 'o': { 112 if (loweredMonth.equals("oct")) 113 return 9; 114 } 115 case 'n': { 116 if (loweredMonth.equals("nov")) 117 return 10; 118 } 119 case 'd': { 120 if (loweredMonth.equals("dec")) 121 return 11; 122 } 123 } 124 125 return -1; 126 } 127 128 private static long getLeapYearCorrection(int day, int month, int year) { 129 int normalizedYear = year - 1972; 130 if (day <= 29 && month < 2) 131 normalizedYear -= 1; 132 133 int leapYears = normalizedYear / 4; 134 135 return 86400000l * leapYears; 136 } 137 138 146 public static Date parse(CharSequence dateString) throws ParserException { 147 String temp = dateString.toString(); 148 149 Matcher matcher; 150 151 matcher = stringPattern.matcher(temp); 153 int month = -1; 154 while (month == -1 && matcher.find() ) { 155 month = getMonth(matcher.group(1)); 156 temp = temp.substring(0,matcher.start()) + temp.substring(matcher.end()); 157 158 matcher = stringPattern.matcher(temp); 159 } 160 if (month == -1) 161 throw new ParserException("Invalid Date: " + dateString); 162 163 164 matcher = timePattern.matcher(temp); 165 if (!matcher.find()) 166 throw new ParserException("Invalid Date: " + dateString); 167 temp = temp.substring(0,matcher.start()) + temp.substring(matcher.end()); 168 169 int hours = Integer.parseInt(matcher.group(1)); 171 int minutes = Integer.parseInt(matcher.group(2)); 172 int seconds = 0; 173 if (matcher.group(3) != null) { 174 seconds = Integer.parseInt(matcher.group(4)); 175 } 176 177 178 matcher = numberPattern.matcher(temp); 180 181 if (!matcher.find()) 182 throw new ParserException("Invalid Date: " + dateString); 183 int day = Integer.parseInt(matcher.group()); 184 temp = temp.substring(0,matcher.start()) + temp.substring(matcher.end()); 185 186 matcher = numberPattern.matcher(temp); 187 if (!matcher.find()) 188 throw new ParserException("Invalid Date: " + dateString); 189 int year = Integer.parseInt(matcher.group()); 190 if (year < 99) { 191 if (year < 49) 192 year += 2000; 193 else 194 year += 1900; 195 } 196 temp = temp.substring(0,matcher.start()) + temp.substring(matcher.end()); 197 198 long date = seconds * 1000l + minutes * 60000l + hours * 3600000l + day 200 * 86400000l + monthOffset[month] + (year - 1970) * 31536000000l 201 + getLeapYearCorrection(day, month, year); 202 203 matcher = timezonePattern.matcher(temp); 205 long zoneoffset = 0; 206 if (matcher.find()) { 207 if (matcher.group(1) != null) { 208 zoneoffset = Integer.parseInt(matcher.group(4)) * 60000l 209 + Integer.parseInt(matcher.group(3)) * 3600000l; 210 if (matcher.group(2).equals("+")) { 211 zoneoffset = -zoneoffset; 212 } 213 } else if (matcher.group(6) != null) { 214 TimeZone timezone = TimeZone.getTimeZone(matcher.group(6)); 215 zoneoffset = timezone.getOffset(date); 216 } 217 } 218 date += zoneoffset; 219 220 return new Date (date); 222 } 223 } 224 | Popular Tags |