Haris
Haris

Reputation: 4230

How parse 2013-03-13T20:59:31+0000 date string to Date

How to parse this format date string 2013-03-13T20:59:31+0000 to Date object?

I'm trying on this way but it doesn't work.

DateFormat df = new SimpleDateFormat("YYYY-MM-DDThh:mm:ssTZD");
Date result =  df.parse(time);
                    

I get this exception from the first line:

java.lang.IllegalArgumentException: Illegal pattern character 'T'

Upvotes: 37

Views: 167237

Answers (7)

java.time

ZonedDateTime.parse("2020-05-08T11:12:13+0001", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"))

I am using java.time, the modern Java date and time API, and its ZonedDateTime class. A ZonedDateTime is a date and time with a time zone. If you want to use java.time before Android Oreo (API level 26), you need core library desugaring.

I am enclosing T in single quotes so the formatter knows that it is not a pattern letter. The T without quotes in the question was the reason for your exception, but there were other problems in the format patterns string too. yyyy-MM-dd'T'HH:mm:ssZ works.

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79395

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

Solution using java.time, the modern Date-Time API: Your input string has timezone offset and therefore it should be parsed to OffsetDateTime.

Demo:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxx", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("2013-03-13T20:59:31+0000", dtf);
        System.out.println(odt);
    }
}

Output:

2013-03-13T20:59:31Z

ONLINE DEMO

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Learn more about the modern Date-Time API from Trail: Date Time. Check this answer and this answer to learn how to use java.time API with JDBC.

If at all you need java.util.Date:

You can use Date#from to get an instance of java.util.Date, as shown below:

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

* 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. Note that Android 8.0 Oreo already provides support for java.time.

Upvotes: 8

Youness HARDI
Youness HARDI

Reputation: 532

it can help also

LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));

Upvotes: 2

SANAT
SANAT

Reputation: 9267

For 2017-02-08 06:23:35 +0000 this kind of Date format, i use below format:

SimpleDateFormat formatDate;
formatDate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZ");

Its working for me. Other answers not working for me.

Upvotes: 4

msam
msam

Reputation: 4287

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");

Year is lower case y. Any characters that are in the input which are not related to the date (like the 'T' in 2013-03-13T20:59:31+0000 should be quoted in ''.

For a list of the defined pattern letters see the documentation

Parse checks that the given date is in the format you specified. To print the date in a specific format after checking see below:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
Date result;
try {
    result = df.parse("2013-03-13T20:59:31+0000");
    System.out.println("date:"+result); //prints date in current locale
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(sdf.format(result)); //prints date in the format sdf
}

Upvotes: 47

Nermeen
Nermeen

Reputation: 15973

Try:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

check http://developer.android.com/reference/java/text/SimpleDateFormat.html

in specific:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
           yyyy-MM-dd HH:mm 1969-12-31 16:00
           yyyy-MM-dd HH:mm 1970-01-01 00:00
          yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
          yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
   yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
   yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

Upvotes: 60

nano_nano
nano_nano

Reputation: 12523

Please try this:

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

Upvotes: 3

Related Questions