-5

I am using a function to take out the difference between a given date and current date. At all the places, that function is working just fine but at one place, it is throwing the unparseable date exception.

Below is the function that I use:

public boolean timeDiff24hrs(String alarmTime) {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = new Date();
    String s = sdf.format(date);
    String alarmDateStr="";
    try {
        Date alarmDate=sdf.parse(alarmTime);
        alarmDateStr=sdf.format(alarmDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    int diff = diffOf2Days(alarmDateStr, s);

    if (diff > 1439) {
        return true;
    }
    return false;
}

Here I get the exception trying to execute the try block.

The alarmDate that I pass into the function is : 1/31/2018 8:58:12 AM +00:00

Can someone please explain.

The Bat
  • 1,085
  • 1
  • 13
  • 31
  • 6
    For date "1/31/2018 8:58:12 AM +00:0" your date formate is not correct . – ADM Feb 09 '18 at 11:03
  • 1
    that date is not for the format you are using – Abdul Kawee Feb 09 '18 at 11:04
  • `unparseable` comes when passing date is not valid format. – Hemant Parmar Feb 09 '18 at 11:05
  • 1
    Change Your Date Format To Appropriate Format – Learning Always Feb 09 '18 at 11:06
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Feb 09 '18 at 11:58
  • If you based your code off some tutorial, you either found a very poor tutorial (they exist) or didn’t read it well enough. Try a couple of others (and I repeat: try one that uses `OffsetDateTime` and `DateTimeFormatter` from `java.time`). – Ole V.V. Feb 09 '18 at 12:03
  • I recommend `OffsetDateTime.parse("1/31/2018 8:58:12 AM +00:00", DateTimeFormatter.ofPattern("M/d/uuuu h:mm:ss a xxx", Locale.ENGLISH))`. – Ole V.V. Feb 09 '18 at 12:05
  • This question is *not* an exact duplicate of [java.text.ParseException: Unparseable date](https://stackoverflow.com/questions/7254307/java-text-parseexception-unparseable-date). In that question the locale was the problem. In this question the problem is that the format pattern string and the alarm date string don’t match when it comes to the `T` and what follows. Very many questions have been asked about unparseable date exception from `SimpleDateFormat`, and searching those is a good idea (also before posting a new question). – Ole V.V. Feb 09 '18 at 12:20

1 Answers1

0

Your date format MM/dd/yyyy'T'HH:mm:ss which your using is incorrect 1/31/2018 8:58:12 AM +00:00 and return different value from expected.

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");

return 1/31/2018T8:58:12

And you are trying to pass below

1/31/2018 8:58:12 AM +00:00
duggu
  • 37,851
  • 12
  • 116
  • 113