Compare between different time and then and display error message android

254 Views Asked by At

I have mostly spent 1 day for just comparing two times but no worth result until now. I am basically trying to compare between two different times which I want to avoid storing data id the first time less than the second time but I couldn't. In addition, the equal part is working smoothly but the second part not working

@Override
        public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) throws ParseException {
            String AM_PM = " AM";
            String pattern = "HH:mm";
            SimpleDateFormat format = new SimpleDateFormat(pattern);

            String mm_precede = "";
            if (hourOfDay >= 12) {
                AM_PM = " PM";
                if (hourOfDay >=13 && hourOfDay < 24) {
                    hourOfDay -= 12;
                }
                else {
                    hourOfDay = 12;
                }
            } else if (hourOfDay == 0) {
                hourOfDay = 12;
            }
            if (minute < 10) {
                mm_precede = "0";
            }

     if (id == 1 && id ==2) {
         Date date1 = format.parse(sTimeApp.getText().toString());
         Date date2 = format.parse(eTimeApp.getText().toString());
         DateTime da1 = new DateTime(date1);
         DateTime da2 = new DateTime(date2);
         System.out.println("Date" + date1);
         System.out.println("Date" + date1);
         if (da1.equals(da2) ){
             Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
             return;
         }else if (da1.isBefore(da2)){
             Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
             return;
         }

     }


    //        if (sTimeApp.equals(eTimeApp) ) {
    //            Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
    //            return;
    //        }

            if (id == 1) {
                if (minute <= 9) {
                    sTimeApp.setText(hourOfDay + ":" +  mm_precede + minute + AM_PM);
                } else {

                    sTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
                }
            } else if (id == 2) {
                if (minute <= 9) {
                    eTimeApp.setText(hourOfDay + ":" +  mm_precede + minute + AM_PM);
                } else {
                    eTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
                }
            }

        }
    }
1

There are 1 best solutions below

0
On

java.time

I think you’re after something like this:

static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);

@Override
public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) {
    if (id == 1 || id == 2) {
        LocalTime start = LocalTime.parse(sTimeApp.getText(), timeFormatter);
        LocalTime end = LocalTime.parse(eTimeApp.getText(), timeFormatter);
        if (start.equals(end)) {
            Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
            return;
        } else if (end.isBefore(start)) {
            Toast.makeText(this, "Error , Time should not be before ", Toast.LENGTH_SHORT).show();
            return;
        }

        LocalTime newTime = LocalTime.of(hourOfDay, minute);
        String formattedTime = newTime.format(timeFormatter);
        if (id == 1) {
            sTimeApp.setText(formattedTime);
        } else {
            assert id == 2 : id;
            eTimeApp.setText(formattedTime);
        }
    }
}

If you are already using Joda-Time, you may not want to upgrade to java.time just now. Joda-Time too has a LocalTime class that you can use much in the same way as I am using org.threeten.bp.LocalTime in the above.

I am unsure whether you also wanted to compare the time set through the time picker to either the start or end time already in sTimeApp or eTimeApp. I have not introduced such a comparison.

What went wrong in your code?

  • id == 1 && id ==2 cannot be true. In my code I have suggested id == 1 || id == 2, but please check what you want.
  • I suspect you compared the wrong way around. As I read your code, you issue an error message if the start time is before the end time, which I would think of as the normal and correct situation. In my code I instead issue an error message if the end time is before the start time.
  • You seem to be hand formatting the time to display. This is the overly complicated way. Instead I am using a DateTimeFormatter for formatting the time (again, Joda-Time has similar functionality).
  • You are using the outdated SimpleDateFormat and Date classes. Instead I recommend you use java.time, the modern Java date and time API. If you are already using Joda-Time, that’s a nice option too. In both cases avoid the mentioned outdated classes completely since either Joda-Time or java.time offers all the functionality you need.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links