提交 e97d5ae7 authored 作者: Patrick Brielmayer's avatar Patrick Brielmayer

Removed deprecated functions getMonth and setMonth

上级 cdeb93e4
...@@ -902,9 +902,17 @@ public class DateTimeUtils { ...@@ -902,9 +902,17 @@ public class DateTimeUtils {
return dateValue(y, m + 3, (int) d); return dateValue(y, m + 3, (int) d);
} }
public static Timestamp addMonths(final Timestamp timestamp, final int numberMonths) { /**
timestamp.setMonth(timestamp.getMonth() + numberMonths); * Adds the number of months to the date. If the resulting month's number of days is less than the original's day-of-month, the resulting
return timestamp; * day-of-months gets adjusted accordingly:
*
* 30.04.2007 - 2 months = 28.02.2007
*/
public static Timestamp addMonths(final Timestamp refDate, final int nrOfMonthsToAdd) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(refDate);
calendar.add(Calendar.MONTH, nrOfMonthsToAdd);
return new Timestamp(calendar.getTime().getTime());
} }
} }
...@@ -1398,19 +1398,23 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1398,19 +1398,23 @@ public class TestFunctions extends TestBase implements AggregateFunction {
} }
private void testAddMonths() throws ParseException { private void testAddMonths() throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-01-29"); Timestamp date;
Timestamp expected;
Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2013-02-29"); // 01-Aug-03 + 3 months = 01-Nov-03
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 1)); date = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-08-01").getTime());
expected = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-11-01").getTime());
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-29"); assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 3));
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 12));
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2012-11-29"); // 31-Jan-03 + 1 month = 28-Feb-2003
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -2)); date = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-01-31").getTime());
expected = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-02-28").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), 1));
expected = new SimpleDateFormat("yyyy-MM-dd").parse("2012-01-29"); // 21-Aug-2003 - 3 months = 21-May-2003
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -12)); date = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-08-21").getTime());
expected = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse("2003-05-21").getTime());
assertEquals(expected, DateTimeUtils.addMonths(new Timestamp(date.getTime()), -3));
} }
private void testToCharFromDateTime() throws SQLException { private void testToCharFromDateTime() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论