0

So I am using DateFormat to convert a date (that I parsed form a CSV) to YYYY-MM-DD format, so I can INSERT it into a MYSQL column which is in DATE format.

I am using DateFormat, but my date output keeps coming out like this:

Mon Dec 29 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Tue Dec 30 00:00:00 CST 2014
Wed Dec 31 00:00:00 CST 2014

How can I convert this date to the format I am looking for?

DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("yyyy-MM-dd");

date = formatter.parse(date_parsed[0].toString());
System.out.println("DATE=" + date);
user1861967
  • 121
  • 5
  • 14
  • When you parse with `DateFormat` the date syntax you specified will be used to process the input strings but has nothing to do with the output. You simply used the `date` instance in output which means a `date.toString()` call and this uses a default format for dates. Try to use `System.out.println(formatter.format(date));` to output your date in your own format. – marczeeee Jan 14 '15 at 21:46
  • Also a duplicate of [Parsing error for date field](http://stackoverflow.com/q/17225948/642706) – Basil Bourque May 14 '17 at 01:54

1 Answers1

0

Try this:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "Tue Dec 30 00:00:00 CST 2014";
Date date = formatter.parse( dateStr );
formatter.applyPattern("yyyy-MM-dd");
String newDate = formatter.format( date );
System.out.println("DATE=" + newDate);

Output :

DATE=2014-12-30
A human being
  • 1,220
  • 8
  • 18