1 package org.oddjob.schedules; 2 3 import java.util.Calendar ; 4 import java.util.Date ; 5 import java.util.GregorianCalendar ; 6 import java.util.TimeZone ; 7 8 13 14 public class DateUtils { 15 16 20 private DateUtils() { 21 } 22 23 29 public static Date startOfDay(Date inDate, TimeZone timeZone) { 30 GregorianCalendar c1 = new GregorianCalendar (); 31 c1.setTimeZone(timeZone); 32 c1.setTime(inDate); 33 GregorianCalendar c2 = new GregorianCalendar ( 34 c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), 35 c1.get(Calendar.DATE)); 36 c2.setTimeZone(timeZone); 37 return c2.getTime(); 38 } 39 40 47 public static Date endOfDay(Date inDate, TimeZone timeZone) { 48 Calendar inCalendar = new GregorianCalendar (); 49 inCalendar.setTimeZone(timeZone); 50 inCalendar.setTime(inDate); 51 Calendar nextDay = new GregorianCalendar ( 52 inCalendar.get(Calendar.YEAR), 53 inCalendar.get(Calendar.MONTH), 54 inCalendar.get(Calendar.DAY_OF_MONTH) + 1); 55 nextDay.setTimeZone(timeZone); 56 return new Date (nextDay.getTime().getTime() - 1); 57 } 58 59 65 public static int dayOfWeek(Date inDate, TimeZone timeZone) { 66 Calendar calendar = Calendar.getInstance(timeZone); 67 calendar.setTime(inDate); 68 69 return calendar.get(Calendar.DAY_OF_WEEK); 70 } 71 72 78 public static int month(Date inDate, TimeZone timeZone) { 79 Calendar calendar = Calendar.getInstance(); 80 calendar.setTimeZone(timeZone); 81 calendar.setTime(inDate); 82 83 return calendar.get(Calendar.MONTH); 84 } 85 86 92 public static int dayOfMonth(Date inDate, TimeZone timeZone) { 93 Calendar calendar = Calendar.getInstance(); 94 calendar.setTimeZone(timeZone); 95 calendar.setTime(inDate); 96 97 return calendar.get(Calendar.DAY_OF_MONTH); 98 } 99 100 107 public static int dayOfYear(Date forDate, TimeZone timeZone) { 108 Calendar calendar = Calendar.getInstance(); 109 calendar.setTimeZone(timeZone); 110 calendar.setTime(forDate); 111 return calendar.get(Calendar.DAY_OF_YEAR); 112 } 113 114 120 public static Date oneMillisAfter(Date date) { 121 if (date.getTime() == Interval.NEVER_AGAIN) { 122 return null; 123 } 124 return new Date (date.getTime() + 1); 125 } 126 127 133 public static Date oneMillisBefore(Date date) { 134 return new Date (date.getTime() - 1); 135 } 136 137 144 public static int compare(Calendar c1, Calendar c2) { 145 long m1 = c1.getTime().getTime(); 146 long m2 = c2.getTime().getTime(); 147 148 if (m1 < m2) { 149 return -1; 150 } 151 if (m1 > m2) { 152 return 1; 153 } 154 return 0; 155 } 156 } 157 158 | Popular Tags |