1 18 package net.sf.drftpd.util; 19 20 import java.util.Calendar ; 21 22 import org.apache.log4j.Logger; 23 24 28 public class CalendarUtils { 29 private static final Logger logger = Logger.getLogger(CalendarUtils.class); 30 31 private CalendarUtils() { 32 } 33 34 public static void floorAllLessThanDay(Calendar cal) { 35 cal.set(Calendar.MILLISECOND, 0); 36 cal.set(Calendar.SECOND, 0); 37 cal.set(Calendar.MINUTE, 0); 38 cal.set(Calendar.HOUR_OF_DAY, 0); 39 } 40 41 public static void ceilAllLessThanDay(Calendar cal) { 42 cal.set( 43 Calendar.MILLISECOND, 44 cal.getActualMaximum(Calendar.MILLISECOND)); 45 cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND)); 46 cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE)); 47 cal.set( 48 Calendar.HOUR_OF_DAY, 49 cal.getActualMaximum(Calendar.HOUR_OF_DAY)); 50 } 51 52 public static void floorDayOfWeek(Calendar cal) { 53 cal.get(Calendar.WEEK_OF_MONTH); 56 57 cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); 58 59 if(cal.get(Calendar.DAY_OF_WEEK) != cal.getFirstDayOfWeek()) 60 logger.error("cal.set(DAY_OF_WEEK) didn't work! " + cal.getTime()); 61 } 62 63 public static void floorDayOfMonth(Calendar cal) { 64 cal.set( 65 Calendar.DAY_OF_MONTH, 66 cal.getActualMinimum(Calendar.DAY_OF_MONTH)); 67 } 68 69 75 public static void incrementMonth(Calendar cal) { 76 Calendar cal2 = (Calendar ) cal.clone(); 77 floorDayOfMonth(cal2); 78 cal2.add(Calendar.MONTH, 1); 79 if (cal.get(Calendar.DAY_OF_MONTH) 80 > cal2.getActualMaximum(Calendar.DAY_OF_MONTH)) { 81 floorDayOfMonth(cal); 82 cal.add(Calendar.MONTH, 2); 83 } 84 cal.add(Calendar.MONTH, 1); 85 } 86 87 public static void incrementWeek(Calendar cal) { 88 cal.add(Calendar.WEEK_OF_YEAR, 1); 89 } 90 public static void incrementDay(Calendar cal) { 91 cal.add(Calendar.DAY_OF_MONTH, 1); 92 } 93 public static int getLastDayOfWeek(Calendar cal) { 94 int dow = cal.getFirstDayOfWeek() - 1; 95 if (dow == 0) 96 dow = Calendar.SATURDAY; 97 return dow; 98 } 99 } 100 | Popular Tags |