1

I am converting java.util.Date to java.sql.date to insert date of birth in mysql database.

But entering 2001-04-03 always results in a java.sql.Date of 2000-12-31

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter dob(YYYY-MM-DD) :");
String date = br.readLine();

SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
            
java.util.Date util_dob = dateFormat.parse(date); 
            
java.sql.Date dob = new java.sql.Date(util_dob.getTime());

input: 2001-04-03

output: 2000-12-31

expecting: 2001-04-03

csalmhof
  • 1,820
  • 2
  • 15
  • 24
  • 1
    I strongly recommend that you don’t use `SimpleDateFormat` and the two `Date` classes. They are troublesome and hacky, nothing you want to struggle with, and fortunately all long outdated. Use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). `LocalDate.parse(date)`. You can directly pass the resulting `LocalDate` to your SQL database. See [Insert & fetch java.time.LocalDate objects to/from an SQL…](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) – Ole V.V. Dec 29 '22 at 07:49
  • 1
    Welcome to Stack Overflow. It’s nice and good that you are giving a small working example and you are precise about both expected and observed output. For a still more minimal example you could hardcode `String date = "2001-04-03";` and leave output reading from input. You might also print the `java.util.Date` since this would demonstrate the problem, and you would not need to convert to `java.sql.Date` for this. If this was your first question, you are doing really good. – Ole V.V. Dec 29 '22 at 08:20

1 Answers1

1

Your format-string is not correct.

Take a look at the documentation of SimpleDateFormat: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html

  • Uppercase Y: pattern letter for Week year
  • Uppercase M: pattern letter for Month in year (correct)
  • Uppercase D: pattern letter for Day in year

You should use lowercase y and lowercase d.

  • Lowercase y: pattern letter for Year
  • Lowercase d: pattern letter for Day in Month

I think your converting should work with this pattern:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
csalmhof
  • 1,820
  • 2
  • 15
  • 24
  • 1
    It’s correct, but allow me to be honest: No one should use `SimpleDateFormat` excatly because it’s so troublesome and easy to get wrong. Helping users how to use it anyway is no real help. Helping users to migrate to java.time, the modern Java date and time API, is the real help. – Ole V.V. Dec 29 '22 at 07:52
  • 1
    You're right, `SimpleDateFormat` should not be the way to deal with dates in 2022. But sometimes you have to deal with legacy code and don't have time or budget to refactor everything. Anyway, only using the java.time API would not solve the user's problem here, because the problem is the format string which would also lead to wrong results with the java.time API. – csalmhof Dec 30 '22 at 10:52
  • Yes and no. (1) `LocalDate.parse(date)` parses the date string without requiring any formatter at all, which definitely does solve the user’s problem. Alternatively use the built-in `DateTimeFormatter.ISO_LOCAL_DATE`. (2) Even if using the incorrect format pattern string from the question, java.time will throw an exception rather than tacitly giving an incorrect date, which is already an improvement. Also I doubt that code trying to parse using the wrong pattern `YYYY-MM-DD` is legacy code. – Ole V.V. Dec 30 '22 at 12:05