This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes date manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "date manipulation" Watch "date manipulation" New topic

date manipulation

Pingili Vishwanath

Joined: Dec 30, 2006
Posts: 7

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

String strDate = sdf.format(cal.getTime());

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

strDate = sdf.format(cal.getTime());

Can anyone give the solution please?
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

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.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Pingili Vishwanath

Joined: Dec 30, 2006
Posts: 7
Hi Freddy, Thanks. It worked.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: date manipulation
It's not a secret anymore!