Java 贷款分期付款计划,计划日期为爪哇语
我试图在我的贷款计划中包含计划日期,但日期错误,因为它们跳过了几个月:Java 贷款分期付款计划,计划日期为爪哇语,java,date,Java,Date,我试图在我的贷款计划中包含计划日期,但日期错误,因为它们跳过了几个月: public static void printAmortizationSchedule(double principal, double annualInterestRate, int numYears) { double interestPaid, principalPaid, newBalance; double monthlyInterestRate, monthlyPayment;
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;
}
}
我敢肯定,问题在于将月份添加到日期中,我如何解决这个问题?您的错误在这里:
cal.add(Calendar.MONTH, month);
应该是:
cal.add(Calendar.MONTH, 1);
cal.add(日历月、月份)代码>可能应该是cal.add(日历月,1)
如果您需要的是日期(没有时间),您可能应该使用而不是日历
。
cal.add(Calendar.MONTH, 1);