cancel
Showing results for 
Search instead for 
Did you mean: 

Past Date check

Former Member
0 Kudos

All,

Im checking whether the entered date is in Past or not with below code: The issue is, If i give current date also its giving me an error. If i give future date then no error.

But I want to send an error message only if the date is in past...Is the below code is correct? If not pls correct me..


Date theDate = (Date)
	wdContext.currentContextElement().getAttributeValue("fld");
	IWDAttributeInfo attributeInfo =
	wdContext.getNodeInfo().getAttribute("fld");
	
	if (theDate.before(new Date(System.currentTimeMillis()))) {

//Throw an error message	

	}

Thanks

BM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Bharathi M

You can simply use this type of code

if (<b>theDate</b>.after(new Date(System.currentTimeMillis())))

{

..........

}

<b>theDate</b> is what you provide for checking

Check out for this Link you will have a clear idea

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webdynpro/wd%20jav...

Regards,

Mathan MP

Former Member
0 Kudos

All,

I need to send an error message only if the date is in Past..

if (theDate.after(new Date(System.currentTimeMillis()))) is not working as i expected.

BM

Former Member
0 Kudos

Hi,

inputDate.<b>before</b>(CurrDate) //means the date has to be earlier then todays date and future date. If it is equal to todays date that means it is not before current date it is equal.

Same way goes for <b>after</b>.

So their is no inbuilt method that can check if input date is past/future including current date.

If you application needs then you have to code it u r self as explained in my above reply.

Answers (6)

Answers (6)

former_member187990
Participant
0 Kudos

The reason for not accepting the current date is

The system.currentmillisecond will be changing every second.But the date which you have given in the inputfield will have a constant value(ie millisecond,( which is calculated from 1/1/1970))

When the millisecond of the inputfield is compared with current millisecond,the currentmillisecond value will be greater than the date given in inputfield, so the condition become true and raises the Error

Inorder to make your scenario work perfect,

Date theDate = (Date) wdContext.currentContextElement().getAttributeValue("fld");

IWDAttributeInfo attributeInfo =

wdContext.getNodeInfo().getAttribute("fld");

<b>//Do the below changes

Long d1,d2;

D1=new Date(System.currentTimeMillis()).getTime();

D2=theDate.getTime();

If(((d1-d2)/(10006060*24)>0)

{

//Throw Error Message

wdComponentAPI.getMessageManager().reportSuccess("Date given is a Past Date");

}</b>

If you still have doubts

print the values of d1 and d2. You will know.

Former Member
0 Kudos

Hi,

Try this

String[] currDate = new Date(System.currentTimeMillis()).toString().split("-");

String[] inpDate = wdContext.currentContextElement().getDatein().toString().split("-");

// check if input year is less than current year

if(Integer.parseInt(inpDate[0]) < Integer.parseInt(currDate[0]))

{

wdComponentAPI.getMessageManager().reportException("Please enter future date or present date",false);

}

else if(Integer.parseInt(inpDate[0]) == Integer.parseInt(currDate[0])) // if input year is equal to curr year

{

if(Integer.parseInt(inpDate[1]) < Integer.parseInt(currDate[1])) // check for the month

{

wdComponentAPI.getMessageManager().reportException("Please enter future date or present date",false);

}

else if(Integer.parseInt(inpDate[1]) == Integer.parseInt(currDate[1])) // if month is same

{

if(Integer.parseInt(inpDate[2]) < Integer.parseInt(currDate[2])) // check for date

{

wdComponentAPI.getMessageManager().reportException("Please enter future date or present date",false);

}

}

}

Message was edited by:

Shriram Kabra

Former Member
0 Kudos

Hi Bharthi,

There's only one way to be positive.Check the type of the date and if it's java.sql.Date change it java.util.Date and I hope you know that this for an obivious reason.

Regards

Amit

Former Member
0 Kudos

Hi Bharati,

Wat error u r getting? Wat u wrote inside the if block? Can u explain in detail?

Regards

Fahad Hamsa

luciano_leitedasilva
Contributor
0 Kudos

Hi there,

This is happing because the current time is coming with some hours and minutes.

Try the following code:

Calendar c = Calendar.getInstance();

c.set(Calendar.HOUR_OF_DAY, 0);

c.set(Calendar.MINUTE, 0);

c.set(Calendar.SECOND, 0);

c.set(Calendar.MILLISECOND, 0);

Date theDate = (Date) wdContext.currentContextElement().getAttributeValue("fld");

IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute("fld");

Date currentDate = new Date(c.getTimeInMillis());

if (theDate.before(currentDate)) {

//Throw an error message

}

Hope it help you!

Regards,

Luciano

Former Member
0 Kudos