1 package prefuse.util; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import java.util.GregorianCalendar ; 7 8 16 public class TimeLib { 17 18 19 public static final int MILLENIUM = -1000; 20 21 public static final int CENTURY = -100; 22 23 public static final int DECADE = -10; 24 25 private static final double SECOND_MILLIS = 1000; 26 private static final double MINUTE_MILLIS = SECOND_MILLIS*60; 27 private static final double HOUR_MILLIS = MINUTE_MILLIS*60; 28 private static final double DAY_MILLIS = HOUR_MILLIS * 24.0015; 29 private static final double WEEK_MILLIS = DAY_MILLIS * 7; 30 private static final double MONTH_MILLIS = DAY_MILLIS * 30.43675; 31 private static final double YEAR_MILLIS = WEEK_MILLIS * 52.2; 32 private static final double DECADE_MILLIS = YEAR_MILLIS * 10; 33 private static final double CENTURY_MILLIS = DECADE_MILLIS * 10; 34 private static final double MILLENIUM_MILLIS = CENTURY_MILLIS * 10; 35 36 private static final int[] CALENDAR_FIELDS = { 37 Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR_OF_DAY, 38 Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND 39 }; 40 41 private TimeLib() { 42 } 44 45 54 public static int getUnitsBetween(long t0, long t1, int field) { 55 boolean negative = false; 56 if ( t1 < t0 ) { 57 long tmp = t1; t1 = t0; t0 = tmp; negative = true; 59 } 60 GregorianCalendar gc1 = new GregorianCalendar (); 61 GregorianCalendar gc2 = new GregorianCalendar (); 62 gc1.setTimeInMillis(t0); 63 gc2.setTimeInMillis(t1); 64 65 int est = estimateUnitsBetween(t0, t1, field); 68 boolean multiYear = isMultiYear(field); 69 if ( multiYear ) { 70 gc1.add(Calendar.YEAR, -field*(est-2)); 71 est = -field*est; 72 } else { 73 gc1.add(field, est-2); 74 } 75 int f = multiYear ? Calendar.YEAR : field; 76 int inc = multiYear ? -field : 1; 77 for( int i=est-inc; ; i+=inc ) { 78 gc1.add(f, inc); 79 if( gc1.after(gc2) ) { 80 return negative ? inc-i : i-inc; 81 } 82 } 83 } 84 85 89 private static int estimateUnitsBetween(long t0, long t1, int field) { 90 long d = t1-t0; 91 switch (field) { 92 case Calendar.MILLISECOND: 93 return (int)d; case Calendar.SECOND: 95 return (int)(d / SECOND_MILLIS + .5); 96 case Calendar.MINUTE: 97 return (int)(d / MINUTE_MILLIS + .5); 98 case Calendar.HOUR_OF_DAY: 99 case Calendar.HOUR: 100 return (int)(d / HOUR_MILLIS + .5); 101 case Calendar.DAY_OF_WEEK_IN_MONTH : 102 case Calendar.DAY_OF_MONTH : 103 return (int) (d / DAY_MILLIS + .5); 105 case Calendar.WEEK_OF_YEAR : 106 return (int) (d / WEEK_MILLIS + .5); 107 case Calendar.MONTH : 108 return (int) (d / MONTH_MILLIS + .5); 109 case Calendar.YEAR : 110 return (int) (d / YEAR_MILLIS + .5); 111 case DECADE: 112 return (int) (d / DECADE_MILLIS + .5); 113 case CENTURY: 114 return (int) (d / CENTURY_MILLIS + .5); 115 case MILLENIUM: 116 return (int) (d / MILLENIUM_MILLIS + .5); 117 default: 118 return 0; 119 } 120 } 121 122 130 public static void increment(Calendar c, int field, int val) { 131 if ( isMultiYear(field) ) { 132 c.add(Calendar.YEAR, -field*val); 133 } else { 134 c.add(field, val); 135 } 136 } 137 138 147 public static int get(Calendar c, int field) { 148 if ( isMultiYear(field) ) { 149 int y = c.get(Calendar.YEAR); 150 return -field * (y/-field); 151 } else { 152 return c.get(field); 153 } 154 } 155 156 159 168 public static long getDate(Calendar c, int year, int month, int day) { 169 c.clear(Calendar.MILLISECOND); 170 c.set(year, month, day, 0, 0, 0); 171 return c.getTimeInMillis(); 172 } 173 174 184 public static long getTime(Calendar c, int hour, int minute, int second) { 185 c.clear(Calendar.MILLISECOND); 186 c.set(1970, 0, 1, hour, minute, second); 187 return c.getTimeInMillis(); 188 } 189 190 197 public static Date getDate(Class type, long d) { 198 try { 199 Constructor c = type.getConstructor(new Class [] {long.class}); 200 return (Date )c.newInstance(new Object [] {new Long (d)}); 201 } catch ( Exception e ) { 202 e.printStackTrace(); 203 return null; 204 } 205 } 206 207 210 224 public static long getClearedTime(long t, Calendar c, int field) { 225 c.setTimeInMillis(t); 226 TimeLib.clearTo(c, field); 227 return c.getTimeInMillis(); 228 } 229 230 242 public static Calendar clearTo(Calendar c, int field) { 243 int i = CALENDAR_FIELDS.length-1; 244 for ( ; i>=1 && field != CALENDAR_FIELDS[i]; i-- ) { 245 int val = (CALENDAR_FIELDS[i]==Calendar.DATE?1:0); 246 c.set(CALENDAR_FIELDS[i],val); 247 } 248 if ( isMultiYear(field) ) { 249 int y = c.get(Calendar.YEAR); 250 y = -field * (y/-field); 251 c.set(Calendar.YEAR, y); 252 } 253 return c; 254 } 255 256 263 public static boolean isMultiYear(int field) { 264 return ( field == DECADE || field == CENTURY || field == MILLENIUM ); 265 } 266 267 } | Popular Tags |