0

I was trying to parse the formatted string date, was getting parse error
input date is "Wed Nov 11 14:24:46 IST 2015", need output date as "Wed Nov 11 2015 14:24:46 IST"

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {
    public static void main(String[] args) {
        try {
            String target = "Wed Nov 11 14:24:46 IST 2015";
            SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
            Date result =  df.parse(target);

            SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
            String result2 =  df2.format(result);

            Date result3 =  df.parse(result2);
            System.out.println(result2);
            System.out.println(result3);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
}

getting error as java.text.ParseException: Unparseable date: "Wed Nov 11 2015 14:24:46 IST"

Raf
  • 7,505
  • 1
  • 42
  • 59
Brittas
  • 491
  • 1
  • 5
  • 18
  • The "yyyy" part is in the wrong place – Nitek Nov 22 '15 at 12:37
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 16 '18 at 03:58

1 Answers1

1

I have updated my answer to do the parsing as mentioned in your question/comment. See below the explanation:

"Wed Nov 11 14:24:46 IST 2015"

to the following

"Wed Nov 11 2015 14:24:46 IST"

I setup two SimpleDateFormat objects as follow

SimpleDateFormat sourceFormat, destinationFormat;

//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);

I then set the timezone as follow

TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");

sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);

I use the sourceFormat object to format the date string to Date object as follow:

Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015

I then use the destination format to format the date object that represents the string as follow:

Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST

Basically in order to get a legit Date object I have to use the first SimpleDateFormat sourceFormat which contains pattern that maps the date in String. Once a legit Date object is created using the String then I use the second formatter to re-format the Date object. Below is full code that should give output if copy/pasted.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class ParseDate {
    public static void main(String[] args) {
        try {
            String target = "Wed Nov 11 14:24:46 IST 2015";
            SimpleDateFormat sourceFormat, destinationFormat;

            sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
            destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);

            TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");

            sourceFormat.setTimeZone(istTimeZone);
            destinationFormat.setTimeZone(istTimeZone);

            Date d = sourceFormat.parse(target);
            System.out.println(d.toString());
            //output: Wed Nov 11 08:54:46 GMT 2015

            System.out.println(destinationFormat.format(d));
            //output: Wed Nov 11 2015 14:24:46 IST

        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
} 
Raf
  • 7,505
  • 1
  • 42
  • 59
  • target variable needs to be parsed in format"EEE MMM dd yyyy kk:mm:ss zzz"" – Brittas Nov 22 '15 at 16:38
  • @Brittas how about now? It addresses all the requirements of the question. – Raf Nov 22 '15 at 18:27
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 16 '18 at 03:58