0

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?

4
  • 1
    but 00:00:00 GMT+0530 is not same time as 00:00:00.000Z Commented Mar 9, 2021 at 9:30
  • anyway try sdf1.setTimeZone(TimeZone.getTimeZone("IST")); but I would also strongly recommend java.time as done here by T.J.Crowder Commented Mar 9, 2021 at 9:42
  • The date is the same. As in: the point in time is the same. It is only printed differently. No, I don’t think you can easily avoid it being the same. :-) You can probably mimic the printing if it’s important to you. Commented Mar 9, 2021 at 14:59
  • 1
    I recommend that in Java you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use Instant from java.time, the modern Java date and time API. Commented Mar 9, 2021 at 15:52

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

+1 on the Java part. For the JavaScript, you first get the current timestamp with const d = new Date(); (really an instant, like java.util.Date), and then you can do either something like `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}` or you can rely on that en-CA uses the ISO format already and do d.toLocaleDateString('en-CA')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.