• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Manipulate Dates?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic