on 12-24-2008 9:19 AM
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.