1

I have a date stored in my database as a string 14/08/2010. I want to parse it into a date in order to compare it with the current date. This is what I did but it doesn't compile I don't know why:

try{
      date = new SimpleDateFormat("dd/MMM/yyyy").parse(rs.getString(2));
      System.out.println(rs.getString(2));
}catch(ParseException s){
      s.printStackTrace();
}

It prints this error:

java.text.ParseException: Unparseable date: "14/08/2010"

I tried changing the date format to dd/MM/yyyy but I have the same problem.

2
  • 3
    Why are you storing the date as a string to start with? And the code does compile - it doesn't work at execution time. It's important to differentiate between compile-time errors and exceptions. Commented Aug 18, 2014 at 16:30
  • In SimpleDateFormat pattern for month is 'M'. Description==> Month: boldIf the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.**bold** docs.oracle.com/javase/7/docs/api/java/text/… Use 'MM' instead of 'MMM' Commented Aug 18, 2014 at 16:52

3 Answers 3

3

You should use dd/MM/yyyy as per the Date string 14/08/2010

MMM matches Month name such as Jan , Feb

Read more about SimpleDateFormat Patterns


I tried

Date date = new SimpleDateFormat("dd/MMM/yyyy").parse("14/08/2010");

that results into

Exception in thread "main" java.text.ParseException: Unparseable date: "14/08/2010"
    at java.text.DateFormat.parse(Unknown Source)
Sign up to request clarification or add additional context in comments.

5 Comments

when I use dd/MM/yyyy it prints this error: java.sql.SQLException: No data found
As per 14/08/2010 date string.
Test Date date = new SimpleDateFormat("dd/MM/yyyy").parse("14/08/2010");
SQLException? java.sql.Date != java.util.Date
Yest this works: Date date = new SimpleDateFormat("dd/MM/yyyy").parse("14/08/2010"); @user3218114 thanks :D
1

You don't know if the problem is with your date parser or the SQL ResultSet.

A debugger will tell you quickly where the problem lies.

I strongly agree with those people who argue that you should not save a DATE in a CHAR column.

I'd write it this way to give myself a chance.

private static final DateFormat DEFAULT_DATE_FORMAT;

static {
    DEFAULT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
    DEFAULT_DATE_FORMAT.setLenient(false);
}


// Your method snippet
try {
    while (rs.next()) {
        String dateAsStr = rs.getString(2);
        Date date = DEFAULT_DATE_FORMAT.parse(dateAsStr);
        System.out.println(DEFAULT_DATE_FORMAT.format(date));
    }
} catch(ParseException e) {
    e.printStackTrace();
} finally {
    // Don't forget to close your resources in finally block.
    close(rs);
    close(statement);
}

Comments

-1

Depending on field definition and Database it is possible that you get "14/08/2010____" instead of "14/08/2010".

On Oracle for instance returns a string filled with spaces when the DB field ist defined as CHAR.

With CHAR(24) you will always get a string with the length of 24 characters.

Try trim.

2 Comments

As per the exception there are no spaces. Date string is enclosed inside double quotes.
Test Date date = new SimpleDateFormat("dd/MM/yyyy").parse(" 14/08/2010 "); Works fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.