1 package org.enhydra.snapper.wrapper.lucene; 2 3 6 7 import java.util.Date ; 8 9 28 public class DateField { 29 private DateField() {} 30 31 private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000, 33 Character.MAX_RADIX).length(); 34 35 public static String MIN_DATE_STRING() { 36 return timeToString(0); 37 } 38 39 public static String MAX_DATE_STRING() { 40 char[] buffer = new char[DATE_LEN]; 41 char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX); 42 for (int i = 0 ; i < DATE_LEN; i++) 43 buffer[i] = c; 44 return new String (buffer); 45 } 46 47 52 public static String dateToString(Date date) { 53 return timeToString(date.getTime()); 54 } 55 60 public static String timeToString(long time) { 61 if (time < 0) 62 throw new RuntimeException ("time too early"); 63 64 String s = Long.toString(time, Character.MAX_RADIX); 65 66 if (s.length() > DATE_LEN) 67 throw new RuntimeException ("time too late"); 68 69 if (s.length() < DATE_LEN) { 71 StringBuffer sb = new StringBuffer (s); 72 while (sb.length() < DATE_LEN) 73 sb.insert(0, 0); 74 s = sb.toString(); 75 } 76 77 return s; 78 } 79 80 81 public static long stringToTime(String s) { 82 return Long.parseLong(s, Character.MAX_RADIX); 83 } 84 85 public static Date stringToDate(String s) { 86 return new Date (stringToTime(s)); 87 } 88 } 89 90 | Popular Tags |