Created
October 8, 2016 15:56
-
-
Save raphaelLacerda/3934b1584bb8e6d3ae97d7b7aa1bc4cf to your computer and use it in GitHub Desktop.
This file contains 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 br.com.raphael.infra.jpa; | |
import java.time.Instant; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.util.Date; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
@Converter(autoApply = true) | |
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> { | |
@Override | |
public Date convertToDatabaseColumn(LocalDate data) { | |
if (data != null) | |
return Date.from(data.atStartOfDay().atZone(ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())).toInstant()); | |
return null; | |
} | |
@Override | |
public LocalDate convertToEntityAttribute(Date data) { | |
if (data != null) | |
return LocalDateTime.ofInstant(Instant.ofEpochMilli(data.getTime()), ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())) | |
.toLocalDate(); | |
return null; | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
import static org.junit.Assert.*; | |
import java.time.LocalDate; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class LocalDateConverterTest { | |
private LocalDateConverter localDateConverter; | |
@Before | |
public void init() { | |
localDateConverter = new LocalDateConverter(); | |
} | |
@Test | |
public void deveConverterParaDate() { | |
Date data = localDateConverter.convertToDatabaseColumn(LocalDate.of(2016, 10, 23)); | |
Assert.assertNotNull(data); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(data); | |
Assert.assertEquals(23, calendar.get(Calendar.DAY_OF_MONTH)); | |
//a porra do calendar é zero based | |
Assert.assertEquals(10, calendar.get(Calendar.MONTH)+1); | |
Assert.assertEquals(2016, calendar.get(Calendar.YEAR)); | |
} | |
@Test | |
public void deveConverterParaLocalDate() { | |
LocalDate data = localDateConverter.convertToEntityAttribute(new GregorianCalendar(2016, 9, 23).getTime()); | |
Assert.assertEquals(23, data.getDayOfMonth()); | |
Assert.assertEquals(10, data.getMonthValue()); | |
Assert.assertEquals(2016, data.getYear()); | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.util.Date; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
@Converter(autoApply = true) | |
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date> { | |
@Override | |
public Date convertToDatabaseColumn(LocalDateTime data) { | |
if (data != null) { | |
return Date.from(data.atZone(ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())).toInstant()); | |
} | |
return null; | |
} | |
@Override | |
public LocalDateTime convertToEntityAttribute(Date data) { | |
if (data != null) | |
return LocalDateTime.ofInstant(Instant.ofEpochMilli(data.getTime()), | |
ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())); | |
return null; | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
import java.time.LocalDateTime; | |
import java.time.LocalTime; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class LocalDateTimeConverterTest { | |
private LocalDateTimeConverter localDateTimeConverter; | |
@Before | |
public void init() { | |
localDateTimeConverter = new LocalDateTimeConverter(); | |
} | |
@Test | |
public void deveConverterParaDate() { | |
Date data = localDateTimeConverter.convertToDatabaseColumn(LocalDateTime.of(2016, 10, 23, 11, 53)); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(data); | |
Assert.assertEquals(11, calendar.get(Calendar.HOUR_OF_DAY)); | |
Assert.assertEquals(53, calendar.get(Calendar.MINUTE)); | |
Assert.assertEquals(23, calendar.get(Calendar.DAY_OF_MONTH)); | |
//a porra do calendar é zero based | |
Assert.assertEquals(10, calendar.get(Calendar.MONTH)+1); | |
Assert.assertEquals(2016, calendar.get(Calendar.YEAR)); | |
data = localDateTimeConverter.convertToDatabaseColumn(LocalDateTime.of(2014, 1, 12, 20, 37)); | |
calendar.setTime(data); | |
Assert.assertEquals(20, calendar.get(Calendar.HOUR_OF_DAY)); | |
Assert.assertEquals(37, calendar.get(Calendar.MINUTE)); | |
Assert.assertEquals(12, calendar.get(Calendar.DAY_OF_MONTH)); | |
//a porra do calendar é zero based | |
Assert.assertEquals(1, calendar.get(Calendar.MONTH)+1); | |
Assert.assertEquals(2014, calendar.get(Calendar.YEAR)); | |
} | |
@Test | |
public void deveConverterParaLocalTime() { | |
LocalDateTime data = localDateTimeConverter.convertToEntityAttribute(new GregorianCalendar(2016, 9, 23, 11, 53).getTime()); | |
Assert.assertEquals(11, data.getHour()); | |
Assert.assertEquals(53, data.getMinute()); | |
Assert.assertEquals(23, data.getDayOfMonth()); | |
Assert.assertEquals(10, data.getMonthValue()); | |
Assert.assertEquals(2016, data.getYear()); | |
data = localDateTimeConverter.convertToEntityAttribute(new GregorianCalendar(2014, 0, 12, 20, 37).getTime()); | |
Assert.assertEquals(20, data.getHour()); | |
Assert.assertEquals(37, data.getMinute()); | |
Assert.assertEquals(12, data.getDayOfMonth()); | |
Assert.assertEquals(1, data.getMonthValue()); | |
Assert.assertEquals(2014, data.getYear()); | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
import java.time.Instant; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.LocalTime; | |
import java.time.ZoneId; | |
import java.util.Date; | |
import javax.persistence.AttributeConverter; | |
import javax.persistence.Converter; | |
@Converter(autoApply = true) | |
public class LocalTimeConverter implements AttributeConverter<LocalTime, Date> { | |
@Override | |
public Date convertToDatabaseColumn(LocalTime data) { | |
if (data != null) | |
return Date.from(data.atDate(LocalDate.now()).atZone(ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())).toInstant()); | |
return null; | |
} | |
@Override | |
public LocalTime convertToEntityAttribute(Date data) { | |
if (data != null) | |
return LocalDateTime.ofInstant(Instant.ofEpochMilli(data.getTime()), ZoneId.of(TiposTimeZone.BRASIL.getTimeZone())) | |
.toLocalTime(); | |
return null; | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
import java.time.LocalTime; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class LocalTimeConverterTest { | |
private LocalTimeConverter localTimeConverter; | |
@Before | |
public void init() { | |
localTimeConverter = new LocalTimeConverter(); | |
} | |
@Test | |
public void deveConverterParaDate() { | |
Date data = localTimeConverter.convertToDatabaseColumn(LocalTime.of(10, 53)); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(data); | |
Assert.assertEquals(10, calendar.get(Calendar.HOUR_OF_DAY)); | |
Assert.assertEquals(53, calendar.get(Calendar.MINUTE)); | |
data = localTimeConverter.convertToDatabaseColumn(LocalTime.of(15, 37)); | |
calendar.setTime(data); | |
Assert.assertEquals(15, calendar.get(Calendar.HOUR_OF_DAY)); | |
Assert.assertEquals(37, calendar.get(Calendar.MINUTE)); | |
} | |
@Test | |
public void deveConverterParaLocalTime() { | |
LocalTime data = localTimeConverter.convertToEntityAttribute(new GregorianCalendar(2016, 9, 23, 11, 53).getTime()); | |
Assert.assertEquals(11, data.getHour()); | |
Assert.assertEquals(53, data.getMinute()); | |
data = localTimeConverter.convertToEntityAttribute(new GregorianCalendar(2016, 9, 23, 15, 37).getTime()); | |
Assert.assertEquals(15, data.getHour()); | |
Assert.assertEquals(37, data.getMinute()); | |
} | |
} |
This file contains 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 br.com.raphael.infra.jpa; | |
public enum TiposTimeZone { | |
BRASIL("America/Sao_Paulo"); | |
private String timeZone; | |
TiposTimeZone(String timeZone) { | |
this.timeZone = timeZone; | |
} | |
public String getTimeZone() { | |
return timeZone; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esse código é em minha homenagem? Nasci no dia 23 de Outubro às 11:30. Tá ai o bug! :P