Changeset 210 for trunk/grails-app/services/DateUtilService.groovy
- Timestamp:
- Dec 2, 2009, 5:26:54 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/DateUtilService.groovy
r180 r210 1 import org.codehaus.groovy.runtime.TimeCategory 2 // the above will be deprecated and replaced by: groovy.time.TimeCategory 3 /// @todo: consider moving this to org.gnumims.DateUtil 4 /// pros: easy to use in domain classes. 5 /// cons: have to import so pulls in all referenced imports? Injection probably does that anyway. 6 7 /** 8 * Provides some convenience methods for working with dates. 9 */ 1 10 class DateUtilService { 2 11 … … 4 13 //static scope = "request" 5 14 15 /** 16 * Get the start of today. 17 * Can be call as dateUtilService.today or dateUtilService.getToday(). 18 * @returns A Date object with today's date and all time fields set to 0. 19 */ 6 20 public static Date getToday() { 7 return setMidnight(new Date())21 return getMidnight(new Date()) 8 22 } 9 23 24 /** 25 * Get the start of tomorrow. 26 * Can be call as dateUtilService.tomorrow or dateUtilService.getTomorrow(). 27 * @returns A Date object with tomorrow's date and all time fields set to 0. 28 */ 10 29 public static Date getTomorrow() { 11 30 return (getToday() + 1) as Date 12 31 } 13 32 14 public static Date setMidnight(Date theDate) { 15 Calendar cal = Calendar.getInstance() 16 cal.setTime(theDate) 33 /** 34 * Get the start of yesterday. 35 * Can be call as dateUtilService.yesterday or dateUtilService.getYesterday(). 36 * @returns A Date object with yesterday's date and all time fields set to 0. 37 */ 38 public static Date getYesterday() { 39 return (getToday() - 1) as Date 40 } 41 42 /** 43 * Get the start of the day one week ago. 44 * Can be call as dateUtilService.oneWeekAgo or dateUtilService.getOneWeekAgo(). 45 * @returns A Date object with the date one week ago and all time fields set to 0. 46 */ 47 public static Date getOneWeekAgo() { 48 return (getToday() - 7) as Date 49 } 50 51 /** 52 * Get the start of the day one week ago. 53 * Can be call as dateUtilService.oneWeekAgo or dateUtilService.getOneWeekAgo(). 54 * @returns A Date object with the date one week ago and all time fields set to 0. 55 */ 56 public static Date getOneWeekFromNow() { 57 return (getToday() + 7) as Date 58 } 59 60 /** 61 * Get the start of a given date by setting all time fields to 0. 62 * The Calendar.getInstance() or Calendar.instance factory returns a new calendar instance, usually 63 * a Gregorian calendar but using Calendar we don't have to worry about those details. 64 * The time fields are then set to zero and cal.getTime() or cal.time returns a java.util.Date object. 65 * @param date The Date object to start with. 66 * @returns A Date object having the given date and all time fields set to 0, so the very start of the given day. 67 */ 68 public static Date getMidnight(Date date) { 69 Calendar cal = Calendar.instance 70 cal.setTime(date) 17 71 cal.set(Calendar.HOUR_OF_DAY, 0) 18 72 cal.set(Calendar.MINUTE, 0) 19 73 cal.set(Calendar.SECOND, 0) 20 74 cal.set(Calendar.MILLISECOND, 0) 21 cal. getTime()75 cal.time 22 76 } 23 77
Note: See TracChangeset
for help on using the changeset viewer.