贷款amortizarion计划与日程安排日期在java

时间:2016-04-11 14:33:08

标签: java date

我正在尝试在我的贷款计划中安排日程安排,但日期错误,因为他们已经跳过几个月:

 public static void printAmortizationSchedule(double principal, double annualInterestRate, int numYears) {
        double interestPaid, principalPaid, newBalance;
        double monthlyInterestRate, monthlyPayment;
        int month;
        int numMonths = numYears * 12;

        // Output monthly payment and total payment
        monthlyInterestRate = annualInterestRate / 12;
        monthlyPayment = monthlyPayment(principal, monthlyInterestRate, numYears);
        System.out.format("Monthly Payment: %8.2f%n", monthlyPayment);
        System.out.format("Total Payment:   %8.2f%n", monthlyPayment * numYears * 12);

        // Print the table header
        printTableHeader();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        for (month = 1; month <= numMonths; month++) {
            // Compute amount paid and new balance for each payment period
            interestPaid = principal * (monthlyInterestRate / 100);
            principalPaid = monthlyPayment - interestPaid;
            newBalance = principal - principalPaid;
            cal.add(Calendar.MONTH, month);
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-YYYY", Locale.getDefault());
            String dddate = sdf.format(cal.getTime());
            // Output the data item
            printScheduleItem(month, dddate, interestPaid, principalPaid, newBalance);

            // Update the balance
            principal = newBalance;
        }
    }

打印计划方法:

 private static void printScheduleItem(int month, String dddate, double interestPaid, double principalPaid, double newBalance) {
        System.out.format("%8d%12s%10.2f%10.2f%12.2f\n",
                month, dddate, interestPaid, principalPaid, newBalance);
    }

我确信问题是将日期添加到日期中如何解决?

1 个答案:

答案 0 :(得分:1)

你的错误在这里:

cal.add(Calendar.MONTH, month);

应该是:

cal.add(Calendar.MONTH, 1);