1 package org.apache.lucene.document; 2 3 18 19 import java.text.ParseException ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.TimeZone ; 24 25 39 public class DateTools { 40 41 private final static TimeZone GMT = TimeZone.getTimeZone("GMT"); 42 43 private DateTools() {} 44 45 54 public static String dateToString(Date date, Resolution resolution) { 55 return timeToString(date.getTime(), resolution); 56 } 57 58 67 public static String timeToString(long time, Resolution resolution) { 68 Calendar cal = Calendar.getInstance(GMT); 69 70 73 cal.setTime(new Date (round(time, resolution))); 74 75 SimpleDateFormat sdf = new SimpleDateFormat (); 76 sdf.setTimeZone(GMT); 77 String pattern = null; 78 if (resolution == Resolution.YEAR) { 79 pattern = "yyyy"; 80 } else if (resolution == Resolution.MONTH) { 81 pattern = "yyyyMM"; 82 } else if (resolution == Resolution.DAY) { 83 pattern = "yyyyMMdd"; 84 } else if (resolution == Resolution.HOUR) { 85 pattern = "yyyyMMddHH"; 86 } else if (resolution == Resolution.MINUTE) { 87 pattern = "yyyyMMddHHmm"; 88 } else if (resolution == Resolution.SECOND) { 89 pattern = "yyyyMMddHHmmss"; 90 } else if (resolution == Resolution.MILLISECOND) { 91 pattern = "yyyyMMddHHmmssSSS"; 92 } else { 93 throw new IllegalArgumentException ("unknown resolution " + resolution); 94 } 95 sdf.applyPattern(pattern); 96 return sdf.format(cal.getTime()); 97 } 98 99 109 public static long stringToTime(String dateString) throws ParseException { 110 return stringToDate(dateString).getTime(); 111 } 112 113 123 public static Date stringToDate(String dateString) throws ParseException { 124 String pattern = null; 125 if (dateString.length() == 4 ) 126 pattern = "yyyy"; 127 else if (dateString.length() == 6 ) 128 pattern = "yyyyMM"; 129 else if (dateString.length() == 8 ) 130 pattern = "yyyyMMdd"; 131 else if (dateString.length() == 10 ) 132 pattern = "yyyyMMddHH"; 133 else if (dateString.length() == 12 ) 134 pattern = "yyyyMMddHHmm"; 135 else if (dateString.length() == 14 ) 136 pattern = "yyyyMMddHHmmss"; 137 else if (dateString.length() == 17 ) 138 pattern = "yyyyMMddHHmmssSSS"; 139 else 140 throw new ParseException ("Input is not valid date string: " + dateString, 0); 141 SimpleDateFormat sdf = new SimpleDateFormat (pattern); 142 sdf.setTimeZone(GMT); 143 Date date = sdf.parse(dateString); 144 return date; 145 } 146 147 156 public static Date round(Date date, Resolution resolution) { 157 return new Date (round(date.getTime(), resolution)); 158 } 159 160 170 public static long round(long time, Resolution resolution) { 171 Calendar cal = Calendar.getInstance(GMT); 172 173 176 cal.setTime(new Date (time)); 177 178 if (resolution == Resolution.YEAR) { 179 cal.set(Calendar.MONTH, 0); 180 cal.set(Calendar.DAY_OF_MONTH, 1); 181 cal.set(Calendar.HOUR_OF_DAY, 0); 182 cal.set(Calendar.MINUTE, 0); 183 cal.set(Calendar.SECOND, 0); 184 cal.set(Calendar.MILLISECOND, 0); 185 } else if (resolution == Resolution.MONTH) { 186 cal.set(Calendar.DAY_OF_MONTH, 1); 187 cal.set(Calendar.HOUR_OF_DAY, 0); 188 cal.set(Calendar.MINUTE, 0); 189 cal.set(Calendar.SECOND, 0); 190 cal.set(Calendar.MILLISECOND, 0); 191 } else if (resolution == Resolution.DAY) { 192 cal.set(Calendar.HOUR_OF_DAY, 0); 193 cal.set(Calendar.MINUTE, 0); 194 cal.set(Calendar.SECOND, 0); 195 cal.set(Calendar.MILLISECOND, 0); 196 } else if (resolution == Resolution.HOUR) { 197 cal.set(Calendar.MINUTE, 0); 198 cal.set(Calendar.SECOND, 0); 199 cal.set(Calendar.MILLISECOND, 0); 200 } else if (resolution == Resolution.MINUTE) { 201 cal.set(Calendar.SECOND, 0); 202 cal.set(Calendar.MILLISECOND, 0); 203 } else if (resolution == Resolution.SECOND) { 204 cal.set(Calendar.MILLISECOND, 0); 205 } else if (resolution == Resolution.MILLISECOND) { 206 } else { 208 throw new IllegalArgumentException ("unknown resolution " + resolution); 209 } 210 return cal.getTime().getTime(); 211 } 212 213 214 public static class Resolution { 215 216 public static final Resolution YEAR = new Resolution("year"); 217 public static final Resolution MONTH = new Resolution("month"); 218 public static final Resolution DAY = new Resolution("day"); 219 public static final Resolution HOUR = new Resolution("hour"); 220 public static final Resolution MINUTE = new Resolution("minute"); 221 public static final Resolution SECOND = new Resolution("second"); 222 public static final Resolution MILLISECOND = new Resolution("millisecond"); 223 224 private String resolution; 225 226 private Resolution() { 227 } 228 229 private Resolution(String resolution) { 230 this.resolution = resolution; 231 } 232 233 public String toString() { 234 return resolution; 235 } 236 237 } 238 239 } 240 | Popular Tags |