Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oracle add_months function differs from Java

I recently discovered a difference between Oracle adds months to a given date (using ADD_MONTHS function) and the way Java adds months to a Calendar object.

For instance in oracle:

select add_months('2009-02-28', +1) from dual;

produced the result: "09-03-31"

And the query:

select add_months('2009-02-28', -1) from dual;

Produces the result "09-01-31"

However in Java, the results of the same calculations (using GregorianCalendar.add() method) are (respectively): 09-03-28 and 09-01-28

Is there some way to make Oracle and Java behave the same? (e.g. some setting in oracle or some parameter in Java)?

like image 584
nivcaner Avatar asked Dec 08 '25 08:12

nivcaner


1 Answers

When you do month arithmetic you need to decide, from the business point of view, what is the right way to deal with months with differing numbers of days. The flip side to the issue you raised is what happens when going from a longer month, like August, to a shorter one like February (and even from Feb to Feb if leap years are involved). If you are happy for errors to be reported because the calculation cannot find 'Feb-30', then look at INTERVAL

like image 174
Gary Myers Avatar answered Dec 09 '25 22:12

Gary Myers



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!