14

RESTful クライアントから次の json を取得した場合、java.util.Date をエレガントに非整列化するにはどうすればよいですか? ((ハードコーディングとも呼ばれる)フォーマットを提供せずにそれは可能ですか、それは私がエレガントに意味するものです...)

{
  "class": "url",
  "link": "http://www.empa.ch",
  "rating": 5,
  "lastcrawl" : "2009-06-04 16:53:26.706 CEST",
  "checksum" : "837261836712xxxkfjhds",
}
4

2 に答える 2

18

最もクリーンな方法は、可能な日付形式のカスタム DataBinder を登録することです。

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CustomDateBinder extends PropertyEditorSupport {

    private final List<String> formats;

    public CustomDateBinder(List formats) {
        List<String> formatList = new ArrayList<String>(formats.size());
        for (Object format : formats) {
            formatList.add(format.toString()); // Force String values (eg. for GStrings)
        }
        this.formats = Collections.unmodifiableList(formatList);
    }

    @Override
    public void setAsText(String s) throws IllegalArgumentException {
        if (s != null)
            for (String format : formats) {
                // Need to create the SimpleDateFormat every time, since it's not thead-safe
                SimpleDateFormat df = new SimpleDateFormat(format);
                try {
                    setValue(df.parse(s));
                    return;
                } catch (ParseException e) {
                    // Ignore
                }
            }
    }
}

PropertyEditorRegistrar も実装する必要があります

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

import grails.util.GrailsConfig;
import java.util.Date;
import java.util.List;

public class CustomEditorRegistrar implements PropertyEditorRegistrar {
    public void registerCustomEditors(PropertyEditorRegistry reg) {
        reg.registerCustomEditor(Date.class, new CustomDateBinder(GrailsConfig.get("grails.date.formats", List.class)));
    }
}          

grails-app/conf/spring/resources.groovy に Spring-bean 定義を作成します。

beans = {
    "customEditorRegistrar"(CustomEditorRegistrar)
}

最後に、grails-app/conf/Config.groovy で日付形式を定義します。

grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"]
于 2009-06-08T09:37:51.360 に答える
5

Grails 2.3+ の新しいバージョンでは、このタイプの機能がすぐにサポートされることに注意してください。データバインディングの日付形式を参照してください

そうは言っても、2.3 より前のバージョンの Grails を使用せざるを得ない場合はCustomEditorRegistrar 、次のコードを使用して を更新して非推奨の警告をなくす@Componentことができます。豆を直接入れresources.groovyます。また、grails 構成プロパティ名を grails.databinding.dateFormats に変更したこともありません。これは、現在 Grails 2.3+ でサポートされているプロパティと一致します。最後に、私のバージョンは .java ファイルではなく .groovy です。

import javax.annotation.Resource
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.beans.PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistry
import org.springframework.stereotype.Component

@Component
public class CustomEditorRegistrar implements PropertyEditorRegistrar {

    @Resource
    GrailsApplication grailsApplication

    public void registerCustomEditors(PropertyEditorRegistry reg){
        def dateFormats = grailsApplication.config.grails.databinding.dateFormats as List
        reg.registerCustomEditor(Date.class, new CustomDateBinder(dateFormats))
    }
}
于 2014-06-12T14:47:15.773 に答える