admin管理员组文章数量:1295303
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 ...
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 ...
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 19, 2014 at 14:23 MikeMurkoMikeMurko 2,2331 gold badge28 silver badges56 bronze badges3 Answers
Reset to default 6Cast 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/~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);
same problem here, solved with:
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");
Object js = engine.eval("new java.util.Date();");
I do this in my project:
retValue = new Date(value.to(Long.class));
本文标签: javascriptNashorn NativeDate conversion to javautilDateStack Overflow
版权声明:本文标题:javascript - Nashorn NativeDate conversion to java.util.Date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741604943a2387919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论