I am a newbie to Java. I was getting date and time in the following format in json,
{
"appointmentDate":"2017-05-30",
"appointmentTime":"23:30:00"
}
In the request, I was doing this,
@NotNull(message = "appointmentDate is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date appointmentDate;
@NotNull(message = "appointmentTime is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
private String appointmentTime;
In the above request class, I was using Date
class for getting date and considering time as String.
Then in service class, I am trying to convert string object into date object, then search in the table to find the list of appointments in the table.
// convert into string
String dateString = null;
SimpleDateFormat sdfr = new SimpleDateFormat("yyyy-MM-dd");
dateString = sdfr.format( appointmentDate );
String dt = dateString + " " + appointmenTime;
Date startDateAndTime = null;
try {
//startDateAndTime = yyyyMMddFormat.parse(yyyyMMddFormat.format(dt));
startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
} catch (ParseException e) {
throw new IllegalArgumentException("Illegal date format");
}
but the issue I am facing is, even when i entered the wrong date it giving me output. No parse exception is thrown on the error in the date.
{
"appointmentDate":"20171-05-30",
"appointmentTime":"231:30:00"
}
this is my constants
public static final String DATEANDTIME = "yyyy-MM-dd HH:mm:ss";
and this is my repository query,
@Query(value = "select COUNT(a.doctorId) from Appointment a WHERE a.doctorId = :doctorId AND a.scheduleFromTime = :appointmentDateTime")
Long findAppointmentExists(@Param("doctorId") Long doctorId,
@Param("appointmentDateTime") Date appointmentDateTime);