forked from hedza06/spring-boot-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Heril Muratovic
committed
Oct 12, 2017
1 parent
78620e7
commit ea0d984
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/com/starter/springboot/converters/LocalDateToSqlDateConverter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.starter.springboot.converters; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import java.time.LocalDate; | ||
import java.sql.Date; | ||
|
||
|
||
@Converter | ||
public class LocalDateToSqlDateConverter implements AttributeConverter<LocalDate, Date> { | ||
|
||
@Override | ||
public Date convertToDatabaseColumn(LocalDate localDate) { | ||
return ( localDate == null ? null : Date.valueOf(localDate) ); | ||
} | ||
|
||
@Override | ||
public LocalDate convertToEntityAttribute(Date date) { | ||
return ( date == null ? null : date.toLocalDate() ); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/starter/springboot/converters/LocalDateToUtilDateConverter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.starter.springboot.converters; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Date; | ||
|
||
|
||
@Converter | ||
public class LocalDateToUtilDateConverter implements AttributeConverter<LocalDate, Date> { | ||
|
||
@Override | ||
public Date convertToDatabaseColumn(LocalDate localDate) | ||
{ | ||
DateFormat dateFormatLD = new SimpleDateFormat("yyyy-MM-dd"); | ||
String strDate = dateFormatLD.format(localDate); | ||
try { | ||
DateFormat dateFormatDL = new SimpleDateFormat("yyyy-MM-dd"); | ||
return dateFormatDL.parse(strDate); | ||
} | ||
catch (ParseException e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public LocalDate convertToEntityAttribute(Date date) | ||
{ | ||
// format java.util.Date to String | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); | ||
String dateString = dateFormat.format(date); | ||
// format String to LocalDate | ||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d/MM/yyyy"); | ||
return LocalDate.parse(dateString, dateTimeFormatter); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/starter/springboot/converters/StringToDateConverter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.starter.springboot.converters; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
|
||
@Converter | ||
public class StringToDateConverter implements AttributeConverter<String, Date> { | ||
|
||
@Override | ||
public Date convertToDatabaseColumn(String s) | ||
{ | ||
try { | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
return dateFormat.parse(s); | ||
} | ||
catch (ParseException e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(Date date) { | ||
Date currentDate = new Date(); | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
return dateFormat.format(currentDate); | ||
} | ||
} |