0

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.

Black
  • 81
  • 1
  • 14

2 Answers2

0

You could try doing something like this if the calendar.setLenient isn't working for you.

I just added some random number for the day for testing.

int day = 21;
    Calendar gmtCal = 
            Calendar.getInstance(TimeZone.getTimeZone("GMT"));

    gmtCal.set(Calendar.MONTH, Calendar.JANUARY);

    if(day <= gmtCal.getActualMaximum(Calendar.DAY_OF_MONTH)){
        gmtCal.set(1993, Calendar.JANUARY, day );
        System.out.println(gmtCal.get(Calendar.YEAR));
        System.out.println(gmtCal.get(Calendar.MONTH));
        System.out.println(gmtCal.get(Calendar.DAY_OF_MONTH));

        System.out.println(gmtCal.getTime());
    }
    else{
        System.out.println("Out of bounds");
    }

The First thing you would have to do is to set the month for the calendar and then do a test on the day that was entered. If it passes then then you can set the whole date and if not print out an error.

Note: when using Calendar.get, the year and day will be printed out correctly. However, the month will start at zero. So in this example the dot gets print out: 1993 (year) 0 (month) 21 (day).

Also the the sysout for getTime() will print out some extra information that you might not need.

I used this post as a reference, it might give you some extra help: Detect invalid date on Calendar.set()

I hope this helps you. Let me know if this needs tweeking

-1

Try util.date package for date.

  • just that or should I do something more? – Black Feb 22 '18 at 12:34
  • I tried util.Date like you said, but the issue still remains – Black Feb 22 '18 at 12:41
  • I have shared everything related to the problem. – Black Feb 22 '18 at 12:47
  • try removing @DateTimeFormat(pattern = "yyyy-MM-dd"). java will internal handle the format. use simpledateformat where you need particular format after retriving data. – laimil patel Feb 22 '18 at 12:48
  • Sorry for confusing you, my problem is when I am trying to insert the data to Mysql, it is converting the "1993-01-35" to "1993-02-04", so I dont want this to happen. Retrieving is not a problem at all – Black Feb 22 '18 at 12:50
  • so how you are inserting date. trying to convert date? – laimil patel Feb 22 '18 at 12:52
  • The Date is sent by the user in Json format with the post request, I take this json and I save it in database, spring takes care of inserting it into database. I am not converting it, I am taking it as is. – Black Feb 22 '18 at 12:55
  • you mention "Here I am getting 1993-02-04 when i give date as 1993-01-35" what that exactly means? why you are sending weird date. – laimil patel Feb 27 '18 at 11:59
  • I am not the only user of this api, this was the input of one of my friend who tried giving a wrong date but was accepted, so I want to prevent this if possible – Black Feb 27 '18 at 12:57
  • so your issue is :- java is converting date to the correct format.? right? – laimil patel Mar 01 '18 at 10:36
  • in this case what should i do to prevent it – Black Mar 01 '18 at 11:38
  • take it as a string. check each field. – laimil patel Mar 01 '18 at 12:11