Need Java Calendar help

R

Ryan

I have a simple java application which needs to pick a date off in the
future some time. I'm using the roll method, to simply jump out some
number of days ahead.

Then I attempt to print out the new day with a simple date
formatter....

---------------------
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DAY_OF_YEAR, 70);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
System.out.println("New Date: "+dateFormat.format(cal.getTime()));
--------------------

Here is the output:

New Date: 02/27/07

As you can see, it thinks the new date is still in 2007 and not 2008.
However, it did pick the right day. Feb 27, 2008 is 70 days out from
today (Dec 19).

This Calendar object is a bit tricky....
Any help would be great.
Ryan
 
A

Andreas Wollschlaeger

Ryan said:
I have a simple java application which needs to pick a date off in the
future some time. I'm using the roll method, to simply jump out some
number of days ahead.

Then I attempt to print out the new day with a simple date
formatter....

---------------------
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DAY_OF_YEAR, 70);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
System.out.println("New Date: "+dateFormat.format(cal.getTime()));
--------------------

Here is the output:

New Date: 02/27/07

As you can see, it thinks the new date is still in 2007 and not 2008.
However, it did pick the right day. Feb 27, 2008 is 70 days out from
today (Dec 19).

This Calendar object is a bit tricky....
Any help would be great.
Ryan


Well, this is exactly what the roll() method is supposed to do - the
javadocs coming with the SDk are pretty clear on that. Perhaps
Calendars' add() method is what you want :)

Cheers
Andreas
 
R

Ryan

Ryan schrieb:










Well, this is exactly what the roll() method is supposed to do - the
javadocs coming with the SDk are pretty clear on that. Perhaps
Calendars' add() method is what you want :)

Cheers
Andreas


That solved the issue. For some reason, I had just missed the add()
method.
Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top