java 迄今为止的字符串解析:非法模式字符“T”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26398657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:52:08  来源:igfitidea点击:

Parsing string to date: Illegal pattern character 'T'.

javastringdatesimpledateformat

提问by mbezunartea

I need to parse a string to date in java. My string has the following format:

我需要在java中解析一个字符串到日期。我的字符串具有以下格式:

2014-09-17T12:00:44.0000000Z

but java throws the following exception when trying to parse such format... java.lang.IllegalArgumentException: Illegal pattern character 'T'.

但Java试图解析这种格式时抛出以下异常... java.lang.IllegalArgumentException: Illegal pattern character 'T'

Any ideas on how to parse that?

关于如何解析它的任何想法?

Thank you!

谢谢!

回答by Meno Hochschild

Given your input of 2014-09-17T12:00:44.0000000Z, it is not sufficient to escape the letter Tonly. You also have to handle the trailing Z. But be aware, this Zis NOT a literal, but has the meaning of UTC+00:00timezone offset according to ISO-8601-standard. So escaping Zis NOT correct.

鉴于您输入的2014-09-17T12:00:44.0000000ZT仅对字母进行转义是不够的。您还必须处理尾随Z. 但请注意,这Z不是文字,而是UTC+00:00根据ISO-8601-standard. 所以逃避Z是不正确的。

SimpleDateFormathandles this special char Zby pattern symbol X. So the final solution looks like:

SimpleDateFormatZ通过模式符号处理这个特殊字符X。所以最终的解决方案看起来像:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX");
 Date d = sdf.parse("2014-09-17T12:00:44.0000000Z");
 System.out.println(d); // output: Wed Sep 17 14:00:44 CEST 2014

Note that the different clock time is right for timezone CEST(toString()uses system timezone), and that the result is equivalent to UTC-time 12:00:44. Furthermore, I had to insert seven symbols S in order to correctly process your input which pretends to have precision down to 100ns(although Java pre 8 can only process milliseconds).

请注意,不同的时钟时间适用于时区CESTtoString()使用系统时区),并且结果等效于UTC-time 12:00:44. 此外,我必须插入七个符号 S 才能正确处理假装精度低至的输入100ns(尽管 Java pre 8 只能处理毫秒)。

回答by Xavier Delamotte

You have to escape the 'T' character:

您必须转义“T”字符:

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date parse = format.parse("2014-09-17T12:00:44.0000000Z");

Using Answer to: What is this date format? 2011-08-12T20:17:46.384Z

使用 Answer to:这是什么日期格式?2011-08-12T20:17:46.384Z

回答by venkatesh.khandare

Try this.

试试这个。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateClass {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date d = sdf.parse("2014-09-17T12:00:44.0000000Z");
        System.out.println(d); //output Wed Sep 17 12:00:44 IST 2014
    }
}