Reputation: 107
I want to pass the date from front-end to backend and then save to database. For front end- I am using javascript and jQuery for backend springboot, java8. When I am creating a date of this year in JavaScript
let setDate = new Date(2022, 11, 31);
console.log(setDate);
I get the output it like : Sat Dec 31 2022 00:00:00 GMT+0530 (India Standard Time)
In javascript:
reqObj.setDate = setDate;
the object in Java for this is String setDate
But when it comes to backend spring boot controller it becomes:
2022-01-30T18:30:00.000Z
(day is decreased by one/ timezone changes from IST to GMT) (datatype is String in java)
After getting the String I make some formatting changes as per my need:
private String getDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy");
String resultsDate = "";
try {
resultsDate = sdf1.format(sdf.parse(date));
} catch (Exception e) {
logger.error(this.getClass().getName(), ".getDate() Exception : {}" + e);
}
return resultsDate;
}
Timezone changes resulting in date change, but the date should be the same i.e. like (2022-01-31T00:00:00.000Z
)
How can I solve it from the front end?
Upvotes: 0
Views: 1867
Reputation: 86323
If I understand you correctly, on the server side you need the calendar date, 2022-12-31 in your example. Neither the time of day nor the time zone.
What you are getting in Spring, 2022-01-30T18:30:00.000Z
, is a point in time. The correct point in time by the way, but if I am correct that you didn’t need a point in time, then this is not very useful. It’s only a little bit of guesswork on my part: Your browser knows your time zone setting, India Standard Time, and therefore does two things based on it: (1) It constructs a JavaScript Date
representing the start of day in your time zone. (2) It prints the point in time represented by the JavaScript Date
in your time zone. The browser further assumes that the server does not know the client time zone, and it therefore converts the point in time to UTC, which gives 2022-01-30T18:30:00.000Z
. The trailing Z
is pronounced Zulu and means UTC.
So I suppose that what you need to do is to persuade the JavaScript code in your client to send the date without time of day and without time zone. Since I neither know your JavaScript code nor very much JavaScript at all, and cannot tell you the details here.
Use java.time. As a slight aside, I recommend that in Java you use java.time, the modern Java date and time API. Assume that you get to send only 2022-12-31
from your client, parsing it into a date in Java is very easy:
String dateString = "2022-12-31";
LocalDate date = LocalDate.parse(dateString);
System.out.println("Date parsed to: " + date);
Output:
Date parsed to: 2022-12-31
LocalDate
is a date without time of day, so what I think you need here. It parses the format 2022-12-31
natively and without any explicit formatter. The format is known as ISO 8601.
Links
Upvotes: 3