-1
    dateTime_form_api = "2020/02/11 10:23 AM";

    dateTime_from_system = "2020/02/11 13:00 PM";

I want to compare dateTime_form_api and dateTime_from_system

if dateTime_from_system after dateTime_form_api show "Next time"

if dateTime_from_system before dateTime_form_api show "Last time"

but if dateTime_form_api's date equals dateTime_from_system's date

only time need to compare , time's sompare, how to do in Android ?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
leona lin
  • 25
  • 5
  • 1
    Does this answer your question? [How to compare two dates along with time in java](https://stackoverflow.com/questions/22039991/how-to-compare-two-dates-along-with-time-in-java) – Molly Feb 11 '20 at 05:08
  • 1
    [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). This has been asked and answered with variations very, very many times before. Please show some effort in searching when you ask a new question about the same. – Ole V.V. Feb 11 '20 at 05:57

1 Answers1

0

This code below maybe help you, if you want to count the difference of two dates in Hours or seconds, then just change the timeUnit to TimeUnit.SECONDS or TimeUnit.HOURS

    TimeUnit timeUnit = TimeUnit.DAYS;

    String dateTime_form_api = "2020/02/11 10:23 AM";

    String dateTime_from_system = "2020/02/11 13:00 PM";

    Date date1, date2;  
    try {
        date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm aa").parse(dateTime_from_system);
        date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm aa").parse(dateTime_form_api);

        long diffInMillies = date1.getTime() - date2.getTime();

        long diffInDays = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);  
        System.out.println(diffInDays);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
TIenHT
  • 61
  • 1
  • 7
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Feb 11 '20 at 05:54