Spring Mvc Wrong Date Format - spring

I try to Spring mvc date formater with binder like this
#InitBinder
public void binder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
But I need format "dd-MM-YYYY". If I use this format mysql save wrong date
Form Data: 07-12-1980
MySql Data: 0013-05-02

In your POJO try adding this onto your date object.
#DateTimeFormat(pattern = "dd/MM/yyyy")
private Date dob;
Source: here

Related

Convert String to LocalDateTime with zoned time

I received a String field in a topic with date and offset and I need to convert this String to a LocalDateTime by adding the offset. For example, if I received:
2021-07-20T19:00:00.000+02:00
I want to convert in LocalDateTime:
2021-07-20T21:00:00.000
And I have a Bean with custom object mapper for this purpose:
#Configuration
public class MyConfiguration {
#Bean
public MyCustomObjectMapper configure() {
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX");
final LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(formatter);
final LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(formatter);
final JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDateTime.class, dateTimeDeserializer);
javaTimeModule.addSerializer(LocalDateTime.class, dateTimeSerializer);
mapper.registerModule(javaTimeModule);
return new MyCustomObjectMapper (mapper);
}
}
But it doesn't work as I expect, since the resulting LocalDateTime offset disappears and is not added:
2021-07-20T19:00:00.000
How can I achieve this goal?
The LocalDateTime class is a date-time representation which is unaware of time zones and it's only logical that the LocalDateTimeDeserializer ignores any time zone information in the source data.
To account for the time zone you could use the InstantDeserializer.OFFSET_DATE_TIME deserializer (DateTimeFormatter.ISO_OFFSET_DATE_TIME is actually the format of the source date time you have) and have its result converted to LocalDateTime within a desired zone. This can be wrapped in a custom deserializer for ease of use, e.g.
class SmartLocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {
private final InstantDeserializer<OffsetDateTime> delegate = InstantDeserializer.OFFSET_DATE_TIME;
public SmartLocalDateTimeDeserializer() {
super(LocalDateTime.class);
}
#Override
public LocalDateTime deserialize(JsonParser p,
DeserializationContext ctxt) throws IOException, JsonProcessingException {
final OffsetDateTime result = delegate.deserialize(p, ctxt);
return result.atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
}
}
...
javaTimeModule.addDeserializer(LocalDateTime.class, new SmartLocalDateTimeDeserializer());

Unable to parse date using DateTimeFormatter

I am trying to parse a date string returned to me from DB to LocalDate using DateTimeFormatter. I am receiving the below exception.
String date = "2018-05-16 03:39:13.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime localDate= LocalDateTime.parse(date , formatter);
java.time.format.DateTimeParseException: Text '2018-05-16 03:39:13.0' could not be parsed at index 20
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) [rt.jar:1.8.0_171]
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) [rt.jar:1.8.0_171]
at java.time.LocalDateTime.parse(LocalDateTime.java:492) [rt.jar:1.8.0_171]
However the below code is working.
String date = "2018-05-16 03:39:13.0";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date newDate= formatter.parse(date);
Since you are using the fraction-of-seconds with fixed length of 3, it cannot be parsed with only one digit. Better use a DateTimeFormatterBuilder and implement variable fraction-of-second.
String date = "2018-05-16 03:39:13.0";
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendFraction(ChronoField.MILLI_OF_SECOND, 0, 3, true)
.toFormatter();
LocalDateTime localDate = LocalDateTime.parse(date, formatter);
System.out.println(formatter.format(localDate));
It could also be done by using optional fields to get variable fraction-of-second length:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSS][.SS][.S]");
If your intent is just to parse the String to LocalDateTime, this will work fine.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
Well this might do your job.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormateer {
public static void main(String[] args) throws ParseException{
String date = "2018-05-16 03:39:13.0";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.a", Locale.US);
Date newDate= formatter.parse(date);
String date1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.a", Locale. getDefault()). format(newDate);
Date t=ft.parse(date1);
ft.applyPattern("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(date);
System.out.println(ft.format(t));
}
}

#requestparam value = date spring boot

#Controller
#RequestMapping(value="/reservations")
public class ReservationController {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
#Autowired
private ReservationService reservationService;
#RequestMapping(method = RequestMethod.GET)
public String getReservation(#RequestParam(value="date", required=false) String dateString, Model model){
Date date = null;
if(dateString != null){
try {
date = DATE_FORMAT.parse(dateString);
} catch (ParseException pe) {
date = new Date();
}
}else{
date = new Date();
}
List<RoomReservation> roomReservationList = this.reservationService.getRoomReservationsForDate(date);
model.addAttribute("roomReservations", roomReservationList);
return "reservations";
}
}
I understand that the #RequestParam annotation is used to bind parameter values of query string to the controller method parameters. So for example, http://localhost:8080/reservations?date=2017-01-01. However, where does the value="date" come from? I dont see any value "date" inside my html page.
if you submit a form as method:"GET" (not POST) and form contains a input field named date then submitting this form will hit this handler method.

How to give default date values in requestparam in spring

#RequestMapping(value = "/getSettlements", method = RequestMethod.GET, headers = "Accept=application/json")
public #ResponseBody
Collection<Settlement> getSettlements
(#RequestParam(value = "startDate") String startDate,
#RequestParam(value = "endDate") String endDate,
#RequestParam(value = "merchantIds", defaultValue = "null") String merchantIds)
How to give today's date in defaultValue ? It only takes constant.
#InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
final CustomDateEditor dateEditor = new CustomDateEditor(df, true) {
#Override
public void setAsText(String text) throws IllegalArgumentException {
if ("today".equals(text)) {
setValue(new Date());
} else {
super.setAsText(text);
}
}
};
binder.registerCustomEditor(Date.class, dateEditor);
}
#RequestParam(required = false, defaultValue = "today") Date startDate
If you are using LocalDate, you can create a default value like this:
#RequestParam(name = "d", defaultValue = "#{T(java.time.LocalDate).now()}", required = true) LocalDate d)
I tried pretty much every option, even using interceptors. But from far the easiest solution was to use SpEL. For Example: defaultValue = "#{new java.util.Date()}"
Since you receive a string you can any date format you want and later on use formatting to extract the date

input date in jsp using nestedPath

I am facing currently a huge issue : I can not retrieve date input by user in JSP page.
JSP code :
<form:form method="POST" action="myAction">
<tr><td>Date</td>
<td>
<spring:nestedPath path="myClasse.startDate" >
<input type="text" name="startDate" value="<c:out value="${status.value}"/> "/></spring:nestedPath>
</td></tr>
I input date in all existing forms. my code part corresponding to the retrieval of startDate in the Controller:
System.out.println("date: " + myClasse.getStartDate());
give me null
Here are some details that can help :
I have this in my model class :
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "start_date", length = 19)
public Date getStartDate()
{
return this.startDate;
}
public void setStartDate(Date startDate)
{
this.startDate = startDate;
}
Service class :
session.createQuery("SELECT DISTINCT name where startDate=:startDate").setParameter("startDate", "startDate");
I found a solution that can fix this problem :
In my controller class I added this :
#InitBinder
public void initBinder(WebDataBinder binder)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Hope that will help people facing this issue :)

Resources