स्पष्ट तरीका संभव तिथि प्रारूप के लिए एक कस्टम DataBinder रजिस्टर करने के लिए शायद है (यह संभव (उर्फ प्रदान किए बिना हार्ड-कोड है।) प्रारूप, कि मैं सुंदर ढंग से से क्या मतलब है ...)।
beans = {
"customEditorRegistrar"(CustomEditorRegistrar)
}
:
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 एप्लिकेशन के अंतर्गत/conf/वसंत/resources.groovy में एक वसंत-सेम परिभाषा बनाने की आवश्यकता होगी
और आखिर में अपने grails-app/conf/Config.groovy में दिनांक प्रारूपों को परिभाषित करें:
grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"]
स्रोत
2009-06-08 09:37:51
बस सोच रहा है कि क्या आप एक कारण है कि आप जावा में (ऊपर के रूप में) ग्रोवी के बजाय इसे लागू करना चाहते हैं? कोड ग्रोवी के साथ काफी छोटा होगा। –
मैंने पूर्व में जावा में कोड का एक समान टुकड़ा लागू किया था जब ग्रोवी अब उससे धीमा था। इस मामले में ग्रोवी ने एक महान छलांग लगाई। मैं सिर्फ पुराने जावा कोड को आलसी से बाहर कर रहा हूं ;-) –
कोड का अच्छा टुकड़ा, जो आप करेंगे, उसके क्लासिक। हालांकि पार्सिंग प्रयासों के माध्यम से फिर से शुरू करने के बजाय प्रारूप को पुनर्प्राप्त करने के लिए लोकेल का सबसे साफ तरीका उपयोग करना होगा। – Gepsens