4

I have a Grails domain class called Event. It has a startTime and an endTime. What object would these two fields be represented as? I need them to persist to the database, so keep that in mind. I don't need to do anything with a particular date, just a particular time of the day, and I'll probably need to do some time calculation (making sure endTime is after startTime and such)

So...would it be easier to use a java.sql.Time, something in the groovy.time package, Integers, or something else? Thanks!

tim_yates
  • 167,322
  • 27
  • 342
  • 338
grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66

2 Answers2

2

I like using JODA time plugin for it. Though the standard Date object works fine if you don't have to do too much in the way of calculations.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
BZ.
  • 1,928
  • 2
  • 17
  • 26
1

If the database allows to use datetime as the datatype for those two columns, then I would prefer Groovy Date although you get flexibility using Joda Time plugin.

Date will be the epoch and groovy date provides an easy mechanism of parsing the date/time from String. For example, if startTime or endTime is represented as a string like 13:45:32 (from view or from a REST call) representing 1:45 PM time then this string can be easily parsed to Date as:

Date.parse('HH:mm:ss', '13:45:32')
//Thu Jan 01 13:45:32 EST 1970

Conversely, after fetching the dates from persistence layer you can compare like:

endTime.after(startTime)

which would ultimately compare on the time (HH:mm:ss) since date will always be the epoch [Jan 1, 1970] in all cases.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117