Java 如何将日期字符串解析为日期?

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

How to parse date string to Date?

javadateformat

提问by Meow

How do I parse the date string below into a Dateobject?

如何将下面的日期字符串解析为Date对象?

String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result =  df.parse(target);  

Throws exception...

抛出异常...

java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
        at java.text.DateFormat.parse(DateFormat.java:337)

采纳答案by BalusC

The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE. You have a 3-letter month abbreviation, so it must be MMM. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormatlocale to English as well, otherwise it will use the platform default locale which may not be English per se.

图案不对。你有一个 3 个字母的日缩写,所以它必须是EEE。你有一个 3 个字母的月份缩写,所以它必须是MMM. 由于那些日期和月份的缩写对语言环境敏感,您还希望将SimpleDateFormat语言环境明确指定为英语,否则它将使用平台默认语言环境,而这本身可能不是英语。

public static void main(String[] args) throws Exception {
    String target = "Thu Sep 28 20:29:30 JST 2000";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date result =  df.parse(target);  
    System.out.println(result);
}

This prints here

这打印在这里

Thu Sep 28 07:29:30 BOT 2000

which is correct as per my timezone.

根据我的时区,这是正确的。

I would also reconsider if you wouldn't rather like to use HHinstead of kk. Read the javadocfor details about valid patterns.

如果您不想使用HH代替kk. 有关有效模式的详细信息,请阅读javadoc

回答by miku

Here is a working example:

这是一个工作示例:

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

public class j4496359 {
    public static void main(String[] args) {
        try {
            String target = "Thu Sep 28 20:29:30 JST 2000";
            DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
            Date result =  df.parse(target);
            System.out.println(result); 
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
}

Will print:

将打印:

Thu Sep 28 13:29:30 CEST 2000

回答by BalusC

new SimpleDateFormat("EEE MMM dd kk:mm:ss ZZZ yyyy");

and

new SimpleDateFormat("EEE MMM dd kk:mm:ss Z yyyy");

still runs. However, if your code throws an exception it is because your tool or jdk or any other reason. Because I got same errorin my IDE but please check these http://ideone.com/Y2cRr(online ide) with ZZZand with Z

仍在运行。但是,如果您的代码抛出异常,那是因为您的工具或 jdk 或任何其他原因。因为在 IDE 中遇到了同样的错误,但请使用ZZZZ检查这些http://ideone.com/Y2cRr(在线 ide)

output is : Thu Sep 28 11:29:30 GMT 2000

输出是: Thu Sep 28 11:29:30 GMT 2000

回答by dennis

I had this issue, and I set the Localeto US, then it work.

我遇到了这个问题,我将 设置LocaleUS,然后它就可以工作了。

static DateFormat visitTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);

for String"Sun Jul 08 00:06:30 UTC 2012"

为了 String"Sun Jul 08 00:06:30 UTC 2012"

回答by Fernando Carvalho

String target = "27-09-1991 20:29:30";
DateFormat df = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
Date result =  df.parse(target);
System.out.println(result); 

This works fine?

这工作正常吗?

回答by nckbrz

A parse exception is a checked exception, so you must catch it with a try-catch when working with parsing Strings to Dates, as @miku suggested...

解析异常是已检查异常,因此在将字符串解析为日期时,您必须使用 try-catch 捕获它,正如@miku 建议的那样...