Menu

#20 Customizable Date and Number Transform classes

open
nobody
None
5
2012-10-01
2012-10-01
No

Could you expand existed Transform classes or add new public ones for custom formats?
This is important especially for Date and Number types which often require conversion from a non-standard format.
Right now I'm ending up on implementing in my applications the same code again and again:

import org.simpleframework.xml.transform.Transform;

import java.text.DateFormat;
import java.util.Date;

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

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

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

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

and then using them like this:
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Transform dateTransform = new DateFormatTransformer(dateFormat);
RegistryMatcher matcher = new RegistryMatcher();
matcher.bind(Date.class, dateTransform);
Serializer s = new Persister(matcher);

which is a bit clumsy.

Discussion


Log in to post a comment.

Auth0 Logo