java简单日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6406470/
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-08-16 05:41:12  来源:igfitidea点击:

java SimpleDateFormat

javadate

提问by user775187

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

在 Java 中,如何解析包含不代表模式的字母的日期字符串?

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

采纳答案by Brad Mace

The time you're trying to parse appears to be in ISO 8601 format. SimpleDateFormatunfortunately 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.

您尝试解析的时间似乎是ISO 8601 格式SimpleDateFormat不幸的是,它不支持所有与 ISO 8601 相同的时区说明符。如果您希望能够正确处理 ISO 中指定的所有形式,最好的做法是使用Joda time

This example is straight out of the user guide:

这个例子直接来自用户指南

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

回答by Bala R

You can try

你可以试试

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

Reference : from Javadoc

参考:来自Javadoc

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

可以使用单引号 (') 引用文本以避免解释。

回答by gmhk

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 linkfor reference

您可以使用以下链接作为参考

回答by j2gl

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.

我不知道它对你是否仍然有用,但我现在遇到了同样的问题,过了一会儿我想出了这个。