Skip to main content
added 127 characters in body
Source Link
Rafael Lima
  • 3.6k
  • 9
  • 63
  • 140

@Savior's answer looked very good but because I didn't really know how a new date converter would mingle among the others spring already has I went to a less elegant but easier solution. I added a set method in my POJO

 @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtFrom(final String date) { this.postedAtFrom = SIMPLE_DATE_FORMATLocalDateTime.parse(date, SIMPLE_DATE_FORMAT).atZone(ZoneId.systemDefault()).toInstant();   }   @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtTo(final String date) { this.postedAtTo = SIMPLE_DATE_FORMATLocalDateTime.parse(date, SIMPLE_DATE_FORMAT).atZone(ZoneId.systemDefault()).toInstant(); } private static final SimpleDateFormatDateTimeFormatter SIMPLE_DATE_FORMAT = new SimpleDateFormatDateTimeFormatterBuilder() .appendPattern("dd-MM-yyyy HH:mm:ss") .toFormatter(); 

the @DateTimeFormat is really irrelevant here but helps me to remember what is the format that string is supposed to come [this is a really big pojo]

@Savior's answer looked very good but because I didn't really know how a new date converter would mingle among the others spring already has I went to a less elegant but easier solution. I added a set method in my POJO

 @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtFrom(final String date) { this.postedAtFrom = SIMPLE_DATE_FORMAT.parse(date).toInstant();   }   @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtTo(final String date) { this.postedAtTo = SIMPLE_DATE_FORMAT.parse(date).toInstant(); } private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 

the @DateTimeFormat is really irrelevant here but helps me to remember what is the format that string is supposed to come [this is a really big pojo]

@Savior's answer looked very good but because I didn't really know how a new date converter would mingle among the others spring already has I went to a less elegant but easier solution. I added a set method in my POJO

 @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtFrom(final String date) { this.postedAtFrom = LocalDateTime.parse(date, SIMPLE_DATE_FORMAT).atZone(ZoneId.systemDefault()).toInstant(); } @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtTo(final String date) { this.postedAtTo = LocalDateTime.parse(date, SIMPLE_DATE_FORMAT).atZone(ZoneId.systemDefault()).toInstant(); } private static final DateTimeFormatter SIMPLE_DATE_FORMAT = new DateTimeFormatterBuilder() .appendPattern("dd-MM-yyyy HH:mm:ss") .toFormatter(); 

the @DateTimeFormat is really irrelevant here but helps me to remember what is the format that string is supposed to come [this is a really big pojo]

Source Link
Rafael Lima
  • 3.6k
  • 9
  • 63
  • 140

@Savior's answer looked very good but because I didn't really know how a new date converter would mingle among the others spring already has I went to a less elegant but easier solution. I added a set method in my POJO

 @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtFrom(final String date) { this.postedAtFrom = SIMPLE_DATE_FORMAT.parse(date).toInstant(); } @SneakyThrows @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss") public void setPostedAtTo(final String date) { this.postedAtTo = SIMPLE_DATE_FORMAT.parse(date).toInstant(); } private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 

the @DateTimeFormat is really irrelevant here but helps me to remember what is the format that string is supposed to come [this is a really big pojo]