Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse date with SimpleFramework

I receive an XML response with an attribute which contains following value:

Wed Sep 05 10:56:13 CEST 2012

I have defined in my model class a field with annotation:

@Attribute(name = "regDate")
private Date registerDate;

However it throws an exception:

java.text.ParseException: Unparseable date: "Wed Sep 05 10:56:13 CEST 2012" (at offset 0)

Is it possible to define date format in SimpleFramework's annotations ?

What format should cover this date string ?

like image 829
hsz Avatar asked Sep 05 '12 09:09

hsz


People also ask

How to parse Date in SimpleDateFormat?

SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy"); Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.

How to format Java Date in yyyy MM dd?

A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.

How to parse Date value in Java?

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String dateString = format. format( new Date() ); Date date = format. parse ( "2009-12-31" ); The string passed as parameter to the SimpleDateFormat class is a pattern that tells how the instance is to parse and format dates.

How to parse different Date formats in Java?

How to parse a date? String input = "Thu Jun 18 20:56:02 EDT 2009"; SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy"); Date date = parser. parse(input); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter. format(date); ...


1 Answers

SimpleXML only supports some DateFormat's:

  • yyyy-MM-dd HH:mm:ss.S z
  • yyyy-MM-dd HH:mm:ss z
  • yyyy-MM-dd z
  • yyyy-MM-dd

(For the meaning of each character see SimpleDateFormat API Doc (Java SE 7))

However it's possible to write a custom Transform who deals with other formats:

Transform

public class DateFormatTransformer implements Transform<Date>
{
    private DateFormat dateFormat;


    public DateFormatTransformer(DateFormat dateFormat)
    {
        this.dateFormat = dateFormat;
    }



    @Override
    public Date read(String value) throws Exception
    {
        return dateFormat.parse(value);
    }


    @Override
    public String write(Date value) throws Exception
    {
        return dateFormat.format(value);
    }

}

Corresponding Annotation

@Attribute(name="regDate", required=true) /* 1 */
private Date registerDate;

Note 1: required=true is optional

How to use it

// Maybe you have to correct this or use another / no Locale
DateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss z YYYY", Locale.US);


RegistryMatcher m = new RegistryMatcher();
m.bind(Date.class, new DateFormatTransformer(format));


Serializer ser = new Persister(m);
Example e = ser.read(Example.class, xml);
like image 88
ollo Avatar answered Oct 17 '22 04:10

ollo