Working with time as a Grails domain class field

I have a Grails domain class called Event. It has startTime and endTime . At what object will these two fields be presented? I need them to be stored in the database, so keep that in mind. I donโ€™t need to do anything with a specific date, just at a specific time of the day, and I probably have to do some time (so that endTime is after startTime, etc.)

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

+4
source share
2 answers

I like to use the JODA time plugin for it. Although the standard Date object works fine unless you need to do too much in the way of computing.

+2
source

If the database allows you to use datetime as the data type for these two columns, I would prefer Groovy Date , although you get the flexibility of using the Joda Time plugin.

The date will be an era and groovy dates a simple date / time analysis mechanism from String. For example, if startTime or endTime is represented as a string of type 13:45:32 (from a view or from a REST call) representing time 1:45 PM , then this string can be easily parsed for Date as:

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

And vice versa, after receiving dates from the persistence level, you can compare:

endTime.after(startTime)

which will ultimately compare time ( HH:mm:ss ), since the date will always be an era [January 1, 1970] in all cases.

+1
source

Source: https://habr.com/ru/post/1488908/


All Articles