4

I am using Facebook graph API to retrieve user wall information. In that, I am getting the post created time value as:

created_time: "2012-04-19T09:00:02+0000"

Can anyone suggest me how to convert this time to UTC or epoch value in Java?

Venkat
  • 113
  • 3
  • 11

2 Answers2

10

The format of date you receive is ISO 8601.

As described in Converting ISO8601-compliant String to java.util.Date (using Joda-Time):

DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis();
String jtdate = "2012-04-19T09:00:02+0000";
System.out.println(parser.parseDateTime(jtdate));
Community
  • 1
  • 1
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
  • Thanks for your reply. When I use the above DatatypeConverter, I receive an exception: java.lang.IllegalArgumentException: 2012-04-19T09:00:02+0000. Its state illegal argument type. Can you please suggest what I am doing wrong? – Venkat Apr 19 '12 at 10:43
  • @Venkat, I've accidently provided wrong sample code. Corrected – Juicy Scripter Apr 19 '12 at 10:59
  • Thank you. After parsing it prints 2012-04-19T14:30:02.000+05:30. I need the values in UTC format. How can I get it? Please correct me if I am wrong. Thanks again. – Venkat Apr 19 '12 at 11:07
  • 1
    You may use `getMillis()` method on result of `parseDateTime()` to get the number of milliseconds from epoch. – Juicy Scripter Apr 19 '12 at 11:19
  • Thank you Juicy Scripter. You saved my time :) – Venkat Apr 20 '12 at 04:43
3

You basically need to parse the ISO8601 date format. There are libraries out there that would do it for you or you can create your own parser (relatively simply), e.g.

http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm

scibuff
  • 13,377
  • 2
  • 27
  • 30