Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to parse date String to Date

Tags:

java

date

time

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.

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

Thank you.

like image 364
user3190987 Avatar asked Jan 13 '14 16:01

user3190987


People also ask

How do you parse a string to a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How do you format a date in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.


1 Answers

In German, "Central European Time" is "Mitteleuropäische Zeit", so if you want to use Locale.GERMAN, change CET to MEZ and it works.

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()
like image 52
tobias_k Avatar answered Oct 01 '22 11:10

tobias_k