cancel
Showing results for 
Search instead for 
Did you mean: 

date setting in webdynpro

Former Member
0 Kudos

hello

i have a from date and to date field

i have to set the to date as (current date) default

and the from date to (current date - 7 days) default

how do i code for this in webdynpro java.

Accepted Solutions (1)

Accepted Solutions (1)

former_member335005
Participant
0 Kudos

Hi rohan,

In poojith's reply he is subtracting 72460*60 to get to 7 days old date.

For one month, replace 7 by 30 and for 1 year replace 7 by 365.

java.sql.date dtToDate = new Date(System.currentTimeMillis());

java.sql.date dtTempDate = dtToDate - (72460*60);// 7 days before

java.sql.date dtTempDate = dtToDate - (302460*60);// 1 month before

java.sql.date dtTempDate = dtToDate - (3652460*60);// 1 year before

java.sql.date dtFromDate = new Date(dtTempDate);

Best regards,

Sangeeta

Answers (3)

Answers (3)

Former Member
0 Kudos

hello again

now if

my to date is the current date

and the from date should be (current date - 1 month)

or (current date - 1 year) default

how should i code for this...

former_member182374
Active Contributor
0 Kudos

Hi,

Example for current date - 1 day (1 month, 1 year etc should be the very similar...)

    Calendar cal = Calendar.getInstance(); // today
    cal.add(Calendar.DAY_OF_YEAR, -1);
    
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    wdComponentAPI.getMessageManager().reportSuccess(sdf.format(cal.getTime()));

Regards,

Omri

former_member214651
Active Contributor
0 Kudos

Hi Rohan,

Try the following piece of code :-

<b>java.sql.date dtToDate = new Date(System.currentTimeMillis());

java.sql.date dtTempDate = dtToDate - (72460*60);

java.sql.date dtFromDate = new Date(dtTempDate);</b>

hope this helps u.

Regards,

Poojith MV

Former Member
0 Kudos

Hi Rohan

java.util.Date monday = DateHelper.getMondayDateOfCurrentWeek();

// it will get the monday of the current week
java.util.Date sunday = DateHelper.addDays(monday, 6);
//it will get the sunday of the current week (in this context Monday is firstday and sunday is the last day)
Date fromDate = new Date(monday.getTime());
Date toDate = new Date(sunday.getTime());
wdContext.currentContextElement().setFromDate(fromDate);
//Sets the monday to your input field (Date Picker)
wdContext.currentContextElement().setToDate(toDate);
//Sets the sunday to your input field (Date Picker)

create a java project and add this code to that project

code for getMondayDateofCurrentWeek and addDays is as follows..please refer to this and make changes accordingly

actually this is a java file .which i included in my project . DateHelper is my class name.you also do the same and .class file to your webdynpro project

static public Date getMondayDateOfCurrentWeek() {
		Date today = new Date();
		Date monDate = today;
		int day = getDayOfWeek(today);
 
		//2 because 2 is Monday
		if (day > 2) 
		{
		while (day > 2) 
			{
			monDate = yesterday(monDate);
			day = getDayOfWeek(monDate);
			}
		} 
		else 
		{
		while (day < 2) 
			{
			monDate = tomorrow(monDate);
			day = getDayOfWeek(monDate);
			}
		}
		return monDate;
	}
 
 
static public final Date addDays(Date target, int days) 
{
		// returns a Date that is the sum of the target Date
		// and the specified number of days;
		// to subtract days from the target Date, the days
		// argument should be negative
 
		long msPerDay = 1000 * 60 * 60 * 24;
		long msTarget = target.getTime();
		long msSum = msTarget + (msPerDay * days);
		Date result = new Date();
		result.setTime(msSum);
		return result;
	}

For More reference <a href="https://forums.sdn.sap.com/click.jspa?searchID=6993015&messageID=4032225">Look this thread</a>

Best Regards

Chaitanya.A