如何在 grails 3 中绑定时间?

How to bind time in grails 3?

在 grails 3 中有没有办法将格式为“11:00 AM”的时间绑定到 java.sql.Time?

import java.sql.Time


Time startTime

有几种方法可以做到这一点。一种是提供适当的 ValueConverter,如 http://docs.grails.org/3.3.8/guide/theWebLayer.html#dataBinding.

自定义数据转换器 部分所述

可能看起来像这样...

package com.myapp.converters

import grails.databinding.converters.ValueConverter
import java.sql.Time

/**
 * A custom converter which will convert String into a Time object.
 */
class TimeValueConverter implements ValueConverter {

    boolean canConvert(value) {
        value instanceof String
    }

    def convert(value) {
        // return a new Time instance initialized with the `value` parameter
    }

    Class<?> getTargetType() {
        java.sql.Time
    }
}

然后在 Spring 应用程序上下文中添加 class 的实例:

beans = {
    timeConverter com.myapp.converters.TimeValueConverter
    // ...
}