Reputation: 86
I'm having problem to convert the xml object using JAXB. The date is in the format Sun Jan 30 16:08:23 BRT 18
, but need to convert to the Brazilian format 12-08-2009 16:08:23
.
The input is coming in format 2009-08-12 16:08:23
and want output 12-08-2009 16:08:23
.
Conversion class JAXB:
public class DateAdapter extends XmlAdapter<String, Date> {
Locale brasil = new Locale("pt", "BR");
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", brasil);
@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}
@Override
public Date unmarshal(String v) throws Exception, ParseException {
return dateFormat.parse(v);
}
}
Where i call the Adapter.
@XmlJavaTypeAdapter(DateAdapter.class)
private Date ultima_atualizacaoProduto;
Upvotes: 1
Views: 1130
Reputation: 9648
You can't use the same DateFormat
both both parsing as well as formatting. Assuming 30
is the Year in your question, here is how you can do it:
public class DateAdapter extends XmlAdapter<String, Date> {
private Locale brasil = new Locale("pt", "BR");
private final SimpleDateFormat SD1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final SimpleDateFormat SD2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
@Override
public String marshal(Date v) throws Exception {
return SD2.format(v);
}
@Override
public Date unmarshal(String v) throws Exception, ParseException {
return SD1.parse(v);
}
}
Input:
2009-08-12 16:08:23
Output:
12-08-2009 16:08:23
Upvotes: 1
Reputation: 1871
in general only use UTC time for DB and transfer and convert it with java8 java.time.*.
If JDK<8, you can use joda time api.
Upvotes: 0