Skip to content

Commit

Permalink
create few attribute converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heril Muratovic committed Oct 12, 2017
1 parent 78620e7 commit ea0d984
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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() );
}
}
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);
}
}
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);
}
}

0 comments on commit ea0d984

Please sign in to comment.