Emmanuelguther
Emmanuelguther

Reputation: 1059

Java Date format. String to date

I have a String that contains a date.

I'm trying to do so but it gives me an error.

java.text.ParseException: Unparseable date: "Fri Mar 20 20:44:49 CET 2015" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:626)

I'm trying this.

String fechaFestivo = c.getString(1);//Fri Mar 20 20:44:49 CET 2015

SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = format.parse(fechaFestivo);

Upvotes: 0

Views: 268

Answers (3)

nicopico
nicopico

Reputation: 3636

Your SimpleDateFormat is using the default locale, which may not be English. In that case, the abbreviated name of the day will not be recognized.

Make sure to use the correct locale with this constructor:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.US);

Upvotes: 3

Arun Kumar
Arun Kumar

Reputation: 194

Use "EEE MMM dd HH:mm:ss zzz yyyy"

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

Try to replace your SimpleDateFormat for DateFormat, something like that :

String fechaFestivo = c.getString(1); //Fri Mar 20 20:44:49 CET 2015

DateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date date = format.parse(fechaFestivo);

Upvotes: 1

Related Questions