Skip to Content
avatar image
Former Member

Past Date check

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    avatar image
    Former Member
    Jun 12, 2007 at 05:21 AM

    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%20java/wd%20tutorials/a%20simple%20input%20form%20with%20message%20and%20error%20service%20support.pdf

    Regards,

    Mathan MP

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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.

  • avatar image
    Former Member
    Jun 11, 2007 at 08:11 PM
    Add comment
    10|10000 characters needed characters exceeded

  • Jun 11, 2007 at 09:27 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 12, 2007 at 11:36 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 12, 2007 at 01:19 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 13, 2007 at 10:42 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 12, 2007 at 05:59 AM

    Hi Bharati,

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

    Regards

    Fahad Hamsa

    Add comment
    10|10000 characters needed characters exceeded