com.bigcommerce.catalog.models.DateTimeAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bigcommerce-sdk Show documentation
Show all versions of bigcommerce-sdk Show documentation
Java SDK for BigCommerce REST APIs
The newest version!
package com.bigcommerce.catalog.models;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class DateTimeAdapter extends XmlAdapter {
public static final String RFC_822_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
@Override
public DateTime unmarshal(final String timestamp) throws Exception {
if (StringUtils.isBlank(timestamp)) {
return null;
}
final DateTimeFormatter formatter = DateTimeFormat.forPattern(RFC_822_DATE_FORMAT);
return DateTime.parse(timestamp, formatter).toDateTime(DateTimeZone.UTC);
}
@Override
public String marshal(final DateTime dateTime) throws Exception {
if (dateTime == null) {
return null;
}
final DateTimeFormatter formatter = DateTimeFormat.forPattern(RFC_822_DATE_FORMAT);
return formatter.print(dateTime);
}
}