Created
January 15, 2014 05:56
-
-
Save ritalin/8431508 to your computer and use it in GitHub Desktop.
Period (Joda Time) <-> PostgreSQL Interval converter for Doma(http://doma.seasar.org/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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