0

I want to compare the date with current date and time to know whether date and time has been passed alredy or not. These are two date objects:

currentDateTime = 2019-11-22 05:25:19 AM eventEndDateTime = 2019-11-22 05:45:00 AM

both are Date() objects. and i am comparing like :currentDateTime.after(eventEndDateTime) so it's giving me true. But as we can see my current date before eventEndDateTime.

SimpleDateFormat endFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
        TimeZone endTz = TimeZone.getTimeZone("UTC");
        endFormatter.setTimeZone(endTz);
        Date eventEndTime = null;
        try {
            eventEndTime = endFormatter.parse(endFormatter.format(new Date(eventDetailsResponce.getData().getEndMillis() * 1000)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat currentFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
        TimeZone currentTz = TimeZone.getTimeZone(eventDetailsResponce.getData().getTimeZone());
        currentFormatter.setTimeZone(currentTz);
        Date currentTime = null;
        try {
            currentTime = currentFormatter.parse(currentFormatter.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (currentTime.after(eventEndTime)) {
        //here i am getting true. 
        }

I have tried with Calendar calendar = Calendar.getInstance() but i couldn't found any solution.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • convert both in milliseconds and than compare – Danial clarc Nov 22 '19 at 11:25
  • Possible duplicate of [Comparing two date objects of different TimeZones and get the exact time difference in seconds](https://stackoverflow.com/questions/44722209/comparing-two-date-objects-of-different-timezones-and-get-the-exact-time-differe) – Ricardo A. Nov 22 '19 at 11:56
  • 1
    currentDateTime.after(eventEndDateTime) returning true may be correct if the timezone of currentDateTime i.e. eventDetailsResponce.getData().getTimeZone() is say UTC + 6 hours – matdev Nov 22 '19 at 13:12
  • Which time zone did you get from `eventDetailsResponce`? – Ole V.V. Nov 23 '19 at 15:05
  • The classes `SimpleDateFormat`, `TimeZone` and `Date` are poorly designed and long outdated. Consider using java.time, the modern Java date and time API, instead. If for API level under 26, then through the the ThreeTenABP library. Search for it and for the date and time tutorial. – Ole V.V. Nov 24 '19 at 15:51

4 Answers4

1

try it out

if (date1.compareTo(date2) > 0) {
   Log.i("app", "Date1 is after Date2");
 } else if (date1.compareTo(date2) < 0) {
   Log.i("app", "Date1 is before Date2");
 } else if (date1.compareTo(date2) == 0) {
   Log.i("app", "Date1 is equal to Date2");
}
Govind Prajapati
  • 208
  • 3
  • 10
1

Calendar.after(Date) and Calendar.before(Date) works perfect for date comparison. I tried your code with a few changes and it gives me the expected result.

private void doCheckDate() {

        String dateFormat = "yyyy-MM-dd HH:mm:ss aa";
        SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);

        String dateCurrentString = "2019-11-22 05:25:19 AM";
        String dateEndString = "2019-11-22 05:45:00 AM";

        TimeZone zone = TimeZone.getTimeZone("UTC");
        dateFormatter.setTimeZone(zone);

        Date currentTime = null;
        Date eventEndTime = null;

        try {
            currentTime = dateFormatter.parse(dateCurrentString);
            eventEndTime = dateFormatter.parse(dateEndString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        boolean isAfter = currentTime.after(eventEndTime);

        Log.d(TAG, "Current Time: " + currentTime);
        Log.d(TAG, "Event Time: " + eventEndTime);

        Log.d(TAG, "Is current time after event time: " + isAfter);
    }

Produces following output;

Current Time: Fri Nov 22 05:55:19 GMT+05:30 2019
Event Time: Fri Nov 22 06:15:00 GMT+05:30 2019
Is current time after event time: false

Mudassir
  • 13,031
  • 8
  • 59
  • 87
  • it seems you comaparing both dates with one timezone. actully scenario is : firstly: eventEndDate need to convert into UTC timezone and current system time need to convert into given timezone which is not UTC and then i want to compare. – Lokesh Yadav Nov 22 '19 at 11:53
0

currentDateTime.after(eventEndDateTime) returning true is correct if the timezone of currentDateTime i.e. eventDetailsResponce.getData().getTimeZone() is say UTC + 6 hours

In order to compare two dates, you need to convert them into the same time zone.

Also you could simplify the way you set eventEndTime as follows:

Date eventEndTime = new Date(eventDetailsResponce.getData().getEndMillis() * 1000);
matdev
  • 4,115
  • 6
  • 35
  • 56
0

Another solution to add to the answers you already have would be comparing in milliseconds:

currentTime.getTime() > eventEndTime.getTime()
Ricardo A.
  • 685
  • 2
  • 8
  • 35