@Test public void testSubtractMonth1() throws Exception { String str1 = "2015-02-23 18:54:00"; String str2 = "2016-08-25 21:55:22"; int rs = DateUtil.subtractMonth(sdf.parse(str1), sdf.parse(str2)); assertEquals("计算有误", 18, rs); }
public static int subtractMonth(String date1, String date2) { int result; Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(DateInstance().parse(date1)); c2.setTime(DateInstance().parse(date2)); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH); if (year1 == year2) { result = month2 - month1; } else { result = 12 * (year2 - year1) + month2 - month1; } } catch (ParseException e) { e.printStackTrace(); result = -1; } return result; }
DateUtil { public static int subtractMonth(String date1, String date2) { int result; Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(DateInstance().parse(date1)); c2.setTime(DateInstance().parse(date2)); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH); if (year1 == year2) { result = month2 - month1; } else { result = 12 * (year2 - year1) + month2 - month1; } } catch (ParseException e) { e.printStackTrace(); result = -1; } return result; } }
DateUtil { public static int subtractMonth(String date1, String date2) { int result; Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(DateInstance().parse(date1)); c2.setTime(DateInstance().parse(date2)); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH); if (year1 == year2) { result = month2 - month1; } else { result = 12 * (year2 - year1) + month2 - month1; } } catch (ParseException e) { e.printStackTrace(); result = -1; } return result; }  }
DateUtil { public static int subtractMonth(String date1, String date2) { int result; Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(DateInstance().parse(date1)); c2.setTime(DateInstance().parse(date2)); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH); if (year1 == year2) { result = month2 - month1; } else { result = 12 * (year2 - year1) + month2 - month1; } } catch (ParseException e) { e.printStackTrace(); result = -1; } return result; }  static String currentDateTime(); static String dateTime(Date date); static Date dateTime(String datestr); static String currentDate(); static String date(Date date); static Date date(String dateStr); static String currentTime(); static String time(Date date); static Date time(String dateStr); static Date year(int year); static Date year(Date date, int year); static Date month(int month); static Date month(Date date, int month); static Date day(int day); static Date day(Date date, int day); static Date hour(float hour); static Date hour(Date date, float hour); static Date minute(int minute); static Date minute(Date date, int minute); static boolean isDate(String date); static long subtract(Date date1, Date date2); static long subtract(String date1, String date2); static int subtractMinute(String date1, String date2); static int subtractMinute(Date date1, Date date2); static int subtractHour(Date date1, Date date2); static int subtractHour(String date1, String date2); static int subtractDay(String date1, String date2); static int subtractDay(Date date1, Date date2); static int subtractMonth(String date1, String date2); static int subtractMonth(Date date1, Date date2); static int subtractYear(String date1, String date2); static int subtractYear(Date date1, Date date2); static String subtractTime(String date1, String date2); static String subtractDate(String date1, String date2); static int subDay(Date startTime, Date endTime); static int subDay(String startTime, String endTime); static long subtimeBurst(String startDate, String endDate, String timeBurst); static long subtimeBurst(Date startDate, Date endDate, String timeBurst); static Date calculate(String date, int second, String timeBurst); static Date calculate(Date date, int second, String timeBurst); static boolean between(String startTime, String endTime, Date date); static boolean between(Date startTime, Date endTime, Date date); }
DateUtil { public static int subtractMonth(String date1, String date2) { int result; Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(DateInstance().parse(date1)); c2.setTime(DateInstance().parse(date2)); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH); if (year1 == year2) { result = month2 - month1; } else { result = 12 * (year2 - year1) + month2 - month1; } } catch (ParseException e) { e.printStackTrace(); result = -1; } return result; }  static String currentDateTime(); static String dateTime(Date date); static Date dateTime(String datestr); static String currentDate(); static String date(Date date); static Date date(String dateStr); static String currentTime(); static String time(Date date); static Date time(String dateStr); static Date year(int year); static Date year(Date date, int year); static Date month(int month); static Date month(Date date, int month); static Date day(int day); static Date day(Date date, int day); static Date hour(float hour); static Date hour(Date date, float hour); static Date minute(int minute); static Date minute(Date date, int minute); static boolean isDate(String date); static long subtract(Date date1, Date date2); static long subtract(String date1, String date2); static int subtractMinute(String date1, String date2); static int subtractMinute(Date date1, Date date2); static int subtractHour(Date date1, Date date2); static int subtractHour(String date1, String date2); static int subtractDay(String date1, String date2); static int subtractDay(Date date1, Date date2); static int subtractMonth(String date1, String date2); static int subtractMonth(Date date1, Date date2); static int subtractYear(String date1, String date2); static int subtractYear(Date date1, Date date2); static String subtractTime(String date1, String date2); static String subtractDate(String date1, String date2); static int subDay(Date startTime, Date endTime); static int subDay(String startTime, String endTime); static long subtimeBurst(String startDate, String endDate, String timeBurst); static long subtimeBurst(Date startDate, Date endDate, String timeBurst); static Date calculate(String date, int second, String timeBurst); static Date calculate(Date date, int second, String timeBurst); static boolean between(String startTime, String endTime, Date date); static boolean between(Date startTime, Date endTime, Date date);  }