GWT: working with JsDate and Java Date
In my overlays I wrap a JavaScript Date object in a JsDate:
public final native JsDate getDueDate() /*-{
return this["dueDate"];
}-*/;
How开发者_运维百科ever when I want to use that date in a widget, say a DateBox, I need to set the value as a Java Date. I could create a Java Date from my JsDate but I believe that adds some overhead.
Date javaDate = new Date(jsDate.getTime());
Is there a cleaner way of achieving this? What is the best way to convert a JsDate object to a Java Date object and vice-versa?
Thanks a lot
Jason's code doesn't work for me since getDueDateNative().getTime()
returns a double
and not a long
. Therefore you have also to cast the value: return new Date((long) getDueDateNative().getTime());
GWT's Date
implementation uses JsDate
under the covers so there should be no meaningful performance penalty at all. To make things easier for consumers of the type change your overlay to return Date
s instead of JsDate
s:
public final Date getDueDate() {
return new Date(getDueDateNative().getTime());
}
private final static JsDate getDueDateNative() /*-{
return this["dueDate"];
}-*/;
精彩评论