Subj : Re: How do I use JavaScript Date object in my Java code? To : netscape.public.mozilla.jseng From : "Jim Wang" Date : Fri Jan 30 2004 07:36 pm Thanks for help. It works for me. However, I feel it is a little bit too heavy. I created a scriptable object extended from java.util.Date. Methods like getYear, etc. works fine. However, got into trouble with getTime() which returns long value. JavaScript does not seem able to take long type value as return value from scriptable object created in Java. Is there any way I can get around this? Thanks Jim "Igor Bukanov" wrote in message news:4014D134.80403@fastmail.fm... > 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 .