3

I have a problem while trying to parse the expiration date of the facebook access token with the joda time library. This is what my method looks like:

 public static DateTime parseDate(String date_string){
DateTime dateTime = DateTime.parse(date_string, DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
return dateTime;
}

I'm calling the method like this:

DateTime fb_token_expire_date;
            fb_token_expire_date = parseDate(fb_token_expire_date_str);

the fb_token_expire_date_str looks like this in my case: "Tue Jan 06 14:15:21 OEZ 2015"

But when i'm trying to run the programm i get this error:

 Caused by: java.lang.IllegalArgumentException: Invalid format: "Tue Jan 06 14:15:21 OEZ 2015"
            at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
            at org.joda.time.DateTime.parse(DateTime.java:160)

i have the feeling that it has something to do with the timezone, but i don't know how to fix it. I hope you can help me.

user3780814
  • 147
  • 1
  • 2
  • 10
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 01 '18 at 00:09

3 Answers3

1

Yes, you're guessing right. Time zone names cannot be parsed. Mainly is because "z" or in your case "zzz" are non an international standard.

Check out: This question

EDIT

I've tested a pattern without the infamous "zzz" and trimmiming the timezone from the string and all went ok:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;

public class parser {

    public static void main(String args[]) throws ClassNotFoundException {

        String originaldate = new String("Tue Jan 06 14:15:21 OEZ 2015");
        String trimmeddate = originaldate.substring(0,19);
        String trimmedyear = originaldate.substring(23);
        String trimmed = trimmeddate+trimmedyear;

        DateTimeParser[] parsers = {
                DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss yyyy").getParser() };
        DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(
                null, parsers).toFormatter();

        DateTime date1 = formatter.parseDateTime(trimmed);

        System.out.println(date1);


    }
}
Community
  • 1
  • 1
Martin Revert
  • 3,242
  • 2
  • 30
  • 33
  • You beat me to it :) At least I got a reference to joda.org in there. – mattias Nov 07 '14 at 17:35
  • and how can i parse it without the timezone? or is there any other way to parse it? – user3780814 Nov 07 '14 at 17:37
  • I edited my reply, there's a [thread](http://stackoverflow.com/questions/4498274/why-joda-datetimeformatter-cannot-parse-timezone-names-z) already covering this issue. – mattias Nov 07 '14 at 17:40
  • 1
    That's the same link I provided. Beat you again. :) – Martin Revert Nov 07 '14 at 17:42
  • @user3780814 I've think that you must to preformat that date because it is not an standard to manange in Java. You must to provide an ISO format from that string, but you will need to create a function for that. – Martin Revert Nov 07 '14 at 17:45
  • As @MartinRevert says, adding on here, see the [ISO8601 profile](http://www.w3.org/TR/NOTE-datetime) for how it _should_ look. If you always get fb_token_expire_date_str in the same format, it should be a straight forward task to accomplish. – mattias Nov 07 '14 at 17:50
  • @user3780814 Could you try your very same code but eliminating "zzz" from DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy") ? – Martin Revert Nov 07 '14 at 17:54
  • @user3780814 See my edit, I played with a formatter and definitely you can parse that date if you get rid of the timezone in the string. – Martin Revert Nov 07 '14 at 18:24
  • it wasn't working without the zzz either. i solved my problem differently as i will describe in a new answer – user3780814 Nov 08 '14 at 21:25
  • Well, as I showed to you, it works ok without the zzz. You just need to trim it and parse works. – Martin Revert Nov 08 '14 at 21:27
0

It seems you are trying to parse the zone name. According to joda.org, the time zone names cannot be parsed.

See for example this thread for more information regarding your issue.

Community
  • 1
  • 1
mattias
  • 2,079
  • 3
  • 20
  • 27
  • yeah there are information covering this issue, but i still have no idea how i can parse the string i get from the facebook token into a valid date or datetime or whatever to check if it is still valid?! – user3780814 Nov 07 '14 at 17:43
0

i solved the problem now by saving the date in the preferences as a long and then creating a new date with that long number.

this isn't solving the problem with parsing this date format, but was a solution in my case.

user3780814
  • 147
  • 1
  • 2
  • 10