基于这个讨论 converting string representation of unknown date-format to Date in java,我想在我的App-Engine项目中使用JavaScript的Date
函数。但是,ScriptEngine在App-Engine上无法工作。所以我需要一点帮助来将其转换为Rhino。以下是我需要转换的ScriptEngine代码:
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName("JavaScript");
String script = "var date = new Date('" + dateInUnknownFormat + "'); var timestamp = date.getTime();";
engine.eval(script);
long timestamp = ((Double) engine.get("timestamp")).longValue();
以下内容未能解决问题。private static long parseDateUsingRhino(String dateInUnknownFormat){
Context mozillaJsContext = Context.enter();
Scriptable scope = mozillaJsContext.initStandardObjects();
String script = "var date = new Date('" + dateInUnknownFormat + "'); var timestamp = date.getTime();";
Object obj = mozillaJsContext.evaluateString( scope, script, "TestScript", 1, null );
Double timeDouble = Double.parseDouble((String) obj);
long timestamp = timeDouble.longValue();
return timestamp;
}
我已经用null
和""
替换了"TestScript"
。
and I have already replaced "TestScript"
with null
and ""
.