Skip to content

Instantly share code, notes, and snippets.

@rponte
Forked from garcia-jj/PersistentLocalDate.java
Created June 4, 2014 08:05
Show Gist options
  • Select an option

  • Save rponte/a9bbc9b3850432836874 to your computer and use it in GitHub Desktop.

Select an option

Save rponte/a9bbc9b3850432836874 to your computer and use it in GitHub Desktop.
package fx.time;
import java.sql.Date;
import java.time.LocalDate;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class PersistentLocalDate implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate value) {
return value == null ? null : Date.valueOf(value);
}
@Override
public LocalDate convertToEntityAttribute(Date value) {
return value == null ? null : value.toLocalDate();
}
}
package fx.time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class PersistentLocalDateTime implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public java.sql.Timestamp convertToDatabaseColumn(LocalDateTime value) {
return value == null ? null : Timestamp.valueOf(value);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp value) {
return value == null ? null : value.toLocalDateTime();
}
}
package fx.time;
import java.sql.Date;
import java.time.YearMonth;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class PersistentYearMonth implements AttributeConverter<YearMonth, Date> {
@Override
public Date convertToDatabaseColumn(YearMonth value) {
return value == null ? null : Date.valueOf(value.atDay(1));
}
@Override
public YearMonth convertToEntityAttribute(Date value) {
return value == null ? null : YearMonth.from(value.toLocalDate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment