首页 > 解决方案 > 解析日期在 webapp 中出现错误

问题描述

我在我的网络应用程序中使用以下函数来比较两个日期是否相等或之后或之前。

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

但是,如果我在我的 webapp 中使用相同的逻辑,它似乎总是将 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。

任何帮助表示赞赏。

标签: javadatejodatime

解决方案


推荐阅读