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

Last change on this file since 498 was 476, checked in by gav, 15 years ago

Improve task search and calendar logic.
Move task quick search links to a template.
Set targetCompletionDate when creating task from calendar.
Remove task budget action and view, functionality moved to task search.
Customise calendar css.

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