How to change the locale of the date converter in JSefa? My locale is pt_BR and the date I want to convert is en_US. I am using a CSV file.
This is my field:
@CsvField(pos = 7, format = "dd MMM yyyy hh:mm:ss a")
private Date createdDate;
When it tries to convert from
03 Jun 2014 01:00:50 AM
Everything works fine, because Junho (pt_BR) and June (en_US) starts with the same three letters. However, when it tries to convert from
31 May 2014 01:05:35 AM
A exception is raised.
GRAVE: Servlet.service() for servlet [socReporterServlet] in context with path [] threw exception [Request processing failed; nested exception is org.jsefa.DeserializationException: Error while deserializing
Position: [22,122]
Object Path: Incident[createdDate]] with root cause
org.jsefa.common.converter.ConversionException: Wrong date format: 31 May 2014 01:05:35 AM
I think it is because the word in Brazil for this month is Maio, not starting with the three letters above.
How can I resolve it?
Thanks
Right after ask the question I found this: https://github.com/oasits/JSefa/blob/master/src/main/java/org/jsefa/common/converter/DateConverter.java.
This is my code.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.jsefa.common.converter.ConversionException;
import org.jsefa.common.converter.SimpleTypeConverter;
import org.jsefa.common.converter.SimpleTypeConverterConfiguration;
public class DateConverter implements SimpleTypeConverter {
private static final String DEFAULT_FORMAT = "dd MMM yyyy hh:mm:ss a";
private final SimpleDateFormat dateFormat;
public static DateConverter create(SimpleTypeConverterConfiguration configuration) {
return new DateConverter(configuration);
}
protected DateConverter(SimpleTypeConverterConfiguration configuration) {
String format = getFormat(configuration);
try {
this.dateFormat = new SimpleDateFormat(format, Locale.US);
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
} catch (Exception e) {
throw new ConversionException("Could not create a " + this.getClass().getName() + " with format "
+ format, e);
}
}
@Override
public Object fromString(String str) {
try {
return (Date) dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString(Object obj) {
return dateFormat.format((Date) obj);
}
protected String getDefaultFormat() {
return DateConverter.DEFAULT_FORMAT;
}
private String getFormat(SimpleTypeConverterConfiguration configuration) {
if (configuration.getFormat() == null) {
return getDefaultFormat();
}
if (configuration.getFormat().length != 1) {
throw new ConversionException("The format for a DateConverter must be a single String");
}
return configuration.getFormat()[0];
}
}