Skip to content

Commit ea0d984

Browse files
author
Heril Muratovic
committed
create few attribute converters.
1 parent 78620e7 commit ea0d984

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.starter.springboot.converters;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
import java.time.LocalDate;
6+
import java.sql.Date;
7+
8+
9+
@Converter
10+
public class LocalDateToSqlDateConverter implements AttributeConverter<LocalDate, Date> {
11+
12+
@Override
13+
public Date convertToDatabaseColumn(LocalDate localDate) {
14+
return ( localDate == null ? null : Date.valueOf(localDate) );
15+
}
16+
17+
@Override
18+
public LocalDate convertToEntityAttribute(Date date) {
19+
return ( date == null ? null : date.toLocalDate() );
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.starter.springboot.converters;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
import java.text.DateFormat;
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.time.LocalDate;
9+
import java.time.format.DateTimeFormatter;
10+
import java.util.Date;
11+
12+
13+
@Converter
14+
public class LocalDateToUtilDateConverter implements AttributeConverter<LocalDate, Date> {
15+
16+
@Override
17+
public Date convertToDatabaseColumn(LocalDate localDate)
18+
{
19+
DateFormat dateFormatLD = new SimpleDateFormat("yyyy-MM-dd");
20+
String strDate = dateFormatLD.format(localDate);
21+
try {
22+
DateFormat dateFormatDL = new SimpleDateFormat("yyyy-MM-dd");
23+
return dateFormatDL.parse(strDate);
24+
}
25+
catch (ParseException e) {
26+
return null;
27+
}
28+
}
29+
30+
@Override
31+
public LocalDate convertToEntityAttribute(Date date)
32+
{
33+
// format java.util.Date to String
34+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
35+
String dateString = dateFormat.format(date);
36+
// format String to LocalDate
37+
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
38+
return LocalDate.parse(dateString, dateTimeFormatter);
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.starter.springboot.converters;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
import java.text.DateFormat;
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
10+
11+
@Converter
12+
public class StringToDateConverter implements AttributeConverter<String, Date> {
13+
14+
@Override
15+
public Date convertToDatabaseColumn(String s)
16+
{
17+
try {
18+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
19+
return dateFormat.parse(s);
20+
}
21+
catch (ParseException e) {
22+
return null;
23+
}
24+
}
25+
26+
@Override
27+
public String convertToEntityAttribute(Date date) {
28+
Date currentDate = new Date();
29+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
30+
return dateFormat.format(currentDate);
31+
}
32+
}

0 commit comments

Comments
 (0)