Java后端获取两个日期之间的天数,获取两个日期之间的月数

获取两个日期之间的天数:
传入两个日期获取它们之间的天数

	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}

获取两个日期之间的月数:
传入两个日期获取它们之间的月数

	public static double getDistanceMonth(Date before, Date after){
	     Calendar bef = Calendar.getInstance();
	     Calendar aft = Calendar.getInstance();
	     bef.setTime(before);
	     aft.setTime(after);
	     int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
	     int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
	     return Math.abs(month + result);
	}

你可能感兴趣的:(后端,获取两个日期之间的月数)