• 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

date manipulation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have date like 09/25/2007. I need to advance it by 7 dyas. So, it should be 10/02/2007. But I am getting 09/32/2007.

Here is the code:

import java.util.*;
import java.text.*;

public class month {

public static void main(String[] args) throws Exception {

DateFormat sdf = new SimpleDateFormat("mm/DD/yyyy");

Date date = null;
date = sdf.parse("09/25/2007");

Calendar cal = Calendar.getInstance();
cal.setTime(date);

String strDate = sdf.format(cal.getTime());
System.out.println(strDate);

// add 7 days
cal.add(Calendar.DAY_OF_MONTH, 7);

strDate = sdf.format(cal.getTime());
System.out.println(strDate);
}
}

Can anyone give the solution please?
Thanks.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your SimpleDateFormat pattern is wrong.
m = minute in hour
D = day in year
Check the API for more information.

To get what you want, you need to change it to MM/dd/yyyy.
 
Pingili Vishwanath
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Freddy, Thanks. It worked.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic