Search code examples
javajodatime

How to convert a string date to millis with Joda (java)?


in my case I have: "Sun, 11 Apr 21 09:09:13 +0000"

my code is:

    Long result = null;
    try {
        DateTimeFormatter dateFormatterRssPubDate = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
        result = dateFormatterRssPubDate.parseMillis(givenStringDate);
        if(result!=null && result>=0) 
           return result;
     } catch (Exception e) {
        // logging
     }

but it returns a negative number, such as: -61495771847000

Am I doing something wrong? if so, what is the best approach to convert a string date to millis with Joda in java?


Solution

  • -61495771847000 is Sun Apr 11 0021 09:09:13, which is the value you gave.

    If you want year 21 to be parsed as year 2021, then you should only specify a 2-digit year pattern, i.e. yy, not yyyy. If you do that, you get 1618132153000 as the result.