I’m attempting something, and I’m not quite sure how to approach it. I have two user defined values, a duration and a duration unit. At this point I will already have a start date, so I want to apply the duration and durationUnit somehow to startDate to get the endDate.
Duration is a decimal, durationUnit a HardCode/AbstractCode value and startDate is a Date.
So if the startDate is 17/12/2010......and the user enters the following;
Duration: 3
Duration Unit: Months
I want the endDate to then be calculated as 17/03/2011. Any idea on how I could do this? The duration unit could be Days, Months or Years.
Have a look at joda-time. It's a superb replacement for Date/Calendar. You can do things like:
DateTime newDate = startDate.plusDays(days);
.plusYears(years);
Period toAdd = periodFormatter.parse(inputString);
DateTime newDate = startDate.plus(toAdd);
You can map the Day/Month/Year to the appropriate Calendar DAY, MONTH or YEAR. Then use calendar.add(). If by decimal you mean a double then you will probably have to do some processing to turn parts of a value into appropriate values (such as changing .5 DAY to 12 hours).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With