File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Manipulate Dates?" Watch "How to Manipulate Dates?" New topic

How to Manipulate Dates?

Ben Koh

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

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());
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,
I agree. Here's the link:
subject: How to Manipulate Dates?
It's not a secret anymore!