/** * 设置月份, 0-11. */ public static Date setMonths(@NotNull final Date date, int amount) { return DateUtils.setMonths(date, amount); }
/** * 设置月份, 1-12. */ public static Date setMonths(@NotNull final Date date, int amount) { if (amount < 1 || amount > 12) { throw new IllegalArgumentException("monthOfYear must be in the range[ 1, 12]"); } return DateUtils.setMonths(date, amount - 1); }
@Test public void testSetMonths() throws Exception { Date result = DateUtils.setMonths(BASE_DATE, 5); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 5, 5, 4, 3, 2, 1); result = DateUtils.setMonths(BASE_DATE, 1); assertNotSame(BASE_DATE, result); assertDate(BASE_DATE, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 2000, 1, 5, 4, 3, 2, 1); try { DateUtils.setMonths(BASE_DATE, 12); fail("DateUtils.setMonths did not throw an expected IllegalArgumentException."); } catch (final IllegalArgumentException e) { } }
/** * 设置月份, 0-11. */ public static Date setMonths(@NotNull final Date date, int amount) { return DateUtils.setMonths(date, amount); }
/** * 设置月份, 0-11. */ public static Date setMonths(@NotNull final Date date, int amount) { return DateUtils.setMonths(date, amount); }
/** * return current date time by specified hour:minute * * @param plan format: hh:mm */ public static Date getCurrentDateByPlan(String plan, String pattern) { try { FastDateFormat format = FastDateFormat.getInstance(pattern); Date end = format.parse(plan); Calendar today = Calendar.getInstance(); end = DateUtils.setYears(end, (today.get(Calendar.YEAR))); end = DateUtils.setMonths(end, today.get(Calendar.MONTH)); end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH)); return end; } catch (Exception e) { throw new RuntimeException( e ); } } }
/** * return current date time by specified hour:minute * * @param plan format: hh:mm */ public static Date getCurrentDateByPlan(String plan, String pattern) { try { FastDateFormat format = FastDateFormat.getInstance(pattern); Date end = format.parse(plan); Calendar today = Calendar.getInstance(); end = DateUtils.setYears(end, (today.get(Calendar.YEAR))); end = DateUtils.setMonths(end, today.get(Calendar.MONTH)); end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH)); return end; } catch (Exception e) { throw ExceptionUtil.unchecked(e); } } }