Adding days to the Given Date

One of my friend was breaking his head to add certain days to the given date in Java. Later I gave him the solution as below. I hope it will be helpful for you too. The source-code goes like this:


/**
 *
 * @author Prashant
 */
import java.util.*;
import java.text.*;
public class DateProg {
 static int day,month,year;
 public void reduceDate1(int d1,int m1,int y1)
 {
 Date dt = new Date();
 Calendar c = Calendar.getInstance();
 c.set(y1, m1, d1);
 // c.setTime(dt);
 c.add(Calendar.DATE, 364);
 c.add(Calendar.MONTH, -1);
 dt = c.getTime();
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 String Date = dateFormat.format(dt);

 System.out.println(Date );
 }
 public static void main(String[] args) {
 DateProg obj= new DateProg();
 obj.reduceDate1(1,01,2004);
 }
}

Output
2004-12-30

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: