Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created January 15, 2014 05:56
Show Gist options
  • Save ritalin/8431508 to your computer and use it in GitHub Desktop.
Save ritalin/8431508 to your computer and use it in GitHub Desktop.
Period (Joda Time) <-> PostgreSQL Interval converter for Doma(http://doma.seasar.org/)
package sample.converters;
import java.sql.SQLException;
import org.joda.time.Period;
import org.postgresql.util.PGInterval;
import org.seasar.doma.ExternalDomain;
import org.seasar.doma.jdbc.domain.DomainConverter;
@ExternalDomain
public class JodaTimePeriodConverter implements DomainConverter<Period, String> {
@Override
public String fromDomainToValue(Period domain) {
PGInterval interval = new PGInterval(
domain.getYears(), domain.getMonths(), domain.getDays(),
domain.getHours(), domain.getMinutes(), domain.getSeconds()
);
return interval.getValue();
}
@Override
public Period fromValueToDomain(String value) {
try {
PGInterval interval = new PGInterval(value);
return new Period(
interval.getYears(), interval.getMonths(), 0, interval.getDays(),
interval.getHours(), interval.getMinutes(), (int)interval.getSeconds(), 0
);
} catch (SQLException e) {
throw new RuntimeException("Invalid value as interval is passed.", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment