0

I have a problem with parsing a string to sql.date This code works in my project only the first time, it will parse the date normally, but second time it throws exception.

I printed the date the function receives and it is the same format, for example 02.02.2016 was okey, I only changed month to 02.04.2016 and the exception was raised.

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final String sqldateFormat = "yyyy-mm-dd"; 

public java.sql.Date changeDate(String date) {      
    String newDate = "";
    try {
        java.util.Date d = dateFormat.parse(date);
        dateFormat.applyPattern(sqldateFormat);
        newDate = dateFormat.format(d);

    } catch (ParseException e) {
        e.printStackTrace();
    }                   

    return java.sql.Date.valueOf(newDate);
}
3
  • 1
    Please show the whole exception (not in comments, by editing the question) Commented Apr 26, 2016 at 14:24
  • 3
    Why go from string to java.util.Date to string and then to java.sql.Date? You can create a java.sql.Date from the time value of a java.util.Date. Commented Apr 26, 2016 at 14:24
  • Consider what happens when you use applyPattern on the field dateFormat: you are changing the dateFormat, so a subsequent invocation of changeDate will fail. Commented Apr 26, 2016 at 14:26

4 Answers 4

2

Try this

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqldateFormat = new SimpleDateFormat("yyyy-mm-dd");

public java.sql.Date changeDate(String date) {      
    String newDate = "";
    try {
        java.util.Date d = dateFormat.parse(date);
        newDate = sqldateFormat.format(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }                   
    return java.sql.Date.valueOf(newDate);
}

Because during the fisrt execution you are modifying the pattern of the SimpleDateFormat it won't be able to parse the second date.

dateFormat.applyPattern(sqldateFormat); will modify the pattern to "yyyy-mm-dd" and then parsing 02.04.2016 will throw an exception.

Sign up to request clarification or add additional context in comments.

Comments

0

this is because you change pattern of dateFormat. This will work:

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-mm-dd");

public java.sql.Date changeDate(String date) {
    String newDate = "";
    try {
        java.util.Date d = dateFormat.parse(date);
        newDate = sqlFormat.format(d);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return java.sql.Date.valueOf(newDate);
}

Comments

0

Apparently, this will work for the first run, but not for the second. Your problem is that you call applyPattern(), so it'll expect the new dates in sql date format only.

Here is a little better code:

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");
private final SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-mm-dd"); 

    public java.sql.Date changeDate(String date) {      

    String newDate = "";
    try {
        java.util.Date d = dateFormat.parse(date);
        newDate = sqlFormat.format(d);

    } catch (ParseException e) {
        e.printStackTrace();
    }                   

    return java.sql.Date.valueOf(newDate);
}

Comments

0

Don't use valueOf().

If you have a java.util.Date and want a java.sql.Date (or java.sql.Timestamp), use the Date(long date) constructor:

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

Also, don't catch exceptions and continue execution without handling it (printing it is not handling it).

Meaning that your code should be:

private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy");

public java.sql.Date changeDate(String date) {
    try {
        return new java.sql.Date(dateFormat.parse(date).getTime());
    } catch (ParseException e) {
        throw new IllegalArgumentException("Invalid date: " + date);
    }
}

Warning: SimpleDateFormat is not thread-safe:

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Comments

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.