1

I am receiving a Java Date formatted like so: "Sun Sep 14 02:00:00 PDT 2014" into a yyyy-MM-dd format but I can't seem to parse it. What I tried is the following:

String time = "Sun Sep 14 02:00:00 PDT 2014";
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date contractEffectiveDateFormat = f.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);

However, I get an error saying that this date is unparsable. I'm not sure how to go about parsing this date because if I try to parse the date using the following:

DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy"); 

which is how to actually parse the date correctly into a Date object, the string would turn into a Date object, but I can't seem to do anything with it from there. I want to turn it in so that it looks like 2014-09-14. Any ideas on how to do so? Thanks!

user1567909
  • 1,450
  • 2
  • 14
  • 24
  • 2
    `Sun Sep 14 02:00:00 PDT 2014` does not match the pattern `yyyy-MM-dd` you need to find a pattern that does – MadProgrammer Sep 15 '14 at 03:39
  • possible duplicate of [Java date/time format](http://stackoverflow.com/questions/3504986/java-date-time-format) – Joe Sep 15 '14 at 03:55

3 Answers3

3

Use two DateFormat(s) one for input and for output,

String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
DateFormat in = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
    Date effectiveDate = in.parse(time);
    System.out.println("Date: " + out.format(effectiveDate));
} catch (ParseException e) {
    e.printStackTrace();
}

Output is the requested

Date: 2014-09-14
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

Your incoming string is this String time = "Sun Sep 14 02:00:00 PDT 2014"; which means the SimpleDateFormat pattern should match the incoming String pattern so you need to use SimpleDateFormat like this

DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);

Then when you called parse() on inFormat it will give you Date Object which doesnot have particular format associated with it. So in order to format the Date again you need to create SimpleDateFormat object specifying the format you want which is this

DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

Ultimately bind all together

One more thing always specify the Locale

String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// good practice to specify the locale
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
try {
    Date parsedDate = inFormat.parse(time);
    System.out.println("Required Formatted Date: " + outFormat.format(effectiveDate));
} catch (ParseException e) {
    e.printStackTrace();
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34
2

Simply add another SimpleDateFormat that'll allow you to present the Date object the way you want:

public static void main(String[] args) throws ParseException {
    String time = "Sun Sep 14 02:00:00 PDT 2014";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
    Date contractEffectiveDateFormat = df.parse(time);
    System.out.println("Date: " + contractEffectiveDateFormat);

    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(f.format(contractEffectiveDateFormat)); // prints 2014-09-14
} 
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129