Changeset 210


Ignore:
Timestamp:
Dec 2, 2009, 5:26:54 AM (14 years ago)
Author:
gav
Message:

Make date calculations and formatting more groovy.

Location:
trunk/grails-app
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/InventoryMovement.groovy

    r177 r210  
    1 import java.text.SimpleDateFormat
    2 
    31class InventoryMovement {
    42    InventoryItem inventoryItem
     
    1917
    2018    String toString() {
    21         def date = new SimpleDateFormat("EEE, dd-MMM-yyyy").format(this.date)
    22         "${this.quantity} ${inventoryMovementType.name} on ${date}"
     19        "${this.quantity} ${inventoryMovementType.name} on ${date.format('EEE, dd-MMM-yyyy')}"
    2320    }
    2421}
  • trunk/grails-app/domain/TaskModification.groovy

    r187 r210  
    1 import java.text.SimpleDateFormat
    2 
    31class TaskModification {
    42    Person person
     
    1917
    2018    String toString() {
    21         def date = new SimpleDateFormat("EEE, dd-MMM-yyyy").format(this.date)
    22         "${taskModificationType} on ${date} by ${person}."
     19        "${taskModificationType} on ${date.format('EEE, dd-MMM-yyyy')} by ${person}."
    2320    }
    2421}
  • trunk/grails-app/domain/TaskRecurringSchedule.groovy

    r199 r210  
    11import org.codehaus.groovy.runtime.TimeCategory
     2// the above will be deprecated and replaced by: groovy.time.TimeCategory
    23
    34class TaskRecurringSchedule {
  • trunk/grails-app/services/CreateDataService.groovy

    r199 r210  
    1111    def personService
    1212    def taskService
     13    def dateUtilService
    1314
    1415/*******************************************
     
    507508                description:"Check specific level sensor",
    508509                comment:"Has been noted as problematic, try recalibrating.",
    509                 targetStartDate:new Date()]
     510                targetStartDate: dateUtilService.today]
    510511
    511512        taskResult = taskService.create(p)
     
    518519                description:"Some follow-up work",
    519520                comment:"Some help required",
    520                 targetStartDate:new Date()+1,
     521                targetStartDate: dateUtilService.tomorrow,
    521522                parentTask: Task.get(1)]
    522523
     
    530531                description:"A Sub Task can be created by setting the Parent Task value",
    531532                comment:"Some help required",
    532                 targetStartDate:new Date()-1,
     533                targetStartDate: dateUtilService.yesterday,
    533534                parentTask: Task.get(1)]
    534535
     
    542543                 description:"Replace sensor at next opportunity.",
    543544                 comment:"Nothing else has worked.",
    544                 targetStartDate:new Date()+7,
     545                targetStartDate: dateUtilService.oneWeekFromNow,
    545546                parentTask: Task.get(1)]
    546547
     
    554555                 description:"Production Report",
    555556                 comment:"Production report for specific production run or shift",
    556                 targetStartDate:new Date()-6]
     557                targetStartDate: dateUtilService.today - 6]
    557558
    558559        taskResult = taskService.create(p)
     
    565566                 description:"This is a recurring task",
    566567                 comment:"If there is a parent task specified then this is a generated sub task, if there is a recurring schedule specified then this is a parent task.",
    567                 targetStartDate:new Date()]
     568                targetStartDate: dateUtilService.today]
    568569
    569570        taskResult = taskService.create(p)
     
    647648                                                                                                    recurEvery: 1,
    648649                                                                                                    recurPeriod: Period.get(2),
    649                                                                                                     nextTargetStartDate: new Date(),
     650                                                                                                    nextTargetStartDate: dateUtilService.today,
    650651                                                                                                    generateAhead: 1,
    651652                                                                                                    taskDuration: 2,
     
    658659                                                                                                    recurEvery: 1,
    659660                                                                                                    recurPeriod: Period.get(1),
    660                                                                                                     nextTargetStartDate: new Date(),
     661                                                                                                    nextTargetStartDate: dateUtilService.today,
    661662                                                                                                    generateAhead: 1,
    662663                                                                                                    taskDuration: 1,
  • trunk/grails-app/services/DateUtilService.groovy

    r180 r210  
     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*/
    110class DateUtilService {
    211
     
    413    //static scope = "request"
    514
     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    */
    620    public static Date getToday() {
    7         return setMidnight(new Date())
     21        return getMidnight(new Date())
    822    }
    923
     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    */
    1029    public static Date getTomorrow() {
    1130        return (getToday() + 1) as Date
    1231    }
    1332
    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)
    1771        cal.set(Calendar.HOUR_OF_DAY, 0)
    1872        cal.set(Calendar.MINUTE, 0)
    1973        cal.set(Calendar.SECOND, 0)
    2074        cal.set(Calendar.MILLISECOND, 0)
    21         cal.getTime()
     75        cal.time
    2276    }
    2377
Note: See TracChangeset for help on using the changeset viewer.