jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes HELP needed on DATE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HELP needed on DATE" Watch "HELP needed on DATE" New topic

HELP needed on DATE

Aero Berien

Joined: Mar 15, 2004
Posts: 1
Hi there everyone, pls help me . i have a question as above
"Code a Java class called Calendar which has a static variable of type Date(initialised to 1st January 2004), a static void method called tock(int days) to advance the variable by the specified number of days, and a static Date method called getDate() to return the current date.".
What i was thinking to write is.
public class Calendar
Date myDate = new Date();
myDate = java.sql.Date.valueOf("2004-01-01"); //but my fren said i could use the toString() instead
public static void tock(int days)
{ int i = 1;
while i < days;
Date = Date + 1; // i am not sure what i am doing ...i am just a beginner .pls help
public static Date getDate()
{ return Date;
Are my answers correct ..? how do i advance the variable (date) by the number of specified days ? the asnwer need to print out, say 4 days , u need to print 01 Jan , 02Jan , 03 Jan , 04 Jan...
anyone please help ..
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
your code

Try this instead:

To print out as a String, look at SimpleDateFormat from the API docs.

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Or you can set the date like this:

I normally use 'cal.add(GregorianCalendar.DATE, days);' when I want to add days to a date - and this works with leapyear as well.
[ March 16, 2004: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
I agree. Here's the link: http://aspose.com/file-tools
subject: HELP needed on DATE
It's not a secret anymore!