I am writing rest apis for my project to create, retrieve and modify values to my database. The model has an sql.Date field which while doing POST request if I give date as for example 2018-01-35(yyyy-MM-dd), it is auto converting to 2018-02-04. I want to avoid this so that it can tell me that the given date is invalid.
I searched in SO and tried this approach,
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dob;
but this did nothing, so then I tried another method
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
but this only checks for format like if i give value as just "1993", it gives error but does not check for "1993-01-35".
I also tried,
public static boolean dateChecker(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setLenient(false);
calendar.setTime(date);
try{
calendar.getTime();
}catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
return true;
}
But when I debug it, this method is already getting "1993-01-35" as "1993-02-04".
The model is,
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
private Date dob;
This is my POST method,
@RequestMapping(value = "/patient",method = RequestMethod.POST,consumes = "application/json")
public ResponseEntity<?> createPatient(@RequestBody Patient patient)
{
if(patient.getPatientId()!=null)
{
Patient patient1 = patientRepository.findOne(patient.getPatientId());
if(patient1!=null)
{
return new ResponseEntity("Patient for the given patient Id already exists.",HttpStatus.BAD_REQUEST);
}
}
System.out.println(patient.getDob());//Here I am getting 1993-02-04 when i give date as 1993-01-35
Please help me solve this.