2009-06-08 5 views
13

RESTfulなクライアントから次のjsonを取得した場合、java.util.Dateをどのように非形式化するのですか? (それは別名(提供なしに可能である。ハードコーディング)形式、それは...私はエレガントで何を意味するかだ)Grails日付のアンマーシャリング

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

答えて

18

クリーンな方法は、可能な日付フォーマット用のカスタムのDataBinderを登録することが考えられます。

beans = { 
    "customEditorRegistrar"(CustomEditorRegistrar) 
} 
:また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のアプリ/ confに/春/ resources.groovyに春のBean定義を作成する必要があると思い

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

そして最終的にあなたのgrails-app/conf/Config.groovyに日付フォーマットを定義する:

grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"] 
+0

GroovyではなくJavaで(上記のように)これを実装する理由があるのだろうかと思いますか? Groovyではコードがかなり短くなります。 –

+0

Groovyが以前よりずっと遅かった時代にJavaで同様のコードを実装しました。 Groovyはこの問題で大きな飛躍を遂げました。私は怠惰から古くなったJavaコードを再利用しています;-) –

+0

あなたがやるべきことの古典的なコードです。もっともきれいな方法は、解析の試行を繰り返すのではなく、ロケールを使用してフォーマットを取得することです。 – Gepsens

5

新しいバージョンのGrails 2.3以降では、このタイプの機能がサポートされていることに注意してください。 あなたが2.3より前のGrailsのバージョンを使用するように強制されている場合CustomEditorRegistrar は非推奨の警告を排除するために、次のコードを使用して更新することができ、言ったことでDate Formats for Data Binding

を参照してください、ともできます@Component注釈を使用しています直接resources.groovyにBeanを追加するステップを削除/スキップします。 Grailsコンフィグレーションプロパティ名をgrails.databinding.dateFormatsに変更したわけではありません。これはGrails 2.3以降でサポートされているプロパティと一致します。最後に、私のバージョンは.gavaファイルではなく.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)) 
    } 
} 
+0

ありがとうございました。あなたは私の一日を救った。 @BindingFormatは正しい選択です。 –

関連する問題