user3053234
user3053234

Reputation: 385

Parsing date in from string in java

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

Upvotes: 0

Views: 1943

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338564

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

Upvotes: 1

Arjit
Arjit

Reputation: 3456

Try this

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

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

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.

Upvotes: 4

Related Questions