开发者

java SimpleDateFormat

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

"2007-11-02开发者_运维问答T14: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)


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.


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


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.


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


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.


Below format code works for me !

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

where tradeDate = 20220722

java SimpleDateFormat

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜