Friday, January 11, 2008

How to Get Date/Time in/with GWT

* get a unix time stamp since the epoch
* get year, month, today, date, hours, minutes seconds


//Get the browsers date (!!!note: I can't get gmt time zone in eclipse debugger)
Date date = new Date();
int Month = date.getMonth();
int Day = date.getDate();
int Year = date.getYear();
int Hour = date.getHours();
int min = date.getMinutes();
int sec = date.getSeconds();
int tz = date.getTimezoneOffset();
int UnixTimeStamp = (int) (date.getTime() * .001);

//get unix time stamp example (seconds)
Long lTimeStamp = date.getTime(); //time in milleseconds since the epoch
int iTimeStamp = (int) (lTimeStamp * .001); //(Cast) to Int from Long, Seconds since epoch
String sTimeStamp = Integer.toString(iTimeStamp); //seconds to string

//get the gmt date - will show tz offset in string in browser, not eclipse debug window
String TheDate = date.toString();

//render date to root panel in gwt
Label label = new Label(TheDate);
RootPanel.get().add(label);

4 comments:

Asad Naeem said...

A very good starting for the datetime in GWT.
Please tell me How to apply custom format to a Date in GWT 2.0?

Asad Naeem said...

I have found the solution

Date resultdate = Calendar.getInstance().getTime();
com.google.gwt.i18n.client.DateTimeFormat dtf = com.google.gwt.i18n.client.DateTimeFormat.getFormat("MM/dd/yyyy");
return dtf.format(resultdate).toString();

Anonymous said...

Date.getYear etc is deprecated. So do not use that :)

Calendar works only on server side. So on client side consider use of DataTimeFormat class from GWT and Date class:


final DateTimeFormat yearFormat = DateTimeFormat.getFormat("y");
final String year = yearFormat.format(new Date());

Anonymous said...

Date.getYear() is deprecated but it is used extensively by GWT's DateTimeFormat.

You may as well just use it directly!

Trying out the Dart Analysis Server

I wanted to see how the Dart Analysis Server was put together and worked. I started looking to see how I could wire it up and try out the co...