简体   繁体   English

无法将日期字符串解析为日期

[英]Unable to parse date String to Date

im want to parse a date String to a Date. 我想将日期字符串解析为日期。 I was looking in some other questions, but I didn't find an answer. 我正在寻找其他一些问题,但我找不到答案。

String mail_delivered = "31.10.2013 17:57:58 CET";

try {
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss z", Locale.GERMAN);
    Date result =  df.parse(mail_delivered);  
    System.out.println(result);
} catch (ParseException pe) {
    pe.printStackTrace();
}

The error message is java.text.ParseException: Unparseable date: "[31.10.2013 17:57:58 CET]" and I don't know what's wrong. 错误消息是java.text.ParseException: Unparseable date: "[31.10.2013 17:57:58 CET]"我不知道什么是错的。

Can you help me please. 你能帮我吗。 Thanks in advance. 提前致谢。


Edit: ok. 编辑:好的。 i canged it to english, but i'have still the same problem. 我把它变成英语,但我仍然是同样的问题。 I wouldn't like to change the input, because it comes from a mail database. 我不想改变输入,因为它来自邮件数据库。 Any other ideas? 还有其他想法吗?

String mail_delivered = "31.10.2013 17:57:58 CET";

try {
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss z", Locale.ENGLISH);
    Date result =  df.parse(mail_delivered);  
    System.out.println(result);
} catch (ParseException pe) {
    pe.printStackTrace();
}

I found the problem. 我发现了这个问题。 I was blind. 我是瞎子。 The Sting from the Database was [31.10.2013 17:57:58 CET] , not 31.10.2013 17:57:58 CET 来自数据库的刺痛是[31.10.2013 17:57:58 CET] ,而不是31.10.2013 17:57:58 CET

Thank you. 谢谢。

In German, "Central European Time" is "Mitteleuropäische Zeit", so if you want to use Locale.GERMAN , change CET to MEZ and it works. 在德语中,“中欧时间”是“MitteleuropäischeZeit”,因此如果您想使用Locale.GERMANLocale.GERMAN CET更改为MEZ并且它可以正常工作。

String mail_delivered = "31.10.2013 17:57:58 MEZ";

For a list of all the legal time zone strings for a given locale, use this: 有关给定语言环境的所有合法时区字符串的列表,请使用以下命令:

DateFormatSymbols.getInstance(Locale.GERMAN).getZoneStrings()

Try This: 试试这个:

public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
       //Middle European Time (MET or MEZ-German)
        String mail_delivered = "31.10.2013 17:57:58 MET";
        try {
            SimpleDateFormat df= new SimpleDateFormat("dd.MM.yyyy HH:mm:ss z",Locale.GERMAN);
            Date result =  df.parse(mail_delivered);  
            System.out.println(result);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }

Output :- Thu Oct 31 22:27:58 IST 2013 输出: - 2013年10月31日星期二22:27:58

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

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