cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Adding Years to a Java.util.date

MichaelWright
1-Newbie

Adding Years to a Java.util.date

I have a simple task that I am struggling with. We could work around it, but I'd like to automate as much as possible for my users in an effort to make it easier for them. I have a workflow that has a variable for them to enter a date. I want to be able to add 4 years to that date by using an expression robot and assign it to another variable. In my mind it seems like a simple task that I keep getting syntax errors for when I attempt my solution. I was wondering if anybody had done this before or if anybody could provide any insight on how to get this to work smoothly. Thanks.

3 REPLIES 3

Date now = new Date(System.currentTimeMillis());

System.out.println(now);
//Calendar calendar = Calendar.getInstance(TimeZoneHelper.getTimeZone(), SessionHelper.getLocale());
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.YEAR, 4);
Date later = calendar.getTime();
System.out.println(later);

Keep gmt offsets in mind if you're passing dates to and from the methodserver.

I don't know if wfexpressions know about java.util by default. You may have to use full classnames.

Also, I recommend not putting this stuff inside your expression. Instead, call another class that does this work. Later, if you decide you don't want to add four years, you can just return the date as is. Or if you decide you want to add five years, you could change that easily.

//wfvariable Timestamp requiredDate

//inside your transition/expression

{

requiredDate = com.custom.wfexpr.UpdateRequiredDate.update(requiredDate);

}

public class UpdateRequiredDate {

public static Timestamp update(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, 4);
return new Timestamp(calendar.getTimeInMillis());
}

}

I see you worked with Dates in windchill. Maybe you've got an idea concerning my problem defined here: http://communities.ptc.com/thread/35822

I'ld like to have a calendar picker for the date variables in activities of my workflows.

Thanks if you can help.

Announcements