source: trunk/grails-app/services/DateUtilService.groovy @ 611

Last change on this file since 611 was 553, checked in by gav, 15 years ago

Small refator of DateUtilService.
New plusMonth() replaces getNextMonth and getPreviousMonth().
Clarify getDayFromDate() to getDayOfMonthFromDate().

File size: 5.9 KB
RevLine 
[210]1import 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*/
[137]10class DateUtilService {
11
12    boolean transactional = false
[180]13    //static scope = "request"
14
[210]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    */
[180]20    public static Date getToday() {
[210]21        return getMidnight(new Date())
[137]22    }
23
[210]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    */
[137]29    public static Date getTomorrow() {
30        return (getToday() + 1) as Date
31    }
32
[210]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)
[137]71        cal.set(Calendar.HOUR_OF_DAY, 0)
72        cal.set(Calendar.MINUTE, 0)
73        cal.set(Calendar.SECOND, 0)
74        cal.set(Calendar.MILLISECOND, 0)
[210]75        cal.time
[137]76    }
77
[214]78    /**
[553]79    * Get the date plus x weeks.
80    * @param date The Date object to start with, defaults to today.
81    * @param plusYears The number of weeks to add, defaults to 1.
82    * @returns A Date object adjusted by x weeks.
[474]83    */
[553]84    public static Date plusWeek(Date date = new Date(), Integer plusWeeks = 1) {
[474]85        use(TimeCategory) {
[553]86            date + plusWeeks.weeks
[474]87        }
88    }
89
90    /**
[553]91    * Get the date plus x months.
92    * @param date The Date object to start with, defaults to today.
93    * @param plusYears The number of months to add, defaults to 1.
94    * @returns A Date object adjusted by x months.
[474]95    */
[553]96    public static Date plusMonth(Date date = new Date(), Integer plusMonths = 1) {
[474]97        use(TimeCategory) {
[553]98            date + plusMonths.months
[474]99        }
100    }
101
102    /**
[552]103    * Get the date plus x years.
104    * @param date The Date object to start with, defaults to today.
105    * @param plusYears The number of years to add, defaults to 1.
106    * @returns A Date object adjusted by x years.
[476]107    */
[552]108    public static Date plusYear(Date date = new Date(), Integer plusYears = 1) {
[476]109        use(TimeCategory) {
[552]110            date + plusYears.years
[476]111        }
112    }
113
114    /**
[214]115    * Make a date object from supplied year, month, day values.
116    * The Calendar.getInstance() or Calendar.instance factory returns a new calendar instance, usually
117    * a Gregorian calendar but using Calendar we don't have to worry about those details.
118    * The time fields are set to zero and cal.getTime() or cal.time returns a java.util.Date object.
119    * @param year The year as a string or integer.
[476]120    * @param month The month as a string or integer, defaults to 1 (i.e. January).
121    * @param day The day as a string or integer, defaults to 1.
[214]122    * @returns A Date object having the given date and all time fields set to 0, so the very start of the given day.
123    */
[476]124    public static Date makeDate(year, month=1, day=1) {
[214]125        Calendar cal = Calendar.instance
126        cal.clear()
127        // Stupid month is 0-based, grumble.
128        cal.set(year.toInteger(), month.toInteger()-1, day.toInteger())
129        cal.time
130    }
131
[476]132    /**
133    * Get the day of month from supplied date.
134    * @param date The date to extract the day of month from.
135    * @returns An integer representing the day of the month.
136    */
[553]137    public static Integer getDayOfMonthFromDate(Date date) {
[476]138        Calendar cal = Calendar.instance
139        cal.setTime(date)
140        cal.get(Calendar.DAY_OF_MONTH)
141    }
142
143    /**
144    * Get the month from supplied date.
145    * @param date The date to extract the month from.
146    * @returns An integer representing the month.
147    */
148    public static Integer getMonthFromDate(Date date) {
149        Calendar cal = Calendar.instance
150        cal.setTime(date)
151        cal.get(Calendar.MONTH) + 1 // Stupid month is 0-based, grumble.
152    }
153
154    /**
155    * Get the year from supplied date.
156    * @param date The date to extract the year from.
157    * @returns An integer representing the year.
158    */
159    public static Integer getYearFromDate(Date date) {
160        Calendar cal = Calendar.instance
161        cal.setTime(date)
162        cal.get(Calendar.YEAR)
163    }
164
[137]165}
Note: See TracBrowser for help on using the repository browser.