angaraeski
angaraeski

Reputation: 468

How to Convert JavaScript Date to Date in Java?

I need to convert JsDate to java.util.Date. I searched but I couldn't find anything. So could you help me with this problem?

Edit: I do this conversion process on GWT screen. I have Datepicker on screen and it gives me JsDate value when I use it's getValue() method. So I'm supposed to put this value into the property of an object which has Date type.

ObjectName.setDate(PickerName.getValue());

I hope my edit will be more clear.

Edit2:

This line is the solution of my problem:

myObject.setDate(new Date((long) myPicker.getValue().getTime()));

Upvotes: 44

Views: 65656

Answers (7)

Anonymous
Anonymous

Reputation: 86323

ISO 8601 and java.time

ISO 8601 is the international standard for date and time including date and time formats. About any programming language has support for it, including both JavaScript and Java.

In JavaScript produce a string in ISO 8601 format using Date.toISOString(). We don’t need any formatter.

  var d = new Date();
  var n = d.toISOString();
  console.log(n);

The result is somewhat human readable as long as you remember that it’s in UTC, denoted by the trailing Z.

In Java parse the string using Instant.parse(). We don’t need to specify any formatter here either.

    String stringFromJavaScript = "2021-07-12T05:54:03.365Z";
    Instant inst = Instant.parse(stringFromJavaScript);
    System.out.println(inst);

Output:

2021-07-12T05:54:03.365Z

The question asked for a java.util.Date for the result from a date picker. We should avoid using java.util.Date for this both because despite the name a Date does not represent a date and because the Date class is poorly designed and long outdated. For a date without time of day a LocalDate is appropriate:

    LocalDate date = inst.atZone(ZoneId.systemDefault()).toLocalDate();
    System.out.println(date);

In my time zone the output was:

2021-07-12

The conversion is time zone dependent and will only be correct if the default time zone of the JVM (or which time zone you pass to atZone()) is the same as used by the date picker.

If you do need a Date for a legacy API not yet upgraded to java.time:

    Date oldfashionedDate = Date.from(inst);
    System.out.println(oldfashionedDate);

Mon Jul 12 07:54:03 CEST 2021

Links

Upvotes: 5

Akhilesh Sinha
Akhilesh Sinha

Reputation: 881

JS Date -- new Date() Wed Aug 14 2019 14:54:38 GMT+0530 (India Standard Time)

Java Date -- new Date().toISOString() "2019-08-14T09:25:50.136Z"

Upvotes: 0

sunxs
sunxs

Reputation: 926

You may want this:

java:
String jsDate="2013-3-22 10:13:00";
Date javaDate=new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(jsDate);
System.out.println(javaDate);

Upvotes: 3

Valentin Waeselynck
Valentin Waeselynck

Reputation: 6051

If people like me are forced to parse a JS-formatted date string (as the result of (new Date()).toString() in JavaScript), here is the SimpleDateFormat spec I used:

DateFormat jsfmt = new SimpleDateFormat("EE MMM d y H:m:s 'GMT'Z (zz)");

If you have control of the producer of the dates, I concur that using timestamps or at least .toUTCString() is definitely more robust.

Upvotes: 15

epoch
epoch

Reputation: 16615

You can create a java.util.Date object from the 'time since epoch' value of the JS Date

javascript

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

java

// get value from client (ajax, form, etc), and construct in Date object

long valueFromClient = ...

Date date = new Date(valueFromClient);

String formatted = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

Upvotes: 36

AlexR
AlexR

Reputation: 115378

The best way of dates conversion is using time in milliseconds, UTC. Both JS Date object and java.util.Date class support conversion to milliseconds (getTime()) and instantiating from milliseconds (using constructor).

Upvotes: 43

Eivind Eidheim Elseth
Eivind Eidheim Elseth

Reputation: 2354

I would suggest using the DateFormat parse method (doc can be found here). It can parse a string representation of a date and return a java.util.Date.

Upvotes: 0

Related Questions