简体   繁体   English

解析日期字符串时出错

[英]Error parsing date string

I need to parse this string as a date:我需要将此字符串解析为日期:

Mon Jun 10 00:00:00 CEST 2013 2013 年 6 月 10 日星期一 00:00:00 CEST

Here is what I do:这是我所做的:

SimpleDateFormat sdf = new SimpleDateFormat("ccc MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(dateString);

But I get a ParseException :但我得到一个ParseException

Unparseable date: "Wed Oct 02 00:00:00 CEST 2013" (at offset 0)

Any help please?有什么帮助吗?

As others have said, you need EEE instead of ccc - but you should also specify a locale, so that it doesn't try to parse the month and day names (and other things) using your system default locale: 正如其他人所说,您需要EEE而不是ccc但您应该指定一个语言环境,这样它就不会尝试使用系统默认语言环境来解析月份和日期名称(以及其他内容):

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",
                                            Locale.US);

Your format is wrong. 您的格式错误。 You need to use EEE instead of ccc , where E means Day name in week . 您需要使用EEE而不是ccc ,其中E表示Day name in week

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

Have a look at the docs regarding all the valid patterns available for SimpleDateFormat . 看看有关SimpleDateFormat可用的所有有效模式的文档

Replace ccc with EEE in the pattern to specify the day of the week: 在模式中将ccc替换为EEE以指定星期几:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

Example: https://gist.github.com/kmb385/8781482 例如: https //gist.github.com/kmb385/8781482

更新格式如下:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

It is a Locale problem. 这是一个Locale问题。 That's because dates are represented differently between Locale s, so the JVM fires an exception if the Date is not in the correct format. 这是因为日期在Locale之间的表示方式不同,因此,如果Date的格式不正确,则JVM将触发异常。 You can solve it by setting a custom Locale: 您可以通过设置自定义区域设置来解决此问题:

String str = "Mon Jun 10 00:00:00 EST 2013";
Locale.setDefault(Locale.US);
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(str);
System.out.println(date);

IDEone examples do work because the default locale is Locale.US IDEone示例可以工作,因为默认语言环境是Locale.US

java.time java.时间

The accepted answer uses SimpleDateFormat which was the correct thing to do in Feb 2014. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern Date-Time API .接受的答案使用SimpleDateFormat ,这是 2014 年 2 月的正确做法。2014 年 3 月, java.util日期时间 API 及其格式 API, SimpleDateFormat现代日期时间 API 取代 Since then, it is highly recommended to stop using the legacy date-time API.从那时起,强烈建议停止使用旧版日期时间 API。

Note : Never use date-time formatting/parsing API without a Locale .注意切勿在没有Locale的情况下使用日期时间格式/解析 API

Solution using the modern date-time API :使用现代日期时间的解决方案 API

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String stdDateTime = "Mon Jun 10 00:00:00 CEST 2013";
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(stdDateTime, parser);
        System.out.println(zdt);
    }
}

Output : Output :

2013-06-10T00:00+02:00[Europe/Paris]

If for any reason, you need an instance of java.util.Date , you can get it as follow:如果出于任何原因,您需要一个java.util.Date的实例,您可以按以下方式获取它:

Date date = Date.from(zdt.toInstant());

ONLINE DEMO在线演示

Learn more about the modern Date-Time API from Trail: Date Time .Trail:Date Time中了解有关现代日期时间 API 的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM