简体   繁体   English

时间字段在grails域类中

[英]Time field In grails domain class

I m working in a grails project. 我正在做一个grails项目。 im using kendo grid in my application. 我在应用程序中使用剑道网格。 i need to create a domain class named "StatTimings" which contains two time field startTime and endTime. 我需要创建一个名为“ StatTimings”的域类,其中包含两个时间字段startTime和endTime。 I cant use "Date" data type for those two variable coz the time format i need is hh:mm. 我不能为这两个变量使用“日期”数据类型,因为我需要的时间格式是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() . 为了易于持久化和绑定(从HTTP参数)此类的实例,我每次将其表示为两个整数字段,并添加一些辅助方法,如getStartTime()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. 如果您需要执行诸如计算开始时间和结束时间之间的差之类的操作,则可能需要更改这些帮助器,以返回Date而不是String (日期部分设置为今天)。

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
  }
}

You could use Date and convert it in the getStartTime() method to the format you need with the help of SimpleDateFormat . 您可以使用Date并借助SimpleDateFormat将其在getStartTime()方法中转换为所需的格式。 This way you ignore the date part of it. 这样,您将忽略日期部分。

class StatTimings {
    ...

    def getStartTime() {
        return new SimpleDateFormat("hh:mm").format(this.startTime);
    }

}

The java.sql.Time class subclasses java.util.Date and it makes almost the same thing and it adds a custom toString implementation as below : java.sql.Time类继承了java.util.Date的子类,它具有几乎相同的功能,并添加了一个自定义的toString实现,如下所示:

public String toString () {

    int hour = super.getHours();
    int minute = super.getMinutes();
    int second = super.getSeconds();
    String hourString;
    String minuteString;
    String secondString;

    if (hour < 10) {
        hourString = "0" + hour;
    } else {        
        hourString = Integer.toString(hour);
    }
    if (minute < 10) {
        minuteString = "0" + minute;
    } else {        
        minuteString = Integer.toString(minute);
    }
    if (second < 10) {
        secondString = "0" + second;
    } else {        
        secondString = Integer.toString(second);
    }
    return (hourString + ":" + minuteString + ":" + secondString);
}

So I guess that you could add dynamically a method to Date class on bootstrap and invoke it when needed. 因此,我想您可以在引导程序上为Date类动态添加一个方法,并在需要时调用它。

You can see the complete source here . 您可以在此处查看完整的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM