0

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);
learn program
  • 93
  • 1
  • 3
  • 9

2 Answers2

2

Allow me to suggest you use the modern classes LocalDate and LocalTime for your date and time. Then it’s straightforward to do for example

if (appointmentDate.isAfter(LocalDate.now().plusYears(5)) {
    throw new IllegalArgumentException("Too far into the future");
}

This will catch the 5-digit year and many other errors in the year value. Please set an appropriate limit yourself. You may similarly forbid dates in the past, maybe with a stricter limit. For the time this will be built-in since LocalTime only accepts times up to 23:59:59.999999999.

To combine both into one object

LocalDateTime startDateAndTime = LocalDateTime.of(appointmentDate, appointmentTime);

I have entered the code snippets off the top of my head, there could be a typo or two. If you cannot fix, please revert.

I haven’t got recent experience with Spring Boot. There’s more on using the modern Java date and time classes with Spring Boot and Jackson in this question: JSON Java 8 LocalDateTime format in Spring Boot. And no doubt still more in other places.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • that means, do i need to change the Date Object to DateTime Object in the Entity, because it when i do like the above you said, i get this error"Parameter value [2017-05-30T23:30] did not match expected type [java.util.Date (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [2017-05-30T23:30] did not match expected type [java.util.Date (n/a)]" – learn program Jun 13 '17 at 05:34
  • I was hoping someone with better knowledge of Spring-Boot would step in here. I believe there are different soliutions to your new error message. My general recommendation is to avoid the oldfashioned `Date` class completely (unless you have very good reasons). The modern classes are considerably more programmer friendly. – Ole V.V. Jun 13 '17 at 06:22
0

The first date format in your code is pointless, it is just converting to a Date(which was failing when I tried) and then immediately converting back to String anyway.

I've taken out the unnecessary lines from your example and posted my "working" example below.

        String dt = appointmentDate + " " + appointmentTime;
        Date startDateAndTime = null;

        try
        {
            startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
        } catch (ParseException e)
        {
            throw new IllegalArgumentException("Illegal date format");
        }
        System.out.println(startDateAndTime);

This tends to happen a lot when something is not working and you keep throwing code at it until you get entirely stuck. It's good to step back sometimes and try to work out what you are actually trying to achieve and then take another crack at it.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
J. Knight
  • 131
  • 9