robert trudel
robert trudel

Reputation: 5779

Parsing a timestamp containg the date, time & offset

I'm using using Java 5.

I need to parse date-time strings in ISO 8601 format such as 2011-11-30T12:00:00.000+00:00:

String dateString = "2011-11-30T12:00:00.000+00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
Date parsed=null;
try {
    parsed = df.parse(dateString);
}

I have also tried this pattern: yyyy-MM-dd'T'HH:mm:ss.SSSz, but get same result:

java.text.ParseException: Unparseable date: "2011-11-30T12:00:00.000+00:00"

Any ideas?

Upvotes: 3

Views: 3325

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79560

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API*.

Also, shown below is a notice on the Joda-Time Home Page:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using modern date-time API

Given below is the excerpt from OffsetDateTime#parse documentation:

Obtains an instance of OffsetDateTime from a text string such as 2007-12-03T10:15:30+01:00.

The string must represent a valid date-time and is parsed using DateTimeFormatter.ISO_OFFSET_DATE_TIME.

Since your text string, 2011-11-30T12:00:00.000+00:00 fully complies with the default format, you do not need to specify any DateTimeFormatter explicitly.

Demo:

import java.time.*;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2011-11-30T12:00:00.000+00:00");
        System.out.println(odt);
    }
}

Output:

2011-11-30T12:00Z

Online Demo

Note: If for some reason, you need an instance of java.util.Date, let java.time API do the heavy lifting of parsing the date-time string and convert odt from the above code into a java.util.Date instance using Date date = Date.from(odt.toInstant()).

Learn more about the modern Date-Time API from Trail: Date Time.


* If you are receiving an instance of java.util.Date, convert it tojava.time.Instant, using Date#toInstant and derive other date-time classes of java.time from it as per your requirement.

Upvotes: 2

Ruslan Ostafiichuk
Ruslan Ostafiichuk

Reputation: 4712

Joda-Time

You have to use Joda-Time (Maven) (supports Java 1.5) if you don't want to parse it manually. Just create an object with new DateTime(String) then you can get Date via toDate() method.

Time Zone

Pass the time zone you want assigned to the resulting date-time object. Unlike java.util.Date, a Joda-Time DateTime object knows its own assigned time zone (DateTimeZone). If omitted, the JVM’s current default time zone is assigned implicitly.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); // Or perhaps DateTimeZone.UTC
DateTime dateTime = new DateTime( "2011-11-30T12:00:00.000+00:00", zone );

Upvotes: 5

Priidu Neemre
Priidu Neemre

Reputation: 3071

The pattern you have currently used in your SimpleDateFormat constructor, yyyy-MM-dd'T'HH:mm:ss.SSSZ, is supposed to accept timestamps in the following format:

2011-11-30T12:00:00.000+0000

however, what you need is something that will handle timestamps such as:

2011-11-30T12:00:00.000+00:00

For this particular purpose, the yyyy-MM-dd'T'HH:mm:ss.SSSXXX format should be specified in your SimpleDateFormat (i.e. the problem lies within the formatting of the timestamp's timezone component).

Upvotes: 1

meskobalazs
meskobalazs

Reputation: 16041

You should remove the colon from +00:00, as this format only works with the X pattern, which is not available in Java 5, only from Java SE 7.

More information: RFC822 needs this style (without colon), in ISO 8601 both is correct.

Upvotes: 3

Related Questions