0

Hi so I do understand there are many threads out here regarding this and ive been through many of them I'm not able to grasp the whole date format thing so here I am seeking your help :)

I've got a json object giving me this date "2014-01-10T02:01:42.657Z" and I have no idea what sort of format that is. I do know its a datetime from mssql database and I wish to parse this in java for which I'm using this code.

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date result = null;
        try {
            result = df.parse(last_active);
        } catch (ParseException e) {
            Log.i("Date Parser problem (Friend.java): ", e.toString());
            e.printStackTrace();
        }  
        Log.i("Date: ", result.toString());

I do understand that the "yyyy-MM-dd HH:mm:ss" is the wrong format to parse this date with but I am not able to find the right type of format string to format the following date.

"2014-01-10T02:01:42.657Z"

I appreciate your help :)

Thank you

6
  • 1
    I don't know what T and Z are supposed to mean here, but ust convert it to a format you know then parse. Commented Mar 23, 2014 at 16:45
  • That is the thing that is throwing me off as well, I've tried similar formats but the T and Z may be variable may not be its very weird. Commented Mar 23, 2014 at 16:46
  • 1
    It's the internationally standardized date format (ISO 8601). The T is the separator between date and time. The Z at the end indicates the UTC+0 timezone. Commented Mar 23, 2014 at 16:48
  • 2
    T is used to separate the date from the time, and Z is used to denote that the time is in UTC Commented Mar 23, 2014 at 16:48
  • 2
    This is an ISO 8601 format date. The T shows you where the date ends and the time starts. The Z tells you time timezone is UTC. Google for 'Parsing ISO 8601 dates in Java' for more info. Commented Mar 23, 2014 at 16:49

3 Answers 3

4

This is simply a standard ISO-formatted date. The T in the middle is simply a separator, and the Z at the end means "UTC".

To parse it, simply use yyyy-MM-dd'T'HH:mm:ss.SSSX as the pattern.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

2 Comments

The Z is relevant. It indicates that the date has been transformed to a String using the UTC timezone.
No need to parse that string in Joda-Time. Simply pass the string to a DateTime constructor. Joda-Time uses the ISO 8601 standard for its defaults. DateTime will automatically parse a valid ISO string.
1

ISO 8601

That string is in standard format, as defined by ISO 8601. In various protocols, this format is gradually replacing the silly formats of yesteryear such as Sun, 06 Nov 1994 08:49:37 GMT.

Avoid java.util.Date/Calendar

The bundled classes java.util.Date and .Calendar are notoriously troublesome. Avoid them. With the arrival of the java.time package in Java 8, they are practically deprecated. If you cannot go to Java 8, use Joda-Time (which inspired java.time).

Joda-Time

The Joda-Time library (third-party, open-source, free-of-cost) uses ISO 8601 for its defaults. So the Joda-Time class DateTime automatically parses such strings.

DateTime dateTime = new DateTime( "2014-01-10T02:01:42.657Z" );

Dump to console…

System.out.println( "dateTime: " + dateTime );

When run…

dateTime: 2014-01-09T18:01:42.657-08:00

Time Zone

Notice Joda-Time applied my JVM’s default time zone thereby adjusting the time appropriately. If you wish to keep the DateTime object in UTC, pass a DateTimeZone object in that constructor.

    DateTime dateTime = new DateTime( "2014-01-10T02:01:42.657Z", DateTimeZone.UTC );

When run…

dateTime: 2014-01-10T02:01:42.657Z

java.time

The java.time package also uses ISO 8601 for its defaults, and automatically parses such standard strings.

Instant instant = Instant.parse( "2014-01-10T02:01:42.657Z" );

Dump to console…

System.out.println( "instant: " + instant );

When run…

instant: 2014-01-10T02:01:42.657Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.