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
Related
We have api: call_summary/
{
"id": 2,
"number: "xyz",
"call_time": "2021-10-11T03:50:23Z"
}
We have multiple users with various timezones like ADT, EDT, IST, etc. When users access this API the call_time should change according to user timezone. I tried to use #JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "IST"), but this won't allow us to change the call_time dynamically.
Is there any way to do it using annotations or filters?
I would recommend storing call_time in two columns, UTC and users local time-zone.
By doing so, it will eliminate complexity and confusion at both ends (server and client)
Check the following link, it may help you: Pass browser timezone to the backend springboot application to generate reports with dates as per the browser timezone. According to the latter, you can use TimeZone as input to your controller. You could do something like the following:
#RestController
public class TestController {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
#GetMapping(value = "/test")
public String generate(TimeZone timezone) throws IOException {
return LocalDateTime.now().atZone(timezone.toZoneId()).format(formatter);
}
}
Alternatively, you could get the timezone from HttpServletRequest.
JSON Serialize is the best fit for this custom data conversion as per user or user role. I created the converter class and called by #JsonSerialize(converter = LocalDateTimeToStringConverter.class) and #JsonDeserialize(converter = StringToLocalDatetimeConverter.class). It worked as per my expection.
For reference, I attached the sample link below.
Custom conversion using JSON-Serialize-Deserialize
Currently I have a Spring application with some resources that receives different kinds of data. One of the data attributes its a Timestamp and the value is sent in the request. I am using Spring Data Jpa to persist the data in a Postgresql database.
This is how I have my object:
#JsonProperty(value = "control_initial_timestamp")
#Temporal(TemporalType.TIMESTAMP)
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
#Column(name = "ctrl_init_ts")
private Date controlInitTimestamp;
... Setters and Getters ...
My Request looks like this:
"record_insert_timestamp" : "2020-05-18 09:53:24.475"
In the database I receive this: 2020-05-18 05:53:24.475000
If you noticed, it changes the time of the whole timestamp.
Also, with Spring Data, all I am doing is object.save(objectlist); I am not doing any specially query.
Please let me know if I am missing anything.
Thanks,
Is the timezone different between your app server and database server? You can enforce timezone for date serialization in JsonFormat by passing timezone
I got the answer. Not what I expected, but actually how it works.
Databases will store Timestamps as per their timezone location of their remote server location and timezone, to maintain a correlation between all timestamps in the DB/tables/views...etc .
When retrieving the timestamp, if you have configure your current location or timezone or remote server timezone, then it will convert to that specific timezone.
There is no direct way to manipulate this, but its actually how it works.
Thanks,
Let us know what is the datatype defined in database (timestamptz/timestamp)
Refer below link on -- PostgreSQL timestamp
https://www.postgresqltutorial.com/postgresql-timestamp/
I am having a hard time trying to make a web service client work. It is a XML RPC specification. I am using Apache WS XML-RPC library, which I find full of holes that causes problem due to Serialization. I have to send a Date parameter for the library to add the tags , however the web service expects it with the TZ, that means adding -0500 at the end of the Date object. If I dont send it as Date Object, it wont add the tags and it will fail. And when trying to do this:
DateFormat df = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
String fecha = df.format(new Date());
Date date = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ").parse(fecha);
And using parameter date, and it always sends it as
<dateTime.iso8601>20130517T20:30:33</dateTime.iso8601>
Can't find a way for it to send it as Date object in the format above but with the -0500 at the end. Any help would be appreciated.
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)
how do you set the twitter4j date object in android? seems pretty straight forward in java but cant make this work on android, the twitter4j javadoc can be found here http://twitter4j.org/en/javadoc/twitter4j/Status.html
Date tweettime = result.getCreatedAt();
textview.setText(""+ tweettime);
Showing a date on Android is hardly different from showing it in a JVM.
Just use a SimpleDateFormat to specify the format of the date. Also, make sure that any UI changes (to your textview) are done on the UI Thread.
Twitter4j has a friendly helper class to transform the date created Date object into something more user readable. It's close to the format twitter uses to show the date;
I made a method you can use:
/*converts standard date to user readable date such as 5m ago, 30 mins ago, 1 hr ago etc*/
private String formatDate(Date create_date){
//twitter date format from json response: Wed Jul 31 13:15:10 EDT 2013
TimeSpanConverter converter = new TimeSpanConverter();
return converter.toTimeSpanString(create_date);
}
You will need to import: import twitter4j.util.TimeSpanConverter;
Try this:
TimeSpanConverter converter = new TimeSpanConverter();
dateTextView.setText(converter.toTimeSpanString(status.getCreatedAt()));