Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java decimal to date value

Tags:

java

date

decimal

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.

like image 842
damien535 Avatar asked Jun 16 '26 16:06

damien535


2 Answers

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);
like image 131
OrangeDog Avatar answered Jun 19 '26 07:06

OrangeDog


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).

like image 37
Jim Avatar answered Jun 19 '26 06:06

Jim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!