This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Manipulate Dates?

 
Ben Koh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Gurus, i have written some code to display the date as shown below.
java.util.Date myDate = new java.util.Date();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy");
String myString = dateformat.format(myDate);
System.out.println(myString);
The date appears fine, however, my question is how do i manipulate the date in terms of displaying the next 5 days to it? For example the result from above is "24-Aug-2001". How do I get it to display the dates for the next 5 days? Example "25-Aug-2001", "26-Aug-2001", "27-Aug-2001", "28-Aug-2001" and "29-Aug-2001"? I understand I will need a for loop to do this.
Any help will be greatly appreciated!! Thanks!!
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "Date" class looks very deprecated. I think that Calendar is the new and better way of doing things.
(Sorry, that didn't answer your question.)
 
Larry Anderson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this would answer your question, let me know if it works:
for (int i=0; i < daysToPass; i++) {
Calendar currCal = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM- yyyy");
String myString = dateformat.format(currCal.getTime());
System.out.println(myString);
currCal.add(Calendar.DATE, 1);
}
Where daysToPass would be the number of days you want to go. There may be a more efficient way, but there are tons of things you can do with the Calendar class which may make you want to consider that for the date manipulations that you need to do.
Let me know if it helps,
Larry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic