[Bugfix] fixed brok Iso8601DateAdapter
This commit is contained in:
parent
c7654e93a5
commit
22ddb3bd74
|
@ -1,25 +1,46 @@
|
||||||
package li.strolch.model.xml;
|
package li.strolch.model.xml;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ch.eitchnet.utils.helper.StringHelper;
|
import ch.eitchnet.utils.helper.StringHelper;
|
||||||
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
|
|
||||||
|
|
||||||
public class Iso8601DateAdapter extends XmlAdapter<String, Date> {
|
public class Iso8601DateAdapter extends XmlAdapter<String, Date> {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Iso8601DateAdapter.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date unmarshal(String value) throws Exception {
|
public Date unmarshal(String value) throws Exception {
|
||||||
if (StringHelper.isEmpty(value))
|
if (StringHelper.isEmpty(value))
|
||||||
return null;
|
return null;
|
||||||
return ISO8601FormatFactory.getInstance().getDateFormat().parse(value);
|
try {
|
||||||
|
return DatatypeConverter.parseDateTime(value).getTime();
|
||||||
|
} catch (Exception e) {
|
||||||
|
IllegalArgumentException ex = new IllegalArgumentException("Failed to parse value: " + value, e);
|
||||||
|
logger.info(ex.getMessage(), ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String marshal(Date value) throws Exception {
|
public String marshal(Date value) throws Exception {
|
||||||
if (value == null)
|
if (value == null)
|
||||||
return StringHelper.EMPTY;
|
return null;
|
||||||
return ISO8601FormatFactory.getInstance().formatDate(value);
|
try {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(value);
|
||||||
|
String valueS = DatatypeConverter.printDateTime(cal);
|
||||||
|
return valueS;
|
||||||
|
} catch (Exception e) {
|
||||||
|
IllegalArgumentException ex = new IllegalArgumentException("Failed to format date: " + value, e);
|
||||||
|
logger.info(ex.getMessage(), ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue