使用Rhino而不是ScriptEngine在Java中运行Javascript代码 [英] Using Rhino instead of ScriptEngine to run Javascript code in Java

查看:1151
本文介绍了使用Rhino而不是ScriptEngine在Java中运行Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于讨论转换字符串表示未知的日期格式为Date in java ,我想在我的App-Engine项目中使用JavaScript Date 函数。但是,ScriptEngine不适用于App-Engine。所以我需要一点帮助转换到犀牛。以下是我需要转换的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();
脚本范围= 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();
返回时间戳;
}

我已经替换了TestScript code>与 null

new Date(dateString).getTime()来计算所需的值。 code>(你也可以使用var date = new Date(dateString); date.getTime(); )。您不应将结果存储在变量中。



我还在您的方法中添加了两个:


  • 要将评估结果转换为字符串,您应该使用 Context.toString(obj)。因为这里的结果实际上是一个数字,我们可以直接使用 Context.toNumber(obj),它返回一个 double ,并将其转换为 long

  • 我最后添加了一个块它从 Context 中退出。



工作代码:

  private static long parseDateUsingRhino(String dateInUnknownFormat){
Context mozillaJsContext = Context.enter();
尝试{
可脚本化范围= 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();


$ / code $ / pre



  System.out.println(parseDateUsingRhino(2015-10-25T15:06:42.000Z)); //打印1445785602000 

注意:这是使用Rhino 1.7.7测试的。


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 "".

解决方案

In 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 directly Context.toNumber(obj), which returns a double, and cast it to long.
  • I added a finally block which exits from the Context.

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.

这篇关于使用Rhino而不是ScriptEngine在Java中运行Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆