Get today's date, and the first day of this week's date?

2,454 views
Skip to first unread message

spierce7

unread,
Jun 8, 2010, 4:58:52 PM6/8/10
to Google Web Toolkit
Hey guys, I just found out that the Java Calendar class isn't
compatible with GWT (Assumed it was since Date was). I'm a Java newb
and spent a few hours figuring out how to do what I wanted to do with
the Calendar class, and I'm clueless how to do this with GWT, so any
help would be appreciated.

1. Get today's date.
2. Using the current date, get the date of the first day of the week
(Sunday), and I think I might need to know how to get the end date
also.
3. How to Add and subtract a number of days to a date.
4. I need to know how to parse all the above questions information
into a string.

Off the top of my head, that's what I'm trying to do. 1 More question:
5. Would it improve performance while handling a start date and an end
date for an event, instead of keeping an end date, calculating the
duration that the event would last, rather than calculating the date?
Would it even be worth it? (A friend recommended this)

6. Are we seeing any updates coming for working with dates? I'm
currently using GWT 2.03.

fmod

unread,
Jun 8, 2010, 5:36:05 PM6/8/10
to Google Web Toolkit
> Hey guys, I just found out that the Java Calendar class isn't
> compatible with GWT (Assumed it was since Date was). I'm a Java newb
> and spent a few hours figuring out how to do what I wanted to do with
> the Calendar class, and I'm clueless how to do this with GWT, so any
> help would be appreciated.
java.util.Date is compatible with GWT. Probably your IDE will complain
about deprecated methods. But they are not for GWT. So you probably
will end up with a lots of '@SuppressWarnings("deprecation") // GWT
requires Date' before your method declaration.


> 1. Get today's date.
new Date();

> 2. Using the current date, get the date of the first day of the week
> (Sunday), and I think I might need to know how to get the end date
> also.
> 3. How to Add and subtract a number of days to a date.
Date date = new Date();
date.getDay() gives you the current day in the week.
date.getTime() gives you the time in milliseconds since 1/1/1970
date.setTime(date.getTime() - 24 * 60 * 60 * 1000) sets the date to
yesterday

> 4. I need to know how to parse all the above questions information
> into a string.
Take a look at this post: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/641e83518db5ee0e

> Off the top of my head, that's what I'm trying to do. 1 More question:
> 5. Would it improve performance while handling a start date and an end
> date for an event, instead of keeping an end date, calculating the
> duration that the event would last, rather than calculating the date?
> Would it even be worth it? (A friend recommended this)
Computing the difference of two dates is straightforward
long millisDiff = end.getTime() - start.getTime();

> 6. Are we seeing any updates coming for working with dates? I'm
> currently using GWT 2.03.
No idea, but I will not depend on that.

To finish you can take a look in the package
com.google.gwt.user.datepicker.client
http://code.google.com/p/google-web-toolkit/source/browse/#svn/trunk/user/src/com/google/gwt/user/datepicker/client
to see how GWT guys seems to work with dates.

Lothar Kimmeringer

unread,
Jun 8, 2010, 5:55:58 PM6/8/10
to google-we...@googlegroups.com
spierce7 schrieb:

> 1. Get today's date.

Date today = new Date();

> 2. Using the current date, get the date of the first day of the week
> (Sunday), and I think I might need to know how to get the end date
> also.

So you're not going to create an i8n-aware application?
Sunday isn't the first day of the week everwhere (e.g.
it's monday in Germany). Here is an (untested) solution:

DateTimeConstants constants = LocaleInfo.getCurrentLocale().getDateTimeConstants();
int firstDay = Integer.parseInt(constants.firstDayOfTheWeek()) - 1;
int offset = firstDay - date.getDay();
date.setDate(date.getDate() + offset);

> 3. How to Add and subtract a number of days to a date.

date.setDate(date.getDate() + days);

> 4. I need to know how to parse all the above questions information
> into a string.

By "parse" do you mean format?

Use the various ways DateTimeFormat offers.

> Off the top of my head, that's what I'm trying to do. 1 More question:
> 5. Would it improve performance while handling a start date and an end
> date for an event, instead of keeping an end date, calculating the
> duration that the event would last, rather than calculating the date?
> Would it even be worth it? (A friend recommended this)

If the event happens during a change in daylight saving times,
you will end up with wrong times.

> 6. Are we seeing any updates coming for working with dates? I'm
> currently using GWT 2.03.

I'm not a developer, so I can't say anything about that, but
if you're sugessting a (maybe restricted) implementation of
Calender, here is a +1 by me, because above methods in Date
are all deprecated since JDK 1.1, so I really hate to use them,
even in a GWT client-class where that doesn't matter much.


Regards, Lothar

spierce7

unread,
Jun 8, 2010, 9:21:34 PM6/8/10
to Google Web Toolkit
Guys, thank you so much for your replies! I've got my program doing
what I needed it to :-) Thank you very much!

Lothar, your code works (at least for the current date) :
DateTimeConstants constants =
LocaleInfo.getCurrentLocale().getDateTimeConstants();
int firstDay = Integer.parseInt(constants.firstDayOfTheWeek()) - 1;
int offset = firstDay - date.getDay();
date.setDate(date.getDate() + offset);

However, I can't find a way to get the first day of the week for any
given date. For instance, lets say I wanted to find the first day of
the week for September 27th, 2010, how would I do that?

Lothar Kimmeringer

unread,
Jun 9, 2010, 4:07:19 AM6/9/10
to google-we...@googlegroups.com
spierce7 schrieb:

> Lothar, your code works (at least for the current date) :

It should work for every date, just instantiate Date with
one of the non-default constructors.

> However, I can't find a way to get the first day of the week for any
> given date. For instance, lets say I wanted to find the first day of
> the week for September 27th, 2010, how would I do that?

Using the deprecated constructor of Date:

Date date = new Date(110, 08, 27);

You can also use the date-parsing classes of GTW to parse
a string to a date.


Regards, Lothar

Charles Keepax

unread,
Jun 9, 2010, 2:50:29 AM6/9/10
to Google Web Toolkit
You could return a string for what day of the week it is from the
DateTimeFormat (using "E"), then do a large if statement to return the
first day of the week.

Charles Keepax

Charles Keepax

unread,
Jun 8, 2010, 5:40:57 PM6/8/10
to Google Web Toolkit
Probably the best class to implement that functionality would be
DateTimeFormat ( http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/i18n/client/DateTimeFormat.html
) which is very similar to the Java DateFormat class. So for example:

Date today = new Date();
DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy.MM.dd HH:mm:ss");
String strToday = dtf.format(today);

Will fill strToday with the current date and time. This can be set to
return the day of the week etc as well.

As for performing operations on Dates such as adding days I usually
use the getTime method (this returns the Date as a long representing
the number of milliseconds that have passed since January 1, 1970,
00:00:00 GMT) and operate on the dates as a long. So for example if I
want to add a day I just add (24*60*60*1000) to the long then create a
new Date object.

Hope that helps some,
Charles Keepax

Lothar Kimmeringer

unread,
Jun 10, 2010, 4:59:02 AM6/10/10
to google-we...@googlegroups.com
Charles Keepax schrieb:

> As for performing operations on Dates such as adding days I usually
> use the getTime method (this returns the Date as a long representing
> the number of milliseconds that have passed since January 1, 1970,
> 00:00:00 GMT) and operate on the dates as a long. So for example if I
> want to add a day I just add (24*60*60*1000) to the long then create a
> new Date object.

you're not the first giving this "advice". This way of adding
days is plain wrong. In countries with e.g. CEST adding 24 hours
to 10/30/2010 03:01 AM will result to 10/31/2010 02:01 AM.

So stop doing this kind of arithmetic and use Calendar on
server-side and wait (as I do) that there will some equal
kind of thing on the client side at some day. If I would
have the time to implement it, I'd program it for myself.

Regards, Lothar

Nicholas

unread,
Jun 9, 2010, 5:41:14 PM6/9/10
to google-we...@googlegroups.com
To answer #6: I asked the GWT team about this during the fireside chat at I/O this year.  They appeared to be caught off guard by the question but eventually came out and said that they have no plans on resolving this issue.  I kind of got the feeling that this was the first time that it came up to them.  We have attempted to tackle this issue in two different ways.  We first made a DateUtil class that wrapped up all of the deprecated methods that we used from java.util.Date.  This was an OK solution but it seemed really dirty.   Then a co-worker went about modifying Joda Time (http://joda-time.sourceforge.net/) for the purpose of using it within GWT.  The result was gwt-time (http://code.google.com/p/gwt-time/).  It is usable but it is considered alpha as there is much work to be done with it before it is ready for GWT prime time.  It is our hope that we will be able to make this a rich implementation of the joda time API that plays well in GWT land.  Unfortunately neither of the contributors have had much time to hack away at it lately.  

-Nick 


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Reply all
Reply to author
Forward
0 new messages