2

I have a date string like this "2010-12-10T20:03:53-06:00" I want to convert the same into equivalent date object in Java. Any Idea how to do this?

Avidev9
  • 601
  • 1
  • 6
  • 15

6 Answers6

2

What you are looking for is SimpleDateFormat.parse(). It will convert a string into a Date object.

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

thedan
  • 1,230
  • 2
  • 9
  • 13
1

You should use DateFormat class for this:

First you need to get rid of that : in the timezone part and make your date string like this

2010-12-10T20:03:53-0600

and use the code snippet below:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");    
Date d = formatter.parse("2010-12-10T20:03:53-0600");

Note: I checked this on Java 6 and Mr. Skeet has mentioned a better answer dealing with Java 7 as I don't know more about Java 7

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

If you're using Java 7, you should be okay without any string massaging, using the new X specifier for the UTC offset:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
Date date = format.parse(text);

(Testing to make sure - when I've installed JDK 7 myself :)

In general I would strongly recommend using Joda Time for date handling, however. Its Z specifier can handle an offset with a colon:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ")
                                            .withLocale(Locale.US);
DateTime dateTime = formatter.parseDateTime(text);

In fact, there's an ISODateTimeFormat class to make this even simpler:

DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();

Joda Time is a significantly better date/time API than the built-in one. (It's far from perfect, but it's a lot better than Date and Calendar...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • well will need another help here ...how to convert date object back to this format? – Avidev9 Nov 20 '12 at 17:21
  • @Avidev9: Call `format` instead of `parse`. That will pick up the time zone from the `SimpleDateTime` though, so you should consider what you set that to. – Jon Skeet Nov 20 '12 at 17:29
0

Use Joda time. It is powerful and easy to use.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
0

You can't parse a date with a colon in the time zone with the standard JDK Date until Java 7. Before Java 7 timezone would have to be either a full time zone with name or in the form -0600.

You have 3 options:

Here is an example with the second option:

public static void main(String[] args) throws ParseException {
    String input = "2010-12-10T20:03:53-06:00";
    int colon = input.lastIndexOf(":");
    input = input.substring(0, colon) + input.substring(colon + 1, input.length());
    System.out.println(input);
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date date = fmt.parse(input);
    System.out.println("date = " + date);
}
assylias
  • 321,522
  • 82
  • 660
  • 783
0

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

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 java.time, the modern Date-Time API:

The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.

Demo:

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2010-12-10T20:03:53-06:00");
        System.out.println(odt);
    }
}

Output:

2010-12-10T20:03:53-06:00

ONLINE DEMO

For any reason, if you need to convert this object of OffsetDateTime to an object of java.util.Date, you can do so as follows:

Date date = Date.from(odt.toInstant());

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


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110