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. so 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 "".
Here is the error trace:
Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
at java.lang.ClassLoader.defineClass(ClassLoader.java:300)
at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:27)
at org.mozilla.javascript.optimizer.Codegen.defineClass(Codegen.java:130)
at org.mozilla.javascript.optimizer.Codegen.createScriptObject(Codegen.java:85)
at org.mozilla.javascript.Context.compileImpl(Context.java:2394)
at org.mozilla.javascript.Context.compileString(Context.java:1335)
at org.mozilla.javascript.Context.compileString(Context.java:1324)
at org.mozilla.javascript.Context.evaluateString(Context.java:1076)
at com.mycompany.android.MainActivity.parseDateUsingRhino(MainActivity.java:52)
at com.mycompany.android.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
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));
}
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 method accessible variables as global variables in the javascript scope):
String script = "var date = new Date(dateInUnknownFormat).getTime();";
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):