I m working in a grails project. i m using kendo grid in my application. i need to create a domain class named "StatTimings" which contains two time field startTime and endTime. I cant use "Date" data type for those two variable coz the time format i need is hh:mm. I dont want to install any plugin for this. this is my domain for now:
class StatTimings{
???? startTime
???? endTime
Date date
AutoPosting autoPosting
Status status
static constraints = {
}
enum Status{ACTIVE,INACTIVE}
enum AutoPosting{SERVICE_CHARGE,STAT_CHARGES,BOTH}
}
Is there any way I can make my field to accept time only?
To make it easy to persist and bind (from HTTP params) instances of this class, I'd represent each time as two integer fields and add some helper method like getStartTime()
and getEndTime()
. You might want to change these helpers to return a Date
instead of a String
(with the day part thereof set to today) if you need to do something like calculating the difference between the start and end time.
class StatTimings {
static transients = ['startTime', 'endTime']
Integer startHours
Integer startMins
Integer endHours
Integer endMins
private String formatTime(Integer hours, Integer mins) {
String formattedHours = hours < 10 ? "0$hours" : hours.toString()
String formattedMins = mins < 10 ? "0$mins" : mins.toString()
"$formattedHours:$formattedMins"
}
String getStartTime() {
formatTime(startHours, startMins)
}
String getEndTime() {
formatTime(endHours, endMins)
}
static constraints = {
startHours range: 0..23
endHours range: 0..23
startMins range: 0..59
endMins range: 0..59
// TODO: add a custom validator that checks the end time is after start time
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With