cancel
Showing results for 
Search instead for 
Did you mean: 

How to ADD Days to the Date?

Former Member
0 Kudos

hi

I have this thing to do:

int days=wdContext.currentContext.getNoofdays();

Date date = wdContext.currentContext.getDate();

Date date2 = date + days;

wdContext.currentContext.setDate(date2);

Means i have to add no of days to my date.

And then set my date to the increased date(increase due to addition of days.

Please help me

Regards

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

Try this

int days=wdContext.currentContext.getNoofdays();

Date date = wdContext.currentContext.getDate();

String DATE_FORMAT = "yyyy-MM-dd";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

Calendar c1 = Calendar.getInstance();

c1.set((date.getYear()1900), date.getMonth()1 , date.getDate());

c1.add(Calendar.DATE,days);

Date d2=c1.getTime();

java.sql.Date date2=new java.sql.Date(d2.getYear(),d2.getMonth(),d2.getDate());

wdContext.currentContext.setDate(date2);

Kind Regards

Mukesh

sridhar_k2
Active Contributor
0 Kudos

Hi Mukesh,

c1.set((date.getYear()1900), date.getMonth()1 , date.getDate());

In the Above line of code, month + 1 takes u to the next month..right?

bhandari,

The below code is working fine. you can directly use this one.

int days=10; // how many days you want add to the current date String DATE_FORMAT = "yyyy-MM-dd";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

Date date = wdContext.currentContextElement().getDate1();

Calendar c1 = Calendar.getInstance();

if(date != null){

c1.set((date.getYear()+1900), date.getMonth(), date.getDate()); // 2006 Aug 28

c1.add(Calendar.DATE,days);

// In the Same way you can add months and year also

java.util.Date d2 = c1.getTime();

java.sql.Date date2=new java.sql.Date(d2.getYear(),d2.getMonth(),d2.getDate());

wdContext.currentContextElement().setDate2(date2);

}

Former Member
0 Kudos

Hi Sridhar

You are correct . There is no need for add the month

Kind Regards

Mukesh

Former Member
0 Kudos

hi

Sridhar my date format is dd/mm/yy will this code work on this format.

Regards

Nidhideep

Former Member
0 Kudos

Hi

This code is working fine

int days=wdContext.currentcontextElement.getDay();

Date date = wdContext.currentContextElement().getTestDate();

String DATE_FORMAT = "dd/mm/yy";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

Calendar c1 = Calendar.getInstance();

c1.set((date.getYear()), date.getMonth() , date.getDate());

c1.add(Calendar.DATE,days);

wdComponentAPI.getMessageManager().reportSuccess(sdf.format(c1.getTime()));

Date d2=c1.getTime();

java.sql.Date date2=new java.sql.Date(d2.getYear(),d2.getMonth(),d2.getDate());

wdContext.currentContextElement().setTestDate(date2);

Kind Regards

Mukesh

sridhar_k2
Active Contributor
0 Kudos

Hi,

That Code will Work, with one change. Just give your date format as "dd/MM/yy".

YY - 2 Digit Year

YYYY - 4 - Digit Year Number

Regards,

Sridhar

Former Member
0 Kudos

hi

I have solved my problem.Thanks for ur support.

Regards

Nidhideep

Answers (3)

Answers (3)

saraswathi_d
Participant
0 Kudos

Hi,

Use the following code.

String DATE_FORMAT = "yyyy-MM-dd";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

Calendar c1 = Calendar.getInstance();

c1.set(2006, 08 , 28); // 2006 Aug 28

wdComponentAPI.getMessageManager().reportSuccess("Date is : " + sdf.format(c1.getTime()));

c1.add(Calendar.DATE,10);

c1.add(Calendar.MONTH,5);

c1.add(Calendar.YEAR,5);

wdComponentAPI.getMessageManager().reportSuccess("Date + 10 days is : " + sdf.format(c1.getTime()));

Regards,

Saraswathi

Former Member
0 Kudos

Hi,

To Add Days into the Date, you need to use java's Calendar Class.



String DATE_FORMAT = "yyyy-MM-dd";
java.text.SimpleDateFormat sdf = 
      new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance(); 
c1.set(1999, 0 , 20); // 1999 jan 20
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.add(Calendar.DATE,20);
System.out.println("Date + 20 days is : " + sdf.format(c1.getTime()));

Hope it helps you.

Regards,

Maheswaran.B

Former Member
0 Kudos

Hi,

Have a look at this thread,

Regards,

Saravanan K