Parsing date gives error in webapp

By | July 12, 2018
Questions:

I am using the below function in my web app to compare two dates are equal or after or before.

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);


    }
}

It prints

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

But if I use the same logic in my webapp it seems to print always the retval as -1 whether it is after ,before and equal to the date .

My code snippet in 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 ");

But the Signup, current dates are printed correctly. It only prints the retval incorrectly.

Any help is appreciated .

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *