cancel
Showing results for 
Search instead for 
Did you mean: 

Substract 1 day to a date, user-defined function.

Former Member
0 Kudos

I want to sybstract 1 day from the given DATE.

My userdefined function has only 1 argument which receives the given Date.

argument name is " a "

Imports :

java.util.Date;java.text.*;java.util.Calendar;java.util.GregorianCalendar;

public String dateTrans(String a,Container container){

String DATE_FORMAT = "yyyyMMdd";

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

Calendar c1 = Calendar.getInstance();

c1.set(Calendar.YEAR,2006);

c1.set(Calendar.MONTH,6);

c1.set(Calendar.DATE,7);

c1.add(Calendar.DATE, -1);

String s = sdf.format(c1.getTime());

return s;

}

My target field receiving the value " 20060706 "

which was my defaut date - 1 day.

Its not taking the value which I'm passing as an argument.

can someone please look into it, as I'm not that good at "Java Programming "

Thanks

sagar

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

my source date format is " yyyyMMdd ".

Former Member
0 Kudos

Try this:

import java.util.Date;

import java.util.Calendar;

import java.text.SimpleDateFormat;

import java.text.DateFormat;

DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");

DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");

//String da = myformat.format(new Date());

Date ed = new Date();

try {

Date sd = inputFormat.parse(a);

a = inputFormat.format(new Date());

Calendar cal = Calendar.getInstance();

cal.setTime(sd);

int num = -1;

cal.add(Calendar.DATE, num);

ed = cal.getTime();

return outputFormat.format(ed).toString();

} catch (Exception e){

return "";

}

Regards,

Sandro

Former Member
0 Kudos

Thanks sandro.. Its working perfect.

points awarded.

Former Member
0 Kudos

Pay attention, sometime its useful to consider the data considering the time (HH:mm) also....

Regards

Sandro

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

First you have to set the date. Please check this method:

void <b>setTime(Date date)</b>

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

Regards,

Wojciech