This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to Manipulate Dates? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Manipulate Dates?" Watch "How to Manipulate Dates?" New topic
Author

How to Manipulate Dates?

Ben Koh
Greenhorn

Joined: Aug 06, 2001
Posts: 7
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

Joined: Mar 16, 2001
Posts: 986

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.)


Please ignore post, I have no idea what I am talking about.
Larry Anderson
Greenhorn

Joined: Aug 06, 2001
Posts: 26
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Manipulate Dates?