13

in Java, how to parse a date string that contains a letter that does not represent a pattern?

"2007-11-02T14:46:03+01:00"
String date ="2007-11-02T14:46:03+01:00";
String format = "yyyy-MM-ddTHH:mm:ssz";
new SimpleDateFormat(format).parse(date);

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:769)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:576)
    at java.text.SimpleDateFormat.(SimpleDateFormat.java:501)
    at java.text.SimpleDateFormat.(SimpleDateFormat.java:476)
3
  • 1
    Note that the question about parsing ISO 8601 dates is a frequently asked and answered question. Search for "Java ISO 8601 date" or something similar and you'll find many answers. Commented Jun 20, 2011 at 6:53
  • For example: stackoverflow.com/questions/6279647/… Commented Jun 20, 2011 at 6:55
  • 1
    For anyone reading this question today or tomorrow I recommend you don’t use SimpleDateFormat. That class is notoriously troublesome and long outdated. Instead just use OffsetDateTime from java.time, the modern Java date and time API. Commented Feb 16, 2021 at 21:21

7 Answers 7

21

You can try

String format = "yyyy-MM-dd'T'HH:mm:ssz";

Reference : from Javadoc

Text can be quoted using single quotes (') to avoid interpretation.

Sign up to request clarification or add additional context in comments.

2 Comments

It works only if the string contains 'GMT'. Just like this : String date ="2007-11-02T14:46:03GMT+01:00";
@Hendra that is true or it has to be String date ="2007-11-02T14:46:03+0100"; SimpleDateTime is is pretty limited in such situations an I guess if you(OP) don't have much control over the input, using libraries such as joda time would work out best.
5

The time you're trying to parse appears to be in ISO 8601 format. SimpleDateFormat unfortunately doesn't support all the same timezone specifiers as ISO 8601. If you want to be able to properly handle all the forms specified in the ISO, the best thing to do is use Joda time.

This example is straight out of the user guide:

DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00");

3 Comments

this doesn't work, +01:00 needs to be +0100 for joda to work.
@user - you're right, the ISO version doesn't like colons. Good news is you can pass the version with colons straight to the DateTime constructor.
@user775187 You are wrong. new DateTime("2004-12-13T21:39:45.618-08:00") (with the colon) works. Tested with Joda-Time 2.12.2, but I believe it has worked in all versions. Joda-Time was later succeeded by java.time, though, so use that now.
1

No formatter needed

It’s time to post the modern answer, the answer that uses java.time, the modern Java date and time API. Your format is ISO 8601, and the classes of java.time generally parse the most common ISO 8601 variants as their default, that is, without any explicit formatter.

    String date ="2007-11-02T14:46:03+01:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(date);
    System.out.println(dateTime);

Output is:

2007-11-02T14:46:03+01:00

Yes, java.time also gives ISO 8601 format back from the toString methods, implicitly called when we print an object.

Enclose literal letters in single quotes

To answer the question as asked, you may enclose letters in single quotes to make DateTimeFormatter take them as literal letters rather than format specifiers. There would be no point whatsoever in doing the following in real code, but for the sake of demonstration:

    DateTimeFormatter isoFormatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX");
    String date ="2007-11-02T14:46:03+01:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(date, isoFormatter);

The result is the same as before.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Comments

0
String testDate = "2007-11-02T14:46:03+01:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
Date date = formatter.parse(testDate);
System.out.println(date);

You can try similar to the above

You can use following link for reference

Comments

0

If you don't care about the time zone, you can use this method.

  public static Date convertToDate(String strDate) throws ParseException {
    Date date = null;
    if (strDate != null) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      date = sdf.parse(strDate);
    }
    return date;
  }

I don't know if it's still useful for you, but I encounter with the same problem now, and after a little I come up with this.

1 Comment

You need to care about the UTC offset or you will get incorrect results (more often than not).
0

Below format code works for me !

But the code converts the date : 20220722 to date : 22-July-2022

where tradeDate = 20220722

enter image description here

2 Comments

Thanks for wanting to contribute. One, this is not what was asked. The question was about a datetime string having a T in it. Two, no one should use the old and very troublesome SimpleDateFormat class nor its friend the Date class. java.time, the modern Java date and time API, parses both your string and the one from the question without the programmer needing to fiddle with a format pattern string. See my answer.
please read and consider the answer to this question: Why should I not upload images of code/data/errors? on Meta Stack Overflow
-1
private static String dateTimeGenerate() {
    Format formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    Date date = new Date(System.currentTimeMillis());
   return formatter.format(date);
}


String reportFilePath = System.getProperty("user.dir") + "/test- 
output/Automation/" + dateTimeGenerate() + "_extentReport.html";

2 Comments

What is this solution adding to the other answers? Anyway, the (very) old classes like Date and SimpleDateFormat should not be used anymore, these were replaced by classes from the java.time package (10+ years ago, Java 8)
Thanks for wanting to contribute. The question was about parsing, not formatting. And as @user85421 says, no one should use SimpleDateFormat any more since it was so troublesome and is long outdated. Finally the question was about a format with colon in the offset, +01:00, which your formatter neither produces nor accepts. I got 2025-01-03T10:21:27CET, And trying to parse the string from the question I got java.text.ParseException: Unparseable date: "2007-11-02T14:46:03+01:00".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.