为什么我尝试将 Date 对象设置为在 Rhino 中执行的 JS 脚本时出现 "Invalid Date" 错误?

Why I obtain an "Invalid Date" error when I try to set a Date object into a JS script performed in Rhino?

我不太喜欢 JavaScript,我在尝试将简单的 JavaScript 代码执行到 Rhino 中时遇到了很大的问题(https://developer.mozilla.org/it/docs/Rhino) 这是一个 Java 脚本实现,允许在 Java 应用程序中执行 JS 代码。在我的具体案例中,Rhino 嵌入到 WSO2 ESB(在 Java 中开发的 ESB 工具)。

我可以在我的 Java\WSO2 ESB 应用程序中执行我的 JS 代码,但是我在尝试创建 JavaScript Date 对象时遇到了一些问题。在这种情况下它不能工作。

如果在我的 JS 代码中有类似的东西:

var dateCurrentOriginalForecast = new Date('2017-11-09 06:00:00');

如果我在浏览器中执行它但在 Rhino 中执行它,它工作正常我在 Java 控制台中收到以下错误消息:

TID: [-1234] [] [2017-11-09 15:55:43,610]  INFO {org.apache.synapse.mediators.bsf.ScriptMessageContext} -  dateCurrentOriginalForecast: Invalid Date {org.apache.synapse.mediators.bsf.ScriptMessageContext}
TID: [-1234] [] [2017-11-09 15:55:43,610] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} -  The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#139) in <Unknown Source> at line number 139
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:345)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:265)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:233)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:97)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:260)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:775)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:282)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:554)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:188)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:262)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#139)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
        at org.mozilla.javascript.NativeDate.toISOString(NativeDate.java:398)
        at org.mozilla.javascript.NativeDate.execIdCall(NativeDate.java:384)
        at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
        at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:85)
        at org.mozilla.javascript.gen._Unknown_Source__1177._c_script_0(<Unknown Source>:139)
        at org.mozilla.javascript.gen._Unknown_Source__1177.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__1177.call(<Unknown Source>)
        at org.mozilla.javascript.gen._Unknown_Source__1177.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 16 more

我也尝试过其他格式,例如:

var dateCurrentOriginalForecast = new Date('2017-11-09 06:00:00');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00.000Z');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00T');

但我仍然得到同样的错误。

为什么?问题是什么?我错过了什么?适用于 Rhino 的正确日期格式是什么?

此处有与 Date 对象和 Rhino 相关的类似内容,但没有解决方案:Javascript invalid date in iOS/Android 2.2

效果很奇怪....

尝试使用明确的日期部分:

new Date(year, month, day [, hour, minute, second, millisecond ])

new Date('2017','11','09','06','00','00')

它有 Date 的不同实现。如果字符串 returns NaNDate.parse 则该字符串不被解释为有效日期并且 new Date(string) 的调用将 return a date但是 InvalidDate.

拆分字符串日期的各个部分并单独设置它们,或者构建符合 Date 要求的字符串日期。