1 package snow.utils; 2 3 import java.text.*; 4 import java.util.Date ; 5 import java.util.GregorianCalendar ; 6 import java.util.Calendar ; 7 import java.text.DecimalFormat ; 8 9 11 public final class DateUtils 12 { 13 static final DecimalFormat format0 = new DecimalFormat ("0.0"); 14 public static SimpleDateFormat dateFormatMDY = new SimpleDateFormat("MMM dd yyyy"); 15 public static SimpleDateFormat dateFormatMDYhm = new SimpleDateFormat("MMM dd yyyy HH:mm"); 16 public static SimpleDateFormat timeFormatHHMM = new SimpleDateFormat("HH:mm"); 17 public static final long oneDayMs = 1000L*3600*24; 18 19 20 21 private DateUtils() 22 { 23 } 24 25 26 28 public static String formatDateAndTimeFix(long time) 29 { 30 return dateFormatMDYhm.format(time); 31 } 32 33 36 public static final String formatTimeDifference(long diff) 37 { 38 return formatTimeDifference(diff,false); 39 } 40 41 42 private static long cachedMidnight = 0; 43 44 public synchronized static long getMidnightTodayMorning() 45 { 46 long dt = System.currentTimeMillis() - cachedMidnight; 48 if(dt>=0 && dt < 24L*3600*1000) 49 { 50 return cachedMidnight; 52 } 53 54 cachedMidnight = getStartOfDayForTime(System.currentTimeMillis()); 57 return cachedMidnight; 58 } 59 60 public synchronized static long getStartOfDayForTime(long time) 61 { 62 63 Calendar cal = GregorianCalendar.getInstance(); 64 cal.setTimeInMillis( time ); 65 cal.set( Calendar.HOUR_OF_DAY, 0); 66 cal.set( Calendar.MINUTE, 0); 67 cal.set( Calendar.SECOND, 0); 68 cal.set( Calendar.MILLISECOND, 0); 69 70 return cal.getTimeInMillis(); 71 } 72 73 74 public static boolean isToday(long t) 75 { 76 long md = getMidnightTodayMorning(); 77 if(t - md >=0 && t - md < 24L*3600*1000) return true; 78 return false; 79 } 80 81 82 85 public static final String formatTimeDifference(long diff, boolean notGreaterMultiplierThanHours) 86 { 87 long adiff = Math.abs(diff); 88 if(adiff < 1000L) return "" + diff + " ms"; 89 if(adiff < 1000L*60) 90 { 91 double ds = diff/1000.0; 92 return roundInt(ds)+" s"; 93 } 94 if(adiff < 1000L*3600) 95 { 96 double dm = diff/1000.0/60.0; 97 if(dm<2) 98 { 99 return format0.format(dm)+" m"; 100 } 101 else 102 { 103 return roundInt(dm)+" m"; 104 } 105 } 106 if(notGreaterMultiplierThanHours ||adiff < 1000L*3600*24) 107 { 108 return format0.format(diff/1000.0/3600.0)+" h"; 109 } 110 if(adiff < 1000L*3600*24*30) { 112 double dd = diff/1000.0/3600.0/24.0; 113 if(dd<2) 114 { 115 return format0.format(dd)+" day"; 116 } 117 else 118 { 119 int rid = roundInt(dd); 120 return rid+" day"+(rid!=1?"s":""); 121 } 122 } 123 if(adiff < 1000L*3600*24*30*13) 124 { 125 double md = diff/1000.0/3600.0/24.0/30; 126 if(md<1.5) 127 { 128 return format0.format(md)+" month"; 129 } 130 else 131 { 132 int rim = roundInt(md); 133 return rim+" month"+(rim!=1?"s":""); 134 } 135 } 136 double yd = diff/1000.0/3600/24/30/12; 137 if(yd<1.5) 138 { 139 return format0.format(yd)+" year"; 140 } 141 else 142 { 143 int riy = roundInt(yd); 144 return ""+riy+" year"+(riy!=1?"s":""); 145 } 146 } 147 148 private static int roundInt(double d) 149 { 150 return (int) Math.round(d); 151 } 152 153 public static int getDaysUntilBirthday(long birth) 154 { 155 return getDaysUntilBirthday(birth, System.currentTimeMillis()); 156 } 157 158 161 public static int getDaysUntilBirthday(long birth, long now) 162 { 163 Calendar cn = GregorianCalendar.getInstance(); 165 cn.setTime(new Date (now)); 166 int dyNow = cn.get(Calendar.DAY_OF_YEAR); 167 168 Calendar cb = GregorianCalendar.getInstance(); 170 cb.setTime(new Date (birth)); 171 cb.set(Calendar.YEAR, cn.get(Calendar.YEAR)); 173 int dyb = cb.get(Calendar.DAY_OF_YEAR); 174 175 176 int diff = dyb - dyNow; 178 if(diff<-180) 179 { 180 int daysThisYear = cn.getActualMaximum(Calendar.DAY_OF_YEAR); 183 int remainingThisYear = daysThisYear-dyNow; 185 cb.set(Calendar.YEAR, cn.get(Calendar.YEAR)+1); 188 dyb = cb.get(Calendar.DAY_OF_YEAR); 189 190 return remainingThisYear+dyb; 191 } 192 else if(diff>180) 193 { 194 cb.set(Calendar.YEAR, cn.get(Calendar.YEAR)-1); 196 int daysOfLastYear = cb.getActualMaximum(Calendar.DAY_OF_YEAR); 197 int lastYearDaysFromBirth = daysOfLastYear-cb.get(Calendar.DAY_OF_YEAR); 198 199 return dyNow+ lastYearDaysFromBirth; 200 201 } 202 else if(diff<0) 203 { 204 return diff; 207 } 208 else 209 { 210 return diff; 212 } 213 } 214 215 218 public static Date getDate(int day, int month, int year) 219 { 220 Calendar cn = GregorianCalendar.getInstance(); 221 cn.set(year,month,day); 222 return cn.getTime(); 223 } 224 225 public static void main(String [] args) 226 { 227 System.out.println("" + 228 getDaysUntilBirthday( 229 getDate(1,8,2007).getTime(), getDate(12,7,2007).getTime() )); 232 233 System.out.println("" + 234 getDaysUntilBirthday( 235 getDate(12,9,2007).getTime(), getDate(12,0,2007).getTime() )); 238 } 239 240 } | Popular Tags |