我正在Web应用程序中使用以下功能比较两个日期相等或之前或之后的日期。
public class DateComareClass{
public static void main(String args[]) throws ParseException, IOException {
DateTimeComparator dateTimeComparator=DateTimeComparator.getDateOnlyInstance();
Calendar cal = Calendar.getInstance();
DateFormat dftreg = new SimpleDateFormat("yyyy-MM-dd");
Date currentDate = new Date(cal.getTime().getTime());
Date SignUpDateCheck = (Date) dftreg.parse("2018-07-05");
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDate + " is the currentDate ");
int retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval);
SignUpDateCheck = (Date) dftreg.parse("2018-07-13");
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDate + " is the currentDate ");
retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval);
}
}
它打印
Thu Jul 05 00:00:00 2018 is the SignUpDateCheck
Thu Jul 12 12:03:39 2018 is the currentDate
1
Fri Jul 13 00:00:00 2018 is the SignUpDateCheck
Thu Jul 12 12:03:39 2018 is the currentDate
-1
但是,如果我在我的web应用程序中使用相同的逻辑,则似乎将retval始终打印为-1,无论它是在日期之后,之前还是等于日期。
我在webapp中的代码段
String GenerateSignupDate=year+"-"+month+"-"+day;
SignUpDateCheck= (Date) dftreg.parse(GenerateSignupDate);
System.out.println(SignUpDateCheck + " is the SignUpDateCheck ");
System.out.println(currentDateReg + " is the currentDateReg ");
//System.out.println(SignUpDateCheck.before(currentDateReg) + " is the answer ");
retval=dateTimeComparator.compare(currentDate,SignUpDateCheck);
System.out.println(retval + " is the retval ");
但是注册时,当前日期可以正确打印。只会错误地打印retval。
感谢您的帮助。