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:
Date javaDate = (Date)js;
jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to java.util.Date
Date javaDate = js.to(Date.class);
Cannot cast jdk.nashorn.internal.objects.NativeDate to java.util.Date
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 ...