Subj : Re: How do I use JavaScript Date object in my Java code? To : Jim From : Igor Bukanov Date : Mon Jan 26 2004 09:35 am Jim wrote: > I am trying to access the JavaScript Object in my Java code. Wonder how I > should do it. Since Date in JS is object, it is represented in Rhino by an instance of Scriptable and if you extend ScriptableObject, you have to do the convertion between that and java.util.Date/other Java Dates manually. > The following is what I am trying: > > 1. I created a scriptable object class, say 'MyScriptable'. It has a method > jsFunction_setDate(Object date) which creates a java.sql.date object from > the date passed in. It then saves the date in the datebase through JDBC. > > In a javscript, I will use it JavaScript as: > var dt = new Date(); > MyScriptable.setDate(dt); > > How would I be able to access the JavaScript Date object in my java function > jsFunction_setDate()? Declare it as public void jsFunction_setDate(Object jsDate) { java.util.Date d = (java.util.Date)Context. toType(jsDate, java.util.Date.class); create sql date from java.util.Date ... } > > 2. In reverse to the above, how can I create a JavaScript Date object from > java.sql.date in jsFunction_getDate()? You need to construct JS Date explicitly by calling its Date(timeInMillis) JS constructor: public Object jsFunction_getDate() throws JavaScriptException { long timeInMillis = extract it from sql date Context cx = Context.getCurrentContext(); Scriptable scope = getParentScope(); Object[] args = { new Long(timeInMillis); } return cx.newObject(scope, "Date", args); } Regards, Igor .