English 中文(简体)
从控制器中的碎片起,装上Grail
原标题:Binding a Grails date from params in a controller

为什么很难通过铁路控制员的伞兵来抽出日期?

我不想照此抽出日期:

instance.dateX = parseDate(params["dateX_value"])//parseDate is from my helper class

我只想使用<代码>instance.properties = params。

模型中的类型为java.util。 日期:和页数均为:[dateX_个月:价值,日期X_day:数值......]

我搜索了这个净额,没有发现任何情况。 我希望,1.3.0号铁路公司能够提供帮助,但仍然是同样的事情。

我可以而且不会认为,按手提一下日期是必要的!

最佳回答

Grails Version >= 2.3

<代码>Config.groovy 界定了当对具有约束力的准绳时将在整个适用期间使用的日期格式。 日期:

grails.databinding.dateFormats = [
         MMddyyyy ,  yyyy-MM-dd HH:mm:ss.S , "yyyy-MM-dd T hh:mm:ss Z "
]

<代码>grails.data binding.dateFormats中规定的格式将按列入清单的顺序加以尝试。

您可使用<条码>@BledFormat否决个人指挥物体的全程格式。

import org.grails.databinding.BindingFormat

class Person { 
    @BindingFormat( MMddyyyy ) 
    Date birthDate 
}

Grails Version < 2.3

i 可以而且不会认为,逐个抽取日期是ness!

你的顽固态度是奖励的,从远到第1.3号铁路,就能够直接确定一个日期。 这些步骤是:

<><>>> 1. 设立一个对编辑进行注册的班级。

import org.springframework.beans.PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistry
import org.springframework.beans.propertyeditors.CustomDateEditor
import java.text.SimpleDateFormat

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar {

    public void registerCustomEditors(PropertyEditorRegistry registry) {

        String dateFormat =  yyyy/MM/dd 
        registry.registerCustomEditor(Date, new CustomDateEditor(new SimpleDateFormat(dateFormat), true))
    }
}

<>strong(2)> 使铁路公司认识到这一日期的编辑,在<条码>上登记下列星号:

beans = {
    customPropertyEditorRegistrar(CustomDateEditorRegistrar)
}

<><>>>> 如今,你以<代码>yyyyyyy/MM/dd<>/code>的形式寄出一个编号为foo的参数。 不动产将自动与名为<代码>foo的财产有关。 使用:

myDomainObject.properties = params

new MyDomainClass(params)
问题回答

第2.1.1号铁路有一条新办法,可轻易取消安全区划。

def val = params.date( myDate ,  dd-MM-yyyy )
// or a list for formats
def val = params.date( myDate , [ yyyy-MM-dd ,  yyyyMMdd ,  yyMMdd ]) 
// or the format read from messages.properties via the key  date.myDate.format 
def val = params.date( myDate )

http://grails.org/doc/latest/guide/introduction.html#whats New21”

Grails Version >= 3.x

你可以提出申请。 y 日期:

grails:
  databinding:
    dateFormats:
      -  dd/MM/yyyy 
      -  dd/MM/yyyy HH:mm:ss 
      -  yyyy-MM-dd HH:mm:ss.S 
      - "yyyy-MM-dd T hh:mm:ss Z "
      - "yyyy-MM-dd HH:mm:ss.S z"
      - "yyyy-MM-dd T HH:mm:ssX"

@Don 见上文的答复。

这里是习惯编辑的一种替代办法,该编辑首先检查日期格式。

Groovy, 只是为了java而增加半殖民地。

import java.text.DateFormat
import java.text.ParseException
import org.springframework.util.StringUtils
import java.beans.PropertyEditorSupport

class CustomDateTimeEditor extends PropertyEditorSupport {
    private final java.text.DateFormat dateTimeFormat
    private final java.text.DateFormat dateFormat
    private final boolean allowEmpty

    public CustomDateTimeEditor(DateFormat dateTimeFormat, DateFormat dateFormat, boolean allowEmpty) {
        this.dateTimeFormat = dateTimeFormat
        this.dateFormat = dateFormat
        this.allowEmpty = allowEmpty
    }

    /**
     * Parse the Date from the given text, using the specified DateFormat.
     */
    public void setAsText(String   text) throws IllegalArgumentException   {
        if (this.allowEmpty && !StringUtils.hasText(text)) {
            // Treat empty String as null value.
            setValue(null)
        }
        else {
            try {
                setValue(this.dateTimeFormat.parse(text))
            }
            catch (ParseException dtex) {
                try {
                    setValue(this.dateFormat.parse(text))
                }
                catch ( ParseException dex ) {
                    throw new IllegalArgumentException  ("Could not parse date: " + dex.getMessage() + " " + dtex.getMessage() )
                }
            }
        }
    }

    /**
     * Format the Date as String, using the specified DateFormat.
     */
    public String   getAsText() {
        Date   value = (Date) getValue()
        return (value != null ? this.dateFormat.format(value) : "")
    }
}

Grails edition >=2.3

www.un.org/Depts/DGACM/index_russian.htm

In src/groovy:

package test

import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.grails.databinding.converters.ValueConverter
import org.springframework.context.MessageSource
import org.springframework.web.servlet.LocaleResolver

import javax.servlet.http.HttpServletRequest
import java.text.SimpleDateFormat

class StringToDateConverter implements ValueConverter {
    MessageSource messageSource
    LocaleResolver localeResolver

    @Override
    boolean canConvert(Object value) {
        return value instanceof String
    }

    @Override
    Object convert(Object value) {
        String format = messageSource.getMessage( default.date.format , null, "dd/MM/yyyy", getLocale())
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format)
        return simpleDateFormat.parse(value)
    }

    @Override
    Class<?> getTargetType() {
        return Date.class
    }

    protected Locale getLocale() {
        def locale
        def request = GrailsWebRequest.lookup()?.currentRequest
        if(request instanceof HttpServletRequest) {
            locale = localeResolver?.resolveLocale(request)
        }
        if(locale == null) {
            locale = Locale.default
        }
        return locale
    }
}

In conf/children/resources.groovy:

beans = {
    defaultDateConverter(StringToDateConverter){
        messageSource = ref( messageSource )
        localeResolver = ref( localeResolver )
    }
}

The bean s nameailDateConverter is really important (to overe theaultdate Dayyer)





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签