2

how to parse this date ?

Mon Jul 29 02:00:00 CEST 2013

the code:

    public class HelloWorld{

     public static void main(String []args){

        try {
                String startdateString = "Mon Jul 29 02:00:00 CEST 2013";
        SimpleDateFormat fromUser2 = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy");

            startdateString = myFormat.format(fromUser2.parse(startdateString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
     }
}

i am getting following exception

java.text.ParseException: Unparseable date: "Mon Jul 29 02:00:00 CEST 2013"

16:13:57,761 ERROR [stderr] (http-/0.0.0.0:8080-1)  at java.text.DateFormat.parse(Unknown Source)
dev2d
  • 4,245
  • 3
  • 31
  • 54
FrankTan
  • 1,626
  • 6
  • 28
  • 63

3 Answers3

3

You don't provide any Locale and the code won't work if you don't run it on an english locale (as US or UK).

The following code (corrected) work for me:

try {
    String startdateString = "Mon Jul 29 02:00:00 CEST 2013";
    SimpleDateFormat fromUser2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

    System.out.println(fromUser2.parse(startdateString));
} catch (ParseException e) {
    e.printStackTrace();
}
LaurentG
  • 11,128
  • 9
  • 51
  • 66
2

You need to format your date using SimpleDateFormat's object.

Replace

startdateString = myFormat.format(fromUser2.parse(startdateString));

By

startdateString = fromUser2.format(fromUser2.parse(startdateString));
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
1

i could not find this answer on google but ok is alread here :

Java - Unparseable date

try {
String startdateString = "Mon Jul 29 02:00:00 CEST 2013";
SimpleDateFormat fromUser2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);

  System.out.println(fromUser2.parse(startdateString));
 } catch (ParseException e) {
  e.printStackTrace();
 }
Community
  • 1
  • 1
FrankTan
  • 1,626
  • 6
  • 28
  • 63