0

I am importing data from a csv file via opencsv to insert into a mysql DB. opencsv imports as string and for 1 field in the DB I need to parse it to date in the format: yyyy-MM-dd. However I am getting an error.

// This is the string that I have extracted from the csv file           
String elem1 = nextLine[0];

// printing out to console I can see the string I wish to convert
System.out.println(elem1); => 2015-08-14

// Below is my code to parse the date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date convertedCurrentDate = sdf.parse(elem1);
String date=sdf.format(convertedCurrentDate );

// printing date to console gives me 2015-08-14
System.out.println(date);

As mentioned above printing date out to console gives me 2015-08-14. However I get the error:

java.text.ParseException: Unparseable date: "" 

Can someone give me some advice as to what I am doing wrong?

The line 'java.util.Date convertedCurrentDate = sdf.parse(elem1);' is the line causing the error.

Thanks!

user1523236
  • 1,403
  • 3
  • 20
  • 43
  • Can you point out what line is causing the error? Is it: java.util.Date convertedCurrentDate = sdf.parse(elem1); Thanks – Stack Player Aug 21 '15 at 16:02
  • Hey Stack Player. Yep that's the line. I have edited the original question to include that detail. – user1523236 Aug 21 '15 at 16:06
  • Then `elem1` is obviously a blank string at that point (obvious because the error message says so). – Andreas Aug 21 '15 at 16:07
  • What version of Java are you using? I know this probably isn't the issue here but in the Java7 Docs I'm seeing no parse(String) only parse(string, Parseposition) ? http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#parse(java.lang.String,%20java.text.ParsePosition) (Edit and Java6 & 8) – Kevvvvyp Aug 21 '15 at 16:12
  • Hey Andreas, OK, but I have printed the value of elem1 before the parse code and it's correct. Also I print the date variable after the parse code and it gives me the date too. So I don't understand how this is a blank string. – user1523236 Aug 21 '15 at 16:12
  • @kevinPaton - Version 8 update 45. – user1523236 Aug 21 '15 at 16:14
  • Strange, ran the code works fine for me. (I wonder why there isn't .parse(String) in the docs? Maybe I'm an idiot and missed it.) – Kevvvvyp Aug 21 '15 at 16:16
  • Try setting the locale? http://stackoverflow.com/questions/781257/unexpected-java-simpledateformat-parse-exception – Kevvvvyp Aug 21 '15 at 16:18
  • If sdf.parse() had thrown an exception, the rest of the statements would not execute. So, I don't think your sdf.format() and sysout(date) would give output then. Are you sure about the stack trace line? – Ravi K Thapliyal Aug 21 '15 at 16:18
  • I think it is something to do with the fact that it has been extracted from a csv file. I created a new variable String elem2 = "2015-08-16"; and updated my code to parse elem2 and it worked. Why it won't parse the elem1 is very strange. – user1523236 Aug 21 '15 at 16:22
  • @KevinPaton No, Locale is not an issue here as there no text, no names of month or names of day-of-week in the expected input. With only digits, and a specified parsing pattern, Locale is irrelevant. If using a soft-coded localized parsing pattern, then Locale would matter. But that is not the case here. – Basil Bourque Aug 22 '15 at 03:21
  • @BasilBourque thank you – Kevvvvyp Aug 22 '15 at 10:46

2 Answers2

1

I am stumped also the following test passes on my machine

public class DateFormatterTest {

private static final String TEST_DATE = "2015-08-14";

   @Test
   public void SimpleDateFormatTest() throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      java.util.Date convertedCurrentDate = sdf.parse(TEST_DATE);
      String date=sdf.format(convertedCurrentDate );
      assertEquals(TEST_DATE, date);
   } 

}

What version of Java are you running? I know there was an issue in the latest java 8 (8u61) and JodaTime.

Also try the test above that eliminates everything but the Date code.

Scott Conway
  • 975
  • 7
  • 13
0

Here is the Simple Example to do that :

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}

Java 8 update

String string = "August 21, 2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2015-09-21

i think this is what you want .Happy to help thanks

Anand Dwivedi
  • 1,452
  • 13
  • 23
  • Hey Anand - Thanks for your reply. I tried your suggestion but got the error: Text '2015-08-14' could not be parsed at index 0. The date format in the csv file is yyyy-MM-dd. so I updated the code above to read: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM, dd", Locale.ENGLISH); but this gave me an error: Text '2015-08-14' could not be parsed at index 4. - Any ideas? – user1523236 Aug 21 '15 at 16:32
  • First you Check you system Date Format then same Format you provide on code – Anand Dwivedi Aug 21 '15 at 16:35
  • I presume you mean the date format on my mac? Checked it and it's in the same format - 2015-08-21. Time is in 24hr format. Not sure if that matters. – user1523236 Aug 21 '15 at 16:40
  • Since your mac it shown like 2015-08-21 and you pass as August 21, 2015 or 7-Jun-2013 may be it create problem . – Anand Dwivedi Aug 21 '15 at 16:54
  • No, I pass in the format - 2015-08-14. Have this listed in the original question. "System.out.println(elem1); => 2015-08-14" - So, yeah. I'm pretty stumped at this stage. :-( – user1523236 Aug 21 '15 at 17:39