-2

I'm developing an app in which I want to check if startTime and startDate is less than endTime and endDate and if it is then only the information should be saved in FirebaseDatabase.

I'm using this code to check is startTime < endTime and startDate < endDate:

        try {
            Date date1 = currentTime.parse(btnChooseStartTime.getText().toString());
            Date date2 = currentTime.parse(btnChooseEndTime.getText().toString());
            differenceBetweenStartEndTime = date2.getTime() - date1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
    }

            isEndDateGreaterThanStartDate = CheckDates(btnChooseEndDate.getText().toString(), btnChooseStartDate.getText().toString());

            if (String.valueOf(differenceBetweenStartEndTime).contains("-")) {
               Snackbar snackbar = Snackbar
                                  .make(coordinatorLayout, "Start time is greater than end time. Fix it please.", Snackbar.LENGTH_SHORT);
               snackbar.setDuration(3500);
               snackbar.show();
               progressDialogPostingE.dismiss();
            } else if (!isEndDateGreaterThanStartDate) {
               Snackbar snackbar = Snackbar
                                  .make(coordinatorLayout, "Start date is greater than end date. Fix it please.", Snackbar.LENGTH_SHORT);
               snackbar.setDuration(3500);
               snackbar.show();
               progressDialogPostingE.dismiss();
            } else {
// code for storing information in database
}

here's CheckDates() method:

public static boolean CheckDates(String d2, String d1)    {
        SimpleDateFormat dfDate  = new SimpleDateFormat("dd-MM-yyyy");
        boolean b = false;
        try {
            if(dfDate.parse(d1).before(dfDate.parse(d2)))
            {
                b = true;//If start date is before end date
            }
            else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
            {
                b = true;//If two dates are equal
            }
            else if (dfDate.parse(d2).before(dfDate.parse(d1)))
            {
                b = false; //If start date is after the end date

            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return b;
    }

This code is working almost fine but when I'm setting startDate = 20-11-2016 and startTime = 10 AM and then endDate = 21-11-2016 and endTime = 8 AM, I'm getting "Start time is greater than end time. Fix it please." shown in the SnackBar because 8 AM is less than 10 AM, but here the 10 AM is of previous day and 8 AM is of next day. So, it should have stored the data in database instead of showing the SnackBar.

What's wrong and how to fix this?

Please let me know.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

1 Answers1

2

Add SimpleDateFormat with hours,minutes and seconds

 SimpleDateFormat dfDate  = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  boolean b = false;
    try {
        if(dfDate.parse(d1).before(dfDate.parse(d2)))
        {
            b = true;//If start date is before end date
        }
        else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
        {
            b = true;//If two dates are equal
        }
        else if (dfDate.parse(d2).before(dfDate.parse(d1)))
        {
            b = false; //If start date is after the end date

        }
sasikumar
  • 12,540
  • 3
  • 28
  • 48