32

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'

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Haris
  • 4,130
  • 3
  • 30
  • 47
  • 1
    For new readers to this question I recommend you don’t use `DateFormat`, `SimpleDateFormat` and `Date`. Those classes are notoriously troublesome and long outdated. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See [the good answer by Arvind Kumar Avinash](https://stackoverflow.com/a/69715173/5772882). – Ole V.V. Oct 26 '21 at 06:30

7 Answers7

56

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
Mike
  • 14,010
  • 29
  • 101
  • 161
Nermeen
  • 15,883
  • 5
  • 59
  • 72
47

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
}
msam
  • 4,259
  • 3
  • 19
  • 32
  • @HarisDautović are you using the correct ' to surround the T ? this is Alt+39 not some other apostrophe like ` – msam Mar 15 '13 at 13:59
  • 1
    Date.toString() uses the local time zone, println is using toString() so you get the local time zone. – msam Mar 15 '13 at 14:41
  • @msam, in your answer you mentioned «*Any characters that are in the input which are not related to the date should be quoted in `''`*». But at the same time `Z` in your initial example was not surrounded by «`''`», is it OK? I just added `'Z'` and everything is working now. – Mike Feb 11 '16 at 15:04
  • @MikeB. Z is part of the Date - it's the Timezone in the +0000 format. 'Z' would mean that the string to parse has a "Z" character – msam Feb 11 '16 at 16:36
  • @msam, It's strange, in my case `DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); LocalDateTime maxDate = pubDates.stream().map(s -> LocalDateTime.parse(s, formatter)).max(LocalDateTime::compareTo).get();` works only if I surround `Z` with `''`. – Mike Feb 11 '16 at 20:16
  • @MikeB. are you sure the string that is being parsed is in the specified format? `DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH); LocalDateTime ldt = LocalDateTime.parse("2013-03-13T20:59:31+0000",formatter);` works - surrounding Z with '' throws an exception. – msam Feb 12 '16 at 09:23
  • In my case the date is in this format: `2016-02-12T09:19:00Z`. – Mike Feb 12 '16 at 10:31
  • @MikeB. If you have a 'Z' character then it must quoted as you said. Unquoted Z refers to the time zone (+ or - followed by 4 digits) – msam Feb 13 '16 at 18:07
  • for me removein tick from z helped 'Z' – silentsudo Nov 30 '18 at 15:05
8

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    About time (read: overdue) this frequently viewed question got a current (non-outdated) answer! Thanks very much! – Ole V.V. Oct 26 '21 at 06:27
4

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.

SANAT
  • 8,489
  • 55
  • 66
3

Please try this:

SimpleDateFormat formatDate;
formatDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
nano_nano
  • 12,351
  • 8
  • 55
  • 83
3

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 4
    Welcome to SO. Code -only answers are discouraged. It's helpful to describe how this answer addresses the 8-year-old question that's different from the other existing answers. – jwvh Feb 19 '21 at 21:01
  • Will you explain more about the details regarding the answer. Make it more meaning full and clear. – Raksha Saini Feb 20 '21 at 02:18
2

it can help also

LocalDateTime dateTime = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
Youness HARDI
  • 402
  • 1
  • 3
  • 13