Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying 2 weeks dates

 
sunita shaw
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me the code for extracting current date & displaying from it the next 2 weeks dates & also 1 week previous date using Gregorian Calendar.its very urgent.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious, does it have to be GregorianCalendar or should you be using the generic Calendar class? Not that it truly matters to the answer, but it is shorter to just type "Calendar" and it more generic
To get the current date,

To get the date of one week ago:

To get the date two weeks hence:

(Note that you add three weeks, because we took one week away in the previous step. You could also get a new instance of the Calendar and add two weeks...this would produce more readable code, but is less efficient)
Once you have the start date and the end date, see
here to get the list of dates between the two.
You can then display the dates either using the .toString() method or, more likely, create a java.text.SimpleDateFormat to format the dates for you.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you could create a loop that adds one day at a time for as many days as you wish.
 
sunita shaw
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx joel , u've got it exactally what i want but can u please write a small java code for this problem, i dunno where i'm making the mistake,cant send u my code coz its too long,so just send me as soon as possible.
 
sunita shaw
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx joel , u've got it exactally what i want but can u please write a small java program for this problem, i dunno where i'm making the mistake,cant send u my code coz its too long,so just send me as soon as possible.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should easily be able to create the java program from the code above and in the related link referenced above. If you are having other problems with your code (displaying the date, etc.) post them and we will be happy to help you with your problems.
If you feel that your code is too long to post here, feel free to send it to me via e-mail and I will take a look and see. While you can get my e-mail address from my profile, I'll save you the click: jsmcnary@earthlink.net
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic