Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP needed on DATE

 
Aero Berien
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code

Try this instead:

To print out as a String, look at SimpleDateFormat from the API docs.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ren´┐Ż
[ March 16, 2004: Message edited by: Rene Larsen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic