cancel
Showing results for 
Search instead for 
Did you mean: 

How to subtract i date from other.

Former Member
0 Kudos

Dear Experts,

I have to subtract a date from todays date , and if the result is greator then 60 I have to raise a message please tell me how to acheive this.

Regards

Upendra

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Please go through this thread

Regards,

ramesh

Former Member
0 Kudos

Thanks Ramesh for your response.

It would be very helpful if you can please tell me how to get the Todays date in java.sql.Date

Regards

Upendra

former_member192434
Active Contributor
0 Kudos

Hi

Try this

java.util.Date today = new java.util.Date();

java.sql.Date sqlToday = new java.sql.Date(today.getTime());

Thanks

Former Member
0 Kudos

Hi,

use this

Date dt = new Date(Calendar.getInstance().getTimeInMillis());

Regards,

ramesh

Former Member
0 Kudos

Hi Upendra,

I was working with a similar kind of requirement a few days ago. But my requirement was more or less

the same as yours. My requirement was as follows:-

The user enters a date at runtime and program should check if the entered date is not older than

6months from the present Month. If the entered date is older than 6months, it should raise an error

message. I have written the following Java Code. Thought it might be of some use to you. Have a look and close the thread if you find your answer.


public void checkPastDate( java.lang.String Datedep )
  {
    //@@begin checkPastDate()
    try{
    	IWDMessageManager msgMan = wdComponentAPI.getMessageManager();
	
		Calendar now = Calendar.getInstance();
	
		int x = now.get(Calendar.MONTH);
		int i = now.get(Calendar.YEAR);

		Calendar now1 = Calendar.getInstance();
	
		Date usrDate = (Date)wdContext.currentGeneralDataElement().getDatedep();
		now1.setTime(usrDate);
	
		int y = now1.get(Calendar.MONTH);
		int j = now1.get(Calendar.YEAR);
	
		if(i==j && ((x-y) >= 7)){
			msgMan.reportMessage
(IMessageVcTreGeneralData.DATE_IN_PAST , new Object[] {Datedep}, true);
}
		if(i>j && ((x-y)>= -5)){
			msgMan.reportMessage(IMessageVcTreGeneralData.DATE_IN_PAST , new Object[] {Datedep}, true);
		}
    }
    catch(NullPointerException e){
		IWDMessageManager msgMan = wdComponentAPI.getMessageManager();
		msgMan.reportMessage(IMessageVcTreGeneralData.REQUIRED_FIELDS, new Object[] {Datedep}, true);	
    }
	   
    //@@end
  }

I hope this helps.

Regards,

PG.

Edited by: PG on Dec 24, 2008 12:02 PM

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

To calculate dates use the Calendar component. To compare dates use Strings.

-


boolean succes = true;

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");

//The Calendar keeps track of the days, months and the years.

//So I don't have to worry about creating non-existing dates (like 34.12.2008 or something)

Calendar calc = Calendar.getInstance(); //todays date

calc.add(Calendar.DATE, -60); //sixty days ago

Date sixtyDaysAgo = new Date(calc);

//Note: When you have a date and want to calculate with it,

//Put the Date into a Calendar using the setTime method of the Calendar class.

//Changing the date to a String removes any time-components (hours, minutes, milliseconds etc)

//which are there when creating a a Calendar. (which are really anoying when comparing dates)

sixtyDaysAgoStr = dateFormatter.format(sixtyDaysAgo);

userInputDateStr = dateFormatter.format(userInput);

//now just compare the strings using the compare method.

// 0. for equals.

// 1. for greater than.

// -1. for smaller than.

if (userInputDateStr.compareTo(sixtyDaysAgoStr) < 0) {

succes = false;

}

-


Jeschael