0

Unable to parse the given date

String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ss z");           
Date date = simpleDateFormat.parse(DT);

after this I want to convert to CST Time in this format 13-JUN-16 08.53.43

Exception StackTrace

java.text.ParseException: Unparseable date: "14-Jun-2016 09:54:02 GMT" at java.text.DateFormat.parse(Unknown Source) at package2.TimeZone.parseTime(TimeZone.java:16) at package2.TimeZone.main(TimeZone.java:10) 
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • have you tried `joda-time` library to change date time into multiple compatible format as you wish ?? @Hifzur – Vikrant Kashyap Jun 14 '16 at 10:59
  • please add stackTrace Error and update your question to clarify the Exception cause. – Vikrant Kashyap Jun 14 '16 at 11:00
  • is Joda-Time in-built of JDK 7? because our project is based on JDK 7! – Hifzur Rahman Jun 14 '16 at 11:02
  • java.text.ParseException: Unparseable date: "14-Jun-2016 09:54:02 GMT" at java.text.DateFormat.parse(Unknown Source) at package2.TimeZone.parseTime(TimeZone.java:16) at package2.TimeZone.main(TimeZone.java:10) – Hifzur Rahman Jun 14 '16 at 11:03
  • ` String DT = "14 Jun 2016 09:54:02 GMT"; DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); Date dd = simpleDateFormat.parse(DT); System.out.println(simpleDateFormat.parse(DT).toString()); //it will print Tue Jun 14 15:24:02 IST 2016 System.out.println(new SimpleDateFormat("dd-MMMM-yy HH:mm:ss").format(dd).toString().toUpperCase()); //it will print 14-JUNE-16 15:24:02` – Vikrant Kashyap Jun 14 '16 at 11:17
  • 1
    thanks @VikrantKashyap – Hifzur Rahman Jun 14 '16 at 11:33

2 Answers2

0

To parse:

You are using the wrong mask to parse it, it should be as follows(with MMM instead of MM):

DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
Date date = simpleDateFormat.parse(DT);

To format it to the desired format:

DateFormat sdf= new SimpleDateFormat("dd-MMM-yy HH.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("US/Central"));
String formattedDate = sdf.format(date).toUpperCase();

Without the uppercase it will show 13-jun-16 08.53.43 instead of 13-JUN-16 08.53.43

SCouto
  • 7,808
  • 5
  • 32
  • 49
0

Date format should be like as below:

String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yy HH:mm:ss z");           
Date date = (Date) simpleDateFormat.parse(DT);

After converting date format, it should pass like below:

    String newstring = new SimpleDateFormat("dd-MMMM-yy HH:mm:ss").format(date).toString().toUpperCase();
    System.out.println(newstring); 
CrazyJavaLearner
  • 368
  • 8
  • 17