From: Kai Moritz Date: Mon, 21 Dec 2015 22:45:00 +0000 (+0100) Subject: Implemented to-String converters for MonthDay, YearMonth and ZoneOffset X-Git-Tag: jpa-converters-1.0.0~21 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=c6566a9706d8642d33eabceab0dbd679ed477ff6;p=jpa-converters Implemented to-String converters for MonthDay, YearMonth and ZoneOffset --- diff --git a/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java new file mode 100644 index 0000000..143ced1 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java @@ -0,0 +1,26 @@ +package de.juplo.jpa.converters; + +import java.time.MonthDay; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link MonthDay} into its {@link String} representation. + * @author Kai Moritz + */ +@Converter(autoApply = true) +public class MonthDayConverter implements AttributeConverter +{ + @Override + public String convertToDatabaseColumn(MonthDay monthday) + { + return monthday.toString(); + } + + @Override + public MonthDay convertToEntityAttribute(String string) + { + return MonthDay.parse(string); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java new file mode 100644 index 0000000..47718d9 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java @@ -0,0 +1,26 @@ +package de.juplo.jpa.converters; + +import java.time.YearMonth; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link YearMonth} into its {@link String} representation. + * @author Kai Moritz + */ +@Converter(autoApply = true) +public class YearMonthConverter implements AttributeConverter +{ + @Override + public String convertToDatabaseColumn(YearMonth yearmonth) + { + return yearmonth.toString(); + } + + @Override + public YearMonth convertToEntityAttribute(String string) + { + return YearMonth.parse(string); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java new file mode 100644 index 0000000..f948b54 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java @@ -0,0 +1,26 @@ +package de.juplo.jpa.converters; + +import java.time.ZoneOffset; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link ZoneOffset} into its {@link String} representation as ID. + * @author Kai Moritz + */ +@Converter(autoApply = true) +public class ZoneOffsetConverter implements AttributeConverter +{ + @Override + public String convertToDatabaseColumn(ZoneOffset offset) + { + return offset.getId(); + } + + @Override + public ZoneOffset convertToEntityAttribute(String string) + { + return ZoneOffset.of(string); + } +}