-4

We have a Java new Date(); which returns the date as below:

 Mon Feb 19 19:51:21 IST 2018

And the javascript Date() function returns the date in a format like below:

 Mon Feb 19 2018 20:31:37 GMT+0530 (India Standard Time)

I have a rest webservice which accepts the date in exact format as that of Java's date (Mon Feb 19 19:51:21 IST 2018).

In an ajax request, I am passing an xml content which has a date tag along with other xml tags like id and author. Example below. If I send an xml like below, the date field gets ignored as the Date format expected in the rest webservice is different than the one below.

<message>
    <autor>User2</autor>
    <date>Mon Feb 19 2018 20:02:03 GMT+0530 (India Standard Time)</date>
    <id>1</id>
    <message>Message2</message>
</message>
Awanish
  • 327
  • 1
  • 3
  • 14
  • 2
    *"The need is to match"* In what way? What has to match and how? Are you looking for the formatted dates to have the same format *(without formatting?)*? Or are you looking to compare the dates? If so, what needs to be compared? – Andreas Feb 19 '18 at 15:13
  • 2
    Java's `new Date()` doesn't return a String, what you see is a default format applied in the toString() method. – Klaimmore Feb 19 '18 at 15:18
  • Updated more details, please see if it helps.. – Awanish Feb 19 '18 at 15:22
  • 1
    Just curious whether your REST service interprets IST as Irish Summer Time, Israel Standard Time or India Standard Time. Those three and four letter time zone abbreviations are dangerous, very often not unique. Couldn’t you find a way to avoid depending on one? – Ole V.V. Feb 19 '18 at 15:37
  • And just to get it straight, are you asking for a way to produce the `java.util.Date` default format in a Javascript script? – Ole V.V. Feb 19 '18 at 15:40
  • 1
    Probably a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Feb 19 '18 at 20:31

1 Answers1

0

Use .getTime() to get date value in milliseconds in JS like this:

var date = new Date().getTime();

And then on your Java back-end use value in milliseconds from JS to create date object:

Date date = new Date(dateFromJS);

And you can also add some formating to it:

String formatted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
JSEvgeny
  • 2,550
  • 1
  • 24
  • 38
  • Thanks for the reply. I know this works, but I just wanted to know a simple way to format the date in JS before sending it to Java webservice. I have edited my post to add more details – Awanish Feb 19 '18 at 15:23
  • When communicating between two different languages, it is often most beneficial and the least complicated to format the messages in a way they both can understand without having to know special formatting rules. In the case of timestamps, milliseconds are just one step away from whatever formatting either one wants without either side having to know what the formatting is. – Taplar Feb 19 '18 at 15:48
  • [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) is the widely used set of standard formats for date and time interchange. I don’t know how to generate it from JavaScript, though. The modern Java date and time classes in [`java.time`](https://docs.oracle.com/javase/tutorial/datetime/) parse it as their default, without the need for any explicit formatter. – Ole V.V. Feb 20 '18 at 10:43