1 package org.apache.lucene.document; 2 3 18 19 import java.util.Date ; 20 21 import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.RangeQuery; 24 44 public class DateField { 45 46 private DateField() {} 47 48 private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000, 50 Character.MAX_RADIX).length(); 51 52 public static String MIN_DATE_STRING() { 53 return timeToString(0); 54 } 55 56 public static String MAX_DATE_STRING() { 57 char[] buffer = new char[DATE_LEN]; 58 char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX); 59 for (int i = 0 ; i < DATE_LEN; i++) 60 buffer[i] = c; 61 return new String (buffer); 62 } 63 64 69 public static String dateToString(Date date) { 70 return timeToString(date.getTime()); 71 } 72 77 public static String timeToString(long time) { 78 if (time < 0) 79 throw new RuntimeException ("time '" + time + "' is too early, must be >= 0"); 80 81 String s = Long.toString(time, Character.MAX_RADIX); 82 83 if (s.length() > DATE_LEN) 84 throw new RuntimeException ("time '" + time + "' is too late, length of string " + 85 "representation must be <= " + DATE_LEN); 86 87 if (s.length() < DATE_LEN) { 89 StringBuffer sb = new StringBuffer (s); 90 while (sb.length() < DATE_LEN) 91 sb.insert(0, 0); 92 s = sb.toString(); 93 } 94 95 return s; 96 } 97 98 99 public static long stringToTime(String s) { 100 return Long.parseLong(s, Character.MAX_RADIX); 101 } 102 103 public static Date stringToDate(String s) { 104 return new Date (stringToTime(s)); 105 } 106 } 107 | Popular Tags |