I'm trying to parse a date that i get from JavaScript script evaluated with rhino library into java.util.Date, can i convert a org.mozilla.javascript.NativeDate into a java.util.Date ?
If convert NativeDate into a string with the Context.tostring method i get a date in the following format :
Wed Oct 12 2011 16:17:59 GMT+0200 (CEST)
How can i parse this string date representation in to a java.util.Date object ?
In Rhino use
context.jsToJava(nativeDateObj, Date.class);
Bvesco's answer works well. However doing this the other way round (java to js) is not entirely as simple - Context.javaTojs()
does not work for dates. I eventually found the solution here - use the javascript constructor:
Object js = context.newObject(scope, "Date", new Object[] {date.getTime()});
The above post also mentioned the following alternative to convert a date from js to java (I haven't confirmed this):
Date date = new Date((long) ScriptRuntime.toNumber(s));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With