1 7 package org.jdesktop.swing.calendar; 8 9 import java.awt.*; 10 import javax.swing.*; 11 import javax.swing.plaf.*; 12 import java.text.*; 13 import java.util.*; 14 15 19 public class DateUtils { 20 private static Calendar CALENDAR = Calendar.getInstance(); 21 22 28 public static Date endOfDay(Date date) { 29 Calendar calendar = CALENDAR; 30 synchronized(calendar) { 31 calendar.setTime(date); 32 calendar.set(Calendar.HOUR_OF_DAY, 23); 33 calendar.set(Calendar.MILLISECOND, 999); 34 calendar.set(Calendar.SECOND, 59); 35 calendar.set(Calendar.MINUTE, 59); 36 return calendar.getTime(); 37 } 38 } 39 40 41 48 public static Date startOfDay(Date date) { 49 Calendar calendar = CALENDAR; 50 synchronized(calendar) { 51 calendar.setTime(date); 52 calendar.set(Calendar.HOUR_OF_DAY, 0); 53 calendar.set(Calendar.MILLISECOND, 0); 54 calendar.set(Calendar.SECOND, 0); 55 calendar.set(Calendar.MINUTE, 0); 56 return calendar.getTime(); 57 } 58 } 59 60 67 public static long startOfDayInMillis(long date) { 68 Calendar calendar = CALENDAR; 69 synchronized(calendar) { 70 calendar.setTimeInMillis(date); 71 calendar.set(Calendar.HOUR_OF_DAY, 0); 72 calendar.set(Calendar.MILLISECOND, 0); 73 calendar.set(Calendar.SECOND, 0); 74 calendar.set(Calendar.MINUTE, 0); 75 return calendar.getTimeInMillis(); 76 } 77 } 78 79 85 public static long endOfDayInMillis(long date) { 86 Calendar calendar = CALENDAR; 87 synchronized(calendar) { 88 calendar.setTimeInMillis(date); 89 calendar.set(Calendar.HOUR_OF_DAY, 23); 90 calendar.set(Calendar.MILLISECOND, 999); 91 calendar.set(Calendar.SECOND, 59); 92 calendar.set(Calendar.MINUTE, 59); 93 return calendar.getTimeInMillis(); 94 } 95 } 96 97 98 104 public static Date nextDay(Date date) { 105 return new Date(addDays(date.getTime(), 1)); 106 } 107 108 115 public static long addDays(long time, int amount) { 116 Calendar calendar = CALENDAR; 117 synchronized(calendar) { 118 calendar.setTimeInMillis(time); 119 calendar.add(Calendar.DAY_OF_MONTH, amount); 120 return calendar.getTimeInMillis(); 121 } 122 } 123 124 130 public static long nextDay(long date) { 131 return addDays(date, 1); 132 } 133 134 140 public static long nextWeek(long date) { 141 return addDays(date, 7); 142 } 143 144 145 154 public static int getDaysDiff(long t1, long t2, boolean checkOverflow) { 155 if (t1 > t2) { 156 long tmp = t1; 157 t1 = t2; 158 t2 = tmp; 159 } 160 Calendar calendar = CALENDAR; 161 synchronized(calendar) { 162 calendar.setTimeInMillis(t1); 163 int delta = 0; 164 while (calendar.getTimeInMillis() < t2) { 165 calendar.add(Calendar.DAY_OF_MONTH, 1); 166 delta++; 167 } 168 if (checkOverflow && (calendar.getTimeInMillis() > t2)) { 169 delta--; 170 } 171 return delta; 172 } 173 } 174 175 183 public static int getDaysDiff(long t1, long t2) { 184 return getDaysDiff(t1, t2, true); 185 } 186 187 191 public static boolean isFirstOfYear(long date) { 192 boolean ret = false; 193 Calendar calendar = CALENDAR; 194 synchronized(calendar) { 195 calendar.setTimeInMillis(date); 196 int currentYear = calendar.get(Calendar.YEAR); 197 calendar.add(Calendar.DATE,-1); 199 int yesterdayYear = calendar.get(Calendar.YEAR); 200 ret = (currentYear != yesterdayYear); 201 } 202 return ret; 203 } 204 205 209 public static boolean isFirstOfMonth(long date) { 210 boolean ret = false; 211 Calendar calendar = CALENDAR; 212 synchronized(calendar) { 213 calendar.setTimeInMillis(date); 214 int currentMonth = calendar.get(Calendar.MONTH); 215 calendar.add(Calendar.DATE,-1); 217 int yesterdayMonth = calendar.get(Calendar.MONTH); 218 ret = (currentMonth != yesterdayMonth); 219 } 220 return ret; 221 } 222 223 224 230 public static long previousDay(long date) { 231 return addDays(date, -1); 232 } 233 234 240 public static long previousWeek(long date) { 241 return addDays(date, -7); 242 } 243 244 245 257 public static long getPreviousDay(long date, int startOfWeek) { 258 return getDay(date, startOfWeek, -1); 259 } 260 261 273 public static long getNextDay(long date, int startOfWeek) { 274 return getDay(date, startOfWeek, 1); 275 } 276 277 private static long getDay(long date, int startOfWeek, int increment) { 278 Calendar calendar = CALENDAR; 279 synchronized(calendar) { 280 calendar.setTimeInMillis(date); 281 int day = calendar.get(Calendar.DAY_OF_WEEK); 282 while (day != startOfWeek) { 284 calendar.add(Calendar.DATE, increment); 285 day = calendar.get(Calendar.DAY_OF_WEEK); 286 } 287 return startOfDayInMillis(calendar.getTimeInMillis()); 288 } 289 } 290 291 297 public static long getPreviousMonth(long date) { 298 return incrementMonth(date, -1); 299 } 300 301 307 public static long getNextMonth(long date) { 308 return incrementMonth(date, 1); 309 } 310 311 private static long incrementMonth(long date, int increment) { 312 Calendar calendar = CALENDAR; 313 synchronized(calendar) { 314 calendar.setTimeInMillis(date); 315 calendar.add(Calendar.MONTH, increment); 316 return calendar.getTimeInMillis(); 317 } 318 } 319 320 326 public static long getStartOfMonth(long date) { 327 return getMonth(date, -1); 328 } 329 330 336 public static long getEndOfMonth(long date) { 337 return getMonth(date, 1); 338 } 339 340 private static long getMonth(long date, int increment) { 341 Calendar calendar = CALENDAR; 342 synchronized(calendar) { 343 calendar.setTimeInMillis(date); 344 if (increment == -1) { 345 calendar.set(Calendar.DAY_OF_MONTH, 1); 346 return startOfDayInMillis(calendar.getTimeInMillis()); 347 } 348 else { 349 calendar.add(Calendar.MONTH, 1); 350 calendar.set(Calendar.DAY_OF_MONTH, 1); 351 calendar.set(Calendar.HOUR_OF_DAY, 0); 352 calendar.set(Calendar.MILLISECOND, 0); 353 calendar.set(Calendar.SECOND, 0); 354 calendar.set(Calendar.MINUTE, 0); 355 calendar.add(Calendar.MILLISECOND, -1); 356 return calendar.getTimeInMillis(); 357 } 358 } 359 } 360 361 367 public static int getDayOfWeek(long date) { 368 Calendar calendar = CALENDAR; 369 synchronized(calendar) { 370 calendar.setTimeInMillis(date); 371 return (calendar.get(Calendar.DAY_OF_WEEK)); 372 } 373 } 374 } 375 | Popular Tags |