Date manipulation

814 views
Skip to first unread message

freeall

unread,
Aug 8, 2008, 1:38:53 PM8/8/08
to Google Web Toolkit
Hi.

I want to do something so simple and yet I can't figure out how to do
it.

I want to have a date and subtract x number of days from that date.
Like this pseudothing:
Date aWeekAgo = new Date();
aWeekAgo.add(Date.DAYS, -7);

This is somewhat what you would do with a Calendar object in Java, but
Calendar doesn't exist in GWT.

So does anyone have any ideas on how to do this? Or a library that can
do it. I would really hate to start doing:
int year = 2008;
int month = 1;
int day = 3;
And then start to subtract 7 days from this... :)

Larry White

unread,
Aug 8, 2008, 6:16:51 PM8/8/08
to Google-We...@googlegroups.com
I would try something like this:
0. Create a constant MSECS_PER_DAY as 1000*60*60*etc.
1. get the time in milliseconds from the date:  long time = date.getTime();
2. multiply your  MSECS_PER_DAY by 7  (for 7 days)
3. Subtract that value from your time value:
4. create a new date and set the time:  date2.setTime(n);

Arthur Kalmenson

unread,
Aug 9, 2008, 5:38:53 PM8/9/08
to Google Web Toolkit
Another solution is to use the Date object's regular methods. So it
would look like this:

Date aWeekAgo = new Date();
aWeekAgo.setDate(aWeekAgo.getDate() - 7);

I know that these methods have been deprecated since JDK 1.1, but they
are valid on the client side (i.e. in Javascript), so you can still
use them. Another way is to mess around with the DateTimeFormat, but
that'll get hairy, quickly.

Regards,
Arthur Kalmenson

On Aug 8, 6:16 pm, "Larry White" <ljw1...@gmail.com> wrote:
> I would try something like this:
> 0. Create a constant MSECS_PER_DAY as 1000*60*60*etc.
> 1. get the time in milliseconds from the date:  long time = date.getTime();
> 2. multiply your  MSECS_PER_DAY by 7  (for 7 days)
> 3. Subtract that value from your time value:
> 4. create a new date and set the time:  date2.setTime(n);
>

sal...@gmail.com

unread,
Aug 11, 2008, 4:47:13 PM8/11/08
to Google Web Toolkit
Careful with Larry's method, too. I had subscriptions in a table and
tried to add 30 days at a time using the milliseconds per day. The
'long' GWT stored this value in overflowed and gave crazy values. I
instead used a for loop (between 0 and 30) to add MSECS_PER_DAY. This
caused the user's browser to freeze while the calculations were
performed. I wish I'd known about Arthur's suggestion@

good luck,

David

On Aug 8, 6:16 pm, "Larry White" <ljw1...@gmail.com> wrote:
> I would try something like this:
> 0. Create a constant MSECS_PER_DAY as 1000*60*60*etc.
> 1. get the time in milliseconds from the date: long time = date.getTime();
> 2. multiply your MSECS_PER_DAY by 7 (for 7 days)
> 3. Subtract that value from your time value:
> 4. create a new date and set the time: date2.setTime(n);
>

Larry White

unread,
Aug 11, 2008, 6:39:25 PM8/11/08
to Google-We...@googlegroups.com
That's interesting.  Is this a known GWT bug?  I checked my copy of JavaScript: The Definitive Guide (which I'm glad to say has been gathering dust since I started with GWT :) and it says standard JavaScript can handle any date within 273,000 odd years of 1970 with millisecond precision.  I'm planning on doing a bunch of date math in the client with GWT so it would be a real issue.

I definitely need to try this out so I understand it better.
Reply all
Reply to author
Forward
0 new messages