-2

I have a date string as

"Wed Jul 01 08:16:13 PDT 2015"

I am trying to parse it with this SimpleDateFormat

SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

this way:

Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");

It is giving me parse error as:

java.text.ParseException: Unparseable date: "Wed Jul 01 08:16:13 PDT 2015" (at offset 0)

How can I get a date in above simple date format from the string

Akshay
  • 833
  • 2
  • 16
  • 34

5 Answers5

1

Try this:

DateFormat originalFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = originalFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
String formattedDate = targetFormat.format(date); 
System.out.println(formattedDate);
Abhishek
  • 878
  • 12
  • 28
0

I suspect you don't realy understand the consept of the SimpleDateFormat.

After you define the template with :

        SimpleDateFormat valueDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

The valueDateFormat could parse Date object acording to it, it not take just a String you have and convert it. it take Date object.

Date

yshahak
  • 4,996
  • 1
  • 31
  • 37
0

Your date string ("Wed Jul 01 08:16:13 PDT 2015") doesn't match your pattern ("yyyy-MM-dd hh:mm:ss")

Write correct pattern which matches date string (first goes Day of week, than month in year, etc.)

Max77
  • 1,466
  • 13
  • 19
0

For the formats like this I created a helper method:

public Date parseString(String date) {

    String value = date.replaceFirst("\\D+([^\\)]+).+", "$1");

    //Timezone could be either positive or negative
    String[] timeComponents = value.split("[\\-\\+]");

    long time = Long.parseLong(timeComponents[0]);
    int timeZoneOffset = Integer.valueOf(timeComponents[1]) * 36000; // (("0100" / 100) * 3600 * 1000)

    //If Timezone is negative
    if(value.indexOf("-") > 0){
        timeZoneOffset *= -1;
    }

    //Remember that time could be either positive or negative (ie: date before 1/1/1970)
    //time += timeZoneOffset;

    return new Date(time);
}

It returns date object from Date string so you can format your string like this:

Date date = parseString("Wed Jul 01 08:16:13 PDT 2015");

And after that you can easily format given date variable.

Tomislav
  • 3,181
  • 17
  • 20
0

try this..

public static void main(String[] a) 
{

  SimpleDateFormat valueDateFormat = new SimpleDateFormat("EEE MMM yy hh:mm:ss");
  try {
    Date parsedDate1 = valueDateFormat.parse("Wed Jul 01 08:16:13 PDT 2015");
    System.out.println(parsedDate1);
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
 }
kavetiraviteja
  • 2,058
  • 1
  • 15
  • 35