Avidev9
Avidev9

Reputation: 601

String to Date in Java

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?

Upvotes: 2

Views: 633

Answers (6)

assylias
assylias

Reputation: 328873

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);
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503469

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...)

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

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.

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15664

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

Upvotes: 1

Burkhard
Burkhard

Reputation: 14738

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

Upvotes: 0

thedan
thedan

Reputation: 1240

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

Upvotes: 2

Related Questions