将日期字符串解析为java.util.Date时,非法模式字符“T” [英] Illegal pattern character 'T' when parsing a date string to java.util.Date

查看:301
本文介绍了将日期字符串解析为java.util.Date时,非法模式字符“T”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串,我想解析为正常日期使用java Date API,以下是我的代码:

  public static void main(String [] args){
String date =2010-10-02T12:23:23Z;
String pattern =yyyy-MM-ddThh:mm:ssZ;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
Date d = sdf.parse(date);
System.out.println(d.getYear());
} catch(ParseException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

但是我收到了一个例外: java.lang.IllegalArgumentException:非法模式字符'T'



所以我想知道我是否必须拆分字符串并手动解析?



BTW,我已经尝试在T的任一侧添加单引号字符:

  String pattern =yyyy-MM-dd'T'hh:mm:ssZ; 

它也无效。

解决方案

这个输入与尾随的 Z 的输入一致,如下所示:




在模式中,$ code> T 在两边都以'进行转义。



结束时 Z 的模式实际上是 XXX 在JavaDoc for SimpleDateFormat 中,只是不太清楚
实际上如何使用它,因为 Z 是旧的
TimeZone 信息的标记。




Q2597083.java



  import java.text.SimpleDateFormat; 
import java.util.Calendar;
import java.util.Date
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Q2597083
{
/ **
*所有日期都按UTC标准化,它是将客户端代码转换为适当的TimeZone。
* /
public static final TimeZone UTC;

/ **
* @see< a href =http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations>组合日期和时间表示< / a> ;
* /
public static final String ISO_8601_24H_FULL_FORMAT =yyyy-MM-dd'T'HH:mm:ss.SSSXXX;

/ **
* 0001-01-01T00:00:00.000Z
* /
public static final日期BEGINNING_OF_TIME;

/ **
* 292278994-08-17T07:12:55.807Z
* /
public static final Date END_OF_TIME;

static
{
UTC = TimeZone.getTimeZone(UTC);
TimeZone.setDefault(UTC);
final Calendar c = new GregorianCalendar(UTC);
c.set(1,0,1,0,0,0);
c.set(Calendar.MILLISECOND,0);
BEGINNING_OF_TIME = c.getTime();
c.setTime(new Date(Long.MAX_VALUE));
END_OF_TIME = c.getTime();
}

public static void main(String [] args)throws异常
{

final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT);
sdf.setTimeZone(UTC);
System.out.println(sdf.format(BEGINNING_OF_TIME)=+ sdf.format(BEGINNING_OF_TIME));
System.out.println(sdf.format(END_OF_TIME)=+ sdf.format(END_OF_TIME));
System.out.println(sdf.format(new Date())=+ sdf.format(new Date()));
System.out.println(sdf.parse(\2015-04-28T14:23:38.521Z\)=+ sdf.parse(2015-04-28T14:23:38.521Z ));
System.out.println(sdf.parse(\0001-01-01T00:00:00.000Z \)=+ sdf.parse(0001-01-01T00:00:00.000Z ));
System.out.println(sdf.parse(\292278994-08-17T07:12:55.807Z\)=+ sdf.parse(292278994-08-17T07:12:55.807Z ));
}
}



生成以下输出:



  sdf.format(BEGINNING_OF_TIME)= 0001-01-01T00:00:00.000Z 
sdf.format(END_OF_TIME)= 292278994-08- 17T07:12:55.807Z
sdf.format(new Date())= 2015-04-28T14:38:25.956Z
sdf.parse(2015-04-28T14:23:38.521Z )= Tue Apr 28 14:23:38 UTC 2015
sdf.parse(0001-01-01T00:00:00.000Z)= Sat Jan 01 00:00:00 UTC 1
sdf。 parse(292278994-08-17T07:12:55.807Z)= Sun Aug 17 07:12:55 UTC 292278994


I have a date string and I want to parse it to normal date use the java Date API,the following is my code:

public static void main(String[] args) {
    String date="2010-10-02T12:23:23Z";
    String pattern="yyyy-MM-ddThh:mm:ssZ";
    SimpleDateFormat sdf=new SimpleDateFormat(pattern);
    try {
        Date d=sdf.parse(date);
        System.out.println(d.getYear());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

However I got an exception: java.lang.IllegalArgumentException: Illegal pattern character 'T'

So I wonder if I have to split the string and parse it manually?

BTW, I have tried to add a single quote character on either side of the T:

String pattern="yyyy-MM-dd'T'hh:mm:ssZ";

It also does not work.

解决方案

This works with the input with the trailing Z as demonstrated:

In the pattern the T is escaped with ' on either side.

The pattern for the Z at the end is actually XXX as documented in the JavaDoc for SimpleDateFormat, it is just not very clear on actually how to use it since Z is the marker for the old TimeZone information as well.

Q2597083.java

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Q2597083
{
    /**
     * All Dates are normalized to UTC, it is up the client code to convert to the appropriate TimeZone.
     */
    public static final TimeZone UTC;

    /**
     * @see <a href="http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations">Combined Date and Time Representations</a>
     */
    public static final String ISO_8601_24H_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    /**
     * 0001-01-01T00:00:00.000Z
     */
    public static final Date BEGINNING_OF_TIME;

    /**
     * 292278994-08-17T07:12:55.807Z
     */
    public static final Date END_OF_TIME;

    static
    {
        UTC = TimeZone.getTimeZone("UTC");
        TimeZone.setDefault(UTC);
        final Calendar c = new GregorianCalendar(UTC);
        c.set(1, 0, 1, 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        BEGINNING_OF_TIME = c.getTime();
        c.setTime(new Date(Long.MAX_VALUE));
        END_OF_TIME = c.getTime();
    }

    public static void main(String[] args) throws Exception
    {

        final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT);
        sdf.setTimeZone(UTC);
        System.out.println("sdf.format(BEGINNING_OF_TIME) = " + sdf.format(BEGINNING_OF_TIME));
        System.out.println("sdf.format(END_OF_TIME) = " + sdf.format(END_OF_TIME));
        System.out.println("sdf.format(new Date()) = " + sdf.format(new Date()));
        System.out.println("sdf.parse(\"2015-04-28T14:23:38.521Z\") = " + sdf.parse("2015-04-28T14:23:38.521Z"));
        System.out.println("sdf.parse(\"0001-01-01T00:00:00.000Z\") = " + sdf.parse("0001-01-01T00:00:00.000Z"));
        System.out.println("sdf.parse(\"292278994-08-17T07:12:55.807Z\") = " + sdf.parse("292278994-08-17T07:12:55.807Z"));
    }
}

Produces the following output:

sdf.format(BEGINNING_OF_TIME) = 0001-01-01T00:00:00.000Z
sdf.format(END_OF_TIME) = 292278994-08-17T07:12:55.807Z
sdf.format(new Date()) = 2015-04-28T14:38:25.956Z
sdf.parse("2015-04-28T14:23:38.521Z") = Tue Apr 28 14:23:38 UTC 2015
sdf.parse("0001-01-01T00:00:00.000Z") = Sat Jan 01 00:00:00 UTC 1
sdf.parse("292278994-08-17T07:12:55.807Z") = Sun Aug 17 07:12:55 UTC 292278994

这篇关于将日期字符串解析为java.util.Date时,非法模式字符“T”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆