我在date对象中的after()函数有问题。
这是我要运行的功能:
private void setDebitDateInAction(Action action) {
// The day the credit card debit is made
int dD = action.getMethodPayment().getDebitDate();
System.out.println("1) The debit day is every "+dD+" in month.");
//The date the purchase was made
Date actionDate = action.getActionDate();
System.out.println( "2) The purchase was in "+ actionDate.toString() );
//The date the purchase will be charged
Date debitDate = actionDate;
System.out.println("3) Currently the debit date is "+debitDate.toString() );
//0 means immediate charge
if(dD == 0) {
//Leave the billing date as the purchase date.
}else {
// Change the day of the billing date
debitDate.setDate(dD);
System.out.println("4) The day of the debit date has changed to "+dD+" and new the debit date is " +debitDate.toString() );
if(actionDate.after(debitDate)) {
debitDate.setMonth(debitDate.getMonth()+1);
System.out.println("5) The month of the debit date has changed to "+debitDate.getMonth() + 1 +" and new the debit date is " +debitDate.toString() );
}
}
action.setDebitDate(debitDate);
System.out.println("6) ActionDebit is set to" +debitDate.toString() );
}
这是在我将ActionDate为15/07/2019且methodPayment的dateDate(出于信用目的)的dateDate为10的Action函数放入控制台后得到的结果。
1) The debit day is every 10 in month.
2) The purchase was in Mon Jul 15 03:00:00 IDT 2019
3) Currently the debit date is Mon Jul 15 03:00:00 IDT 2019
4) The day of the debit date has changed to 10 and now the debit date is Wed Jul 10 03:00:00 IDT 2019
6) ActionDebit is set toWed Jul 10 03:00:00 IDT 2019
我希望结算日期为10/08/2019,但不会成功。
由于某种原因,他无法识别购买日期是在开票日期之后。
最佳答案
因此,有两种(以下)方案:
CompareTo()-如果您使用正确的`debitDate:
这是因为您的actionDate
和debitDate
相同,因此在两个日期相等的情况下if(actionDate.after(debitDate))
将始终返回false。
您应该使用compareTo()
代替。
代码应如下所示:
if(actionDate.compareTo(debitDate) >=0) {
debitDate.setMonth(debitDate.getMonth()+1);
System.out.println("5) The month of the debit date has changed to "+debitDate.getMonth() + 1 +" and new the debit date is " +debitDate.
}
您使用了错误的
debitDate
:您应该设置正确的debitDate,代码应如下所示:
//购买日期
日期debitDate = action.getMethodPayment()。getDebitDate();
希望这能解决您的问题!