为什么我获得"无效日期"我尝试将Date对象设置为在Rhino中执行的JS脚本时出错?

时间:2017-11-09 15:16:39

标签: javascript java wso2esb rhino

我不是那么喜欢JavaScript而且我在尝试将一个简单的JavaScript代码导入 Rhino https://developer.mozilla.org/it/docs/Rhino)时遇到了很大的问题,这是一个允许执行JS代码的JavaScript实现一个Java应用程序。在我的特定情况下, 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$1.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

2 个答案:

答案 0 :(得分:2)

有效地说这很奇怪......

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

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

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

答案 1 :(得分:1)

它有Date的不同实现。如果字符串的Date.parse返回NaN,则字符串不会被解释为有效日期,new Date(string)的调用将返回dateInvalidDate

在其各个部分拆分字符串日期并单独设置,或者使用符合Date要求的字符串日期。