gpt4 book ai didi

java - 如果日期错误,则不会打印出错误消息

转载 作者:行者123 更新时间:2023-11-29 03:16:31 24 4
gpt4 key购买 nike

如果交易日期大于计算日期,我想返回消息“Date is very wrong”。相反,它只打印“-1 天”。

import java.text.SimpleDateFormat;
import java.util.Date;

public class Age {

public static long computeAge(String transDate, String computeDate){
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

long lapseDays = 0;

try {
Date TDate = format.parse(transDate);
Date CDate = format.parse(computeDate);

long aging = CDate.getTime() - TDate.getTime();
lapseDays = aging/(24*60*60*1000);

}catch (Exception e){
e.printStackTrace();
System.out.print("Date is very wrong");
}

return lapseDays;
}
public static void main(String[] args) {

String transaction = "10/01/2014";
String computation = "09/30/2014";
System.out.println(computeAge(transaction, computation)+ " days");
}
}

最佳答案

如果我理解你的问题,你可以调用方法 Date.after(Date)和类似的东西

Date TDate = format.parse(transDate);
Date CDate = format.parse(computeDate);
if (TDate.after(CDate)) {
System.out.print("Date is very wrong");
return -1;
}

关于java - 如果日期错误,则不会打印出错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26256902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com