-5

I am trying to get Java to read in a date formatted as follows: Thu Mar 8 13:33:25 2012 But getting an unparseable exception. Here's the code:

SimpleDateFormat formatter2 = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
String currentDate = "Thu Mar 8 13:33:25 2012";
Date date2 = formatter.parse(currentDate);

It throws the following exception: Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Mar 8 13:33:25 2012"

Could someone help me out please? I've tried changing the "d" to "dd" but still doesn't work.

Smita Ahinave
  • 1,833
  • 7
  • 19
  • 39

2 Answers2

2

You are not using formatter2 variable as specified in your example.

Your code works when using formatter2.

SimpleDateFormat formatter2 = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
String currentDate = "Thu Mar 8 13:33:25 2012";
Date date2 = formatter2.parse(currentDate);
System.out.println(date2);

Result:

Thu Mar 08 13:33:25 CAT 2012

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
0
String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat =
        new SimpleDateFormat(pattern, new Locale("en", "IN"));

String date = simpleDateFormat.format(new Date());
System.out.println(date);
plamut
  • 2,702
  • 9
  • 27
  • 35