4

When returning Javascript Date objects to Java with Nashorn on Java 8 like so:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");
Object js = engine.eval("new Date();");

With the following attempts I get exceptions:

  1. Date javaDate = (Date)js;

    jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to java.util.Date

  2. Date javaDate = js.to(Date.class);

    Cannot cast jdk.nashorn.internal.objects.NativeDate to java.util.Date

  3. Date javaDate = (Date)ScriptUtils.convert(js.to(NativeDate.class), Date.class);

    Cannot cast jdk.nashorn.internal.objects.NativeDate to java.util.Date

Back with Rhino I was simply using context.jsToJava(nativeDateObj, Date.class);.

Any ideas how I can actually cast this NativeDate when it's returned to Java?

P.S. If I do js.toString() then it gives me "[Date 2012-01-01T19:00:00.000Z]". I guess I could regex parse that ... but why-oh-why ...

3 Answers 3

6

Cast returned JavaScript object on jdk.nashorn.api.scripting.ScriptObjectMirror, then you will be able to access its properties in a "map-like" manner.

ScriptObjectMirror jsDate = (ScriptObjectMirror) engine.eval("new Date();")
long timestampLocalTime = (long) (double) jsDate.callMember("getTime"); 
// yes, js date returns timestamp in local time so you need to adjust it... ;)
int timezoneOffsetMinutes = (int) (double) jsDate.callMember("getTimezoneOffset");
// java.util.Date construcctor utilizes UTC timestamp
Date jDate = new Date(timestampLocalTime + timezoneOffsetMinutes * 60 * 1000);

See also: http://cr.openjdk.java.net/~sundar/jdk.nashorn.api/8u20/javadoc/jdk/nashorn/api/scripting/ScriptObjectMirror.html

However if you are about to use some "JavaScript class" frequently on the Java side - you may find it useful to define "overlay" interface to access javascript object's methods in more convenient way. See the following example:

public interface JsDateWrapper {
    long getTime();
    int getTimezoneOffset();
    // ...
}

Object jso = engine.eval("new Date();");
JsDateWrap jsDate = ((Invocable) engine).getInterface(jso, JsDateWrapper.class);
Date jDate = new Date(jsDate.getTime() + jsDate.getTimezoneOffset() * 60 * 1000);
3
  • 1
    Casting to Long and Integer doesn't work because both getTime and getTimezoneOffSet returns a double. Casting them to Double and ask for longValue() and intValue() then it works.
    – Aris2World
    Sep 8, 2017 at 13:42
  • 1
    @Aris2World - Actually the first part raised cast exceptions. I fixed it now. But I could have bet it worked when I posted it for the first time ;) (I usually built and run the samples). Anyway, thanks for Your notice. Sep 12, 2017 at 9:29
  • 1
    I'm sure about it. As stated in this answer stackoverflow.com/a/33126596/3148880, I suppose that the problem is that internal classes change without any notice. Anyway your solution is the best one, IMHO, and I'm using it with satisfaction so I'm glad to contribute to its correctness. Thank again.
    – Aris2World
    Sep 12, 2017 at 10:24
1

same problem here, solved with:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");
Object js = engine.eval("new java.util.Date();");
2
  • 2
    In this you're not generating a JavaScript Date object though.
    – MikeMurko
    Jun 1, 2016 at 13:43
  • Setting the full Date class name in the script (for the cases I want to create a new Date instance inside the script) helped me; new java.util.Date()
    – Youness
    Jul 30, 2018 at 22:22
1

I do this in my project:

retValue = new Date(value.to(Long.class));
1
  • you need to get the js class name:if ("Date".equalst(value.getClassName())
    – Lin Yaohui
    Jul 5, 2017 at 13:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.