admin管理员组文章数量:1384238
Based on the discussion converting string representation of unknown date-format to Date in java, I want to use the JavaScript Date
function in my App-Engine project. However, ScriptEngine does not work on App-Engine. So I need a little help converting to Rhino. Here is the ScriptEngine code I need to convert:
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();
The following has not worked
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;
}
and I have already replaced "TestScript"
with null
and ""
.
Based on the discussion converting string representation of unknown date-format to Date in java, I want to use the JavaScript Date
function in my App-Engine project. However, ScriptEngine does not work on App-Engine. So I need a little help converting to Rhino. Here is the ScriptEngine code I need to convert:
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();
The following has not worked
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;
}
and I have already replaced "TestScript"
with null
and ""
.
- Did you forget to add the error trace? – Filip Commented Oct 22, 2015 at 22:04
- 1 No. I took it out because it was irrelevant. I just don't know how to write the code correctly. The irrelevance is due to that I get different errors or no errors depending on what I try. But in the end, I can never get Rhino to produce a result. Clearly someone who knows how Rhino works, will have no problem translating the ScriptEngine version to Rhino. – learner Commented Oct 22, 2015 at 22:13
2 Answers
Reset to default 1In the script that is executed by the JavaScript engine, you just need to calculate the value you want using new Date(dateString).getTime()
(you could also use "var date = new Date(dateString); date.getTime()";
). You should not store the result inside a variable.
I also made two additions to your method:
- To convert the result of the evaluation to a String, you should use
Context.toString(obj)
. Since here, the result is actually a number, we can use directlyContext.toNumber(obj)
, which returns adouble
, and cast it tolong
. - I added a
finally
block which exits from theContext
.
Working code:
private static long parseDateUsingRhino(String dateInUnknownFormat) {
Context mozillaJsContext = Context.enter();
try {
Scriptable scope = mozillaJsContext.initStandardObjects();
String script = "new Date('" + dateInUnknownFormat + "').getTime()";
Object obj = mozillaJsContext.evaluateString(scope, script, "TestScript", 1, null);
return (long) Context.toNumber(obj);
} finally {
Context.exit();
}
}
Sample:
System.out.println(parseDateUsingRhino("2015-10-25T15:06:42.000Z")); // prints 1445785602000
Side note: this was tested with Rhino 1.7.7.
You should try it first like this and see if it works:
private static long parseDateUsingRhino(String dateInUnknownFormat){
Context mozillaJsContext = Context.enter();
Scriptable scope = mozillaJsContext.initStandardObjects();
String script = "var date = new Date().getTime();";
Object result = mozillaJsContext.evaluateString( scope, script, "", 1, null );
return Long.valueOf(Context.toString(result));
}
And according to this all you need to do next is pass the dateInUnknownFormat
variable like so, no java string concatenation required (the context is smart enough to probably consider the java method's accessible variables as global variables in the javascript scope):
String script = "var date = new Date(dateInUnknownFormat).getTime();";
本文标签: Using Rhino instead of ScriptEngine to run Javascript code in JavaStack Overflow
版权声明:本文标题:Using Rhino instead of ScriptEngine to run Javascript code in Java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744507898a2609697.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论