Serialize Date: Swagger and Spring-MVC - java

I'm using swagger-ui and a stub server in Java Spring-MVC generated using swagger-codegen. I have a date format somewhere and if I enter some date, it always returns me the UNIX Date Time format. I would like to have the RFC-3339 format for the date and date-time. Does anyone knows how to do this?

I found the answer..
I had to overwrite the JsonFormat since I'm returning a JSON object..
Therefore, if you have the same problem as me, use:
#JsonFormat(pattern="...")
Date foo;
Do not forget to use import com.fasterxml.jackson.annotation.JsonFormat;

Related

Validate Date in the request body

I have a request body have date field and I want to validate the input of the date and return error code 400 BadRequest if the format is wrong.
If I use the JsonFormat then it throws error 500:
#JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
private Date dateAt;
So I changed the field to String and then parse and throw BadRequest format everywhere using the getDate()
I am not really satisfied with this approach. Is there any other elegant way to achieve this validation?
I think I will accept error 500 from JSON formatter and skip my custom exception.
Since Java 8, you should avoid using Date. Java 8 brought an entirely new date and time API.
According to your question, a suitable replacement for Date would be LocalDate, which is expected to be in the format yyyy-MM-dd.
Jackson has a module supporting the new date and time API and an exception will be thrown if the date is not in the correct format. When serializing, you want to make sure the WRITE_DATES_AS_TIMESTAMPS feature is disabled, so the values will be written according to the ISO 8601 format.

Jersey JSON Date formatting POJO Mapping

I have date stored in mysql db as DATETIME and the value is stored in db as
"2016-09-09 15:56:26"
I wanted to display in same format, I am using Jersey 1.19 with POJOMapping and I used Date format in pojo, but the value shown in API is as:
"createdDt":1473454586000
What is the right approach to display correct date format in GET API, should I use String?

JSON date changed

I have a web application where user can post the message to a restful API, so that that information can be saved in the database.
My problem when the data is sent from the UI, the date sent is "effStartDate":"2016-08-13" , but when i see the date value in the java code it is showing Fri Aug 12 20:00:00 EDT 2016.
I am using AngularJS,Spring and iBatis as the ORM tool. Attached are the screen shots with data sent from UI and what i see in the backend code.
can anyone help me with this?
You can add annotations to realize in the entity.
(Have to rely on JackJson`s jar)
and Then add in required fields
"#JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")"
You can treat the date as a string. Something like this
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
You can try this use date format from javascript code to send requests to a server by REST api.
For example:
effStartDate.toISOString();
The toISOString() method returns a string in simplified extended ISO format

Google Calendars API - setTimeMax causing error

I'm trying to retrieve a list of events from a google calendar, using the Java api (jar version v3-rev9-1.7.0-beta)
This code works fine
Events e = service.events().list("primary").
setMaxResults(3).
execute();
where service is a Calendar object.
However, if I add a setTimeMin or setTimeMax parameter, like this
Date now = new java.util.Date();
Events e = service.events().list("primary").
setTimeMin(new DateTime(now)).
setMaxResults(3).
execute();
it returns a failure message, "Bad Request".
(note that as of this version, the setTime functions take a google DateTime object. I've also tried with the previous version of the jar, which takes a string, but received the same message).
So I was just wondering if anyone has successfully used these functions - perhaps they're not supposed to be called in this manner? Is there a way to get more detail back on the error?
Thanks :)
DateTime startTime = new DateTime(new Date(), TimeZone.getDefault());
Sorts the problem
I also encountered this. It seems the format of the DateTime.toString() or DateTime.toStringRfc3339() methods are incorrect as input to setTimeMin().
The DateTime.toString() format is:
2012-07-04T21:02:16.590
yyyy-MM-dd'T'HH:mm:ss.SSS (SimpleDateFormat notation)
The format which it expects seems to be close to xsd:datetime format (whatever that is):
2012-07-04T21:02:16Z (zulu, gmt)
2012-07-04T21:02:16-07:00 (mst, -7h)
2012-07-04T21:02:16-0700 (it also works without the colon in the timezone)
yyyy-MM-dd'T'HH:mm:ssZ (SimpleDateFormat)
Formatting can be done with a SimpleDateFormat:
SimpleDateFormat FMT_TIME=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String fromTimeStr=FMT_TIME.format(new Date());
Events evts = client.events().list(cal.getUid()).setTimeMin(fromTimeStr)
.execute();
Now, since I'm using the older API, I'm not sure how this would be done if the only method is setTimeMin(DateTime), but this should get you closer.
The Google documentation or source should mention this somewhere.

JSON parse date and time?

Im having a little issue with parsing json date.
Here is what I would like to parse:
{"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"}
I am having trouble parsing "lastLatitudeUpdate" is it because there are spaces in between? Thanks in advance for the help.
Assuming you are on Android and therefore working with java (yes you don't mention that, only the tag in your question suggests it...)
Like mentioned here (and in various other places) you can parse a date in java using the SimpleDateFormat class:
SimpleDateFormat parserSDF=new SimpleDateFormat("M/d/yyyy h:m:s a");
Date d = parserSDF.parse(dateField,0);
Of course you have to first parse you json input with some library (e.g. standard library from json.org or Google gson) and then parse the string you'll get there for the field into a date.
Short answer: No, there is no way for the JSON engine to recognize a string as a Date object.
Long answer:
There is no 'date' type in JSON. However, this JSON is fine, the catch is that lastLatitudeUpdate will be parsed as a string. In order to convert this to a date you should try something like
var my_object= JSON.parse({"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"});
my_object.lastLatitudeUpdate= Date.parse(my_object.lastLatitudeUpdate)
This function will give a timestamp. However, you have to make sure the string is correctly recognized, you may have to do some extra work.
Some links for hints
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
http://www.java-samples.com/showtutorial.php?tutorialid=406
How are you parsing the date? In Chrome this seems to work fine:
new Date("5/21/2012 4:49:17 PM");
Mon May 21 2012 16:49:17 GMT-0400 (US Eastern Daylight Time)

Categories