0

There is a Date of birth property in my request POJO which is String. In my response POJO, date of birth property is joda's DateTime. I converted it into DateTime by using "new DateTime(requestModel.getDateOfBirth())". Though, it is only accepting the date format(which is in String) as "yyyy-mm-dd". When I change the request date format, it throws "500

Blockquote

". Here is my POJOs:

RequestModel.java

public class RequestModel {
@NotNull(message = "dateOfBirth cannot be null")
private String dateOfBirth;

public RequestModel(String dateOfBirth) {
    super();
    this.dateOfBirth = dateOfBirth;
}
public String getDateOfBirth() {
    return dateOfBirth;
}

}

ResponseModel.java

public class ResponseModel {
private DateTime dateOfBirth;
public ResponseModel( DateTime dateOfBirth) {
    super();
    this.dateOfBirth = dateOfBirth;
}
public DateTime getDateOfBirth() {
    return dateOfBirth;
}
}

MyController.java

    @PostMapping(consumes = {
                            MediaType.APPLICATION_JSON_VALUE,
                            MediaType.APPLICATION_XML_VALUE
                        },
             produces = {
                        MediaType.APPLICATION_JSON_VALUE,
                        MediaType.APPLICATION_XML_VALUE
                        })
public ResponseEntity<ResponseModel> create(@Valid @RequestBody RequestModel requestModel) {
    ResponseModel response = new ResponseModel(new DateTime(requestModel.getDateOfBirth()));
    return new ResponseEntity<ResponseModel>(response, HttpStatus.CREATED);
}

This is the post JSON request which works fine:

{
"dateOfBirth" : "2012-12-12"
}

The following request body throws error:

{
"dateOfBirth" : "12-12-2012",
}

The error is:

{
"timestamp": "2020-03-14T19:09:51.647+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Invalid format: \"12-12-2012\" is malformed at \"12\"",
"path": "/myPath/"
}

I want to know how can I improve the controller logic or the design to accept any Date format in the API? Thanks for helping out

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Siddharth Shankar
  • 489
  • 1
  • 10
  • 21
  • 1
    Does this answer your question? [Converting a date string to a DateTime object using Joda Time library](https://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library) – Ole V.V. Mar 15 '20 at 05:48

2 Answers2

2

First, since you are getting a date without time of day, you should prefer Joda-Time’s LocalDate over DateTime.

Second, the format that the constructor accepts (and the LocalDate constructor as well) is ISO 8601, the international standard. I should say that requiring your client to use this format in their request would be nice and recommended.

But if you insist: Use a formatter for parsing the string from the request into a LocalDate.

    DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd-MM-yyyy");

    String requestDateOfBirth = "12-12-2012";
    LocalDate dateOfBirth = LocalDate.parse(requestDateOfBirth, dateFormatter);

    System.out.println("Date of birth: " + dateOfBirth);

Output from this snippet is:

Date of birth: 2012-12-12

In 12-12-2012 I didn’t know which was day of month and which was month, but you can swap dd for day of month and MM for month in the format pattern string if required.

And finally, if this is new code, you will probably prefer to use java.time, the modern Java date and time API, over Joda-Time. The official recommendation from the Joda-Time project says to migrate.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

Date(String date) is deprecated constructor.

You can use Calendar class to build DateTime object

Calendar c = Calendar.getInstance();
c.set(2000,11,2);
Date d = c.getTime();
System.out.println(d.toString());

This create a Date object on 2 December 2000.

You need to split and manipulate your input string before pass it to get method

set method accept 3 argument.

set(int year, int month, int date) Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

See calendar API for more

https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

ciro
  • 771
  • 1
  • 8
  • 30
  • Thanks @ciro, I am currently using joda.DateTime. I can update it to Calendar API. My main question is how could I use the date(String) request at runtime? I mean the request which the API is getting is not always in same format. It may be "dd-mm-yyyy" or "mm-dd-yyyy" or "yyyy-mm-dd". Splitting it and manipulating it could work upto certain extent but is there a way using Joda which I can do that on runtime so that I do not have to worry about the format of the request hitting my API? – Siddharth Shankar Mar 14 '20 at 20:16
  • Regardless of the API you use, you must know the format of the data that arrives to you. Because while for the year you can establish based on the 4 digits, for month and day you cannot. If 01/01/2010 arrives How do you distinguish month by day? – ciro Mar 14 '20 at 21:08