Java是否与Oracle函数MONTHS_BETWEEN类似?

最佳答案

您可以使用:

public static int monthsBetween(Date minuend, Date subtrahend){

    Calendar cal = Calendar.getInstance();
    cal.setTime(minuend);
    int minuendMonth =  cal.get(Calendar.MONTH);
    int minuendYear = cal.get(Calendar.YEAR);
    cal.setTime(subtrahend);
    int subtrahendMonth =  cal.get(Calendar.MONTH);
    int subtrahendYear = cal.get(Calendar.YEAR);

    return ((minuendYear - subtrahendYear) * (cal.getMaximum(Calendar.MONTH)+1)) +
    (minuendMonth - subtrahendMonth);
}

编辑:

根据此documentation MONTHS_BETWEEN返回分数结果,我认为此方法可以实现相同效果:
public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date d = sdf.parse("02/02/1995");
    Date d2 = sdf.parse("01/01/1995");
    System.out.println(monthsBetween(d, d2));

}

public static double monthsBetween(Date baseDate, Date dateToSubstract){

    Calendar cal = Calendar.getInstance();
    cal.setTime(baseDate);
    int baseDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
    int baseMonth =  cal.get(Calendar.MONTH);
    int baseYear = cal.get(Calendar.YEAR);

    cal.setTime(dateToSubstract);
    int subDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
    int subMonth =  cal.get(Calendar.MONTH);
    int subYear = cal.get(Calendar.YEAR);

    //int fullMonth = ((baseYear - subYear) * (cal.getMaximum(Calendar.MONTH)+1)) +
    //(baseMonth - subMonth);
    //System.out.println(fullMonth);

    return ((baseYear - subYear) * (cal.getMaximum(Calendar.MONTH)+1)) +
           (baseDayOfYear-subDayOfYear)/31.0;
}

08-06 22:12