-1

I want to convert an int value to a date.

For example, 20200605 is 2020-06-05

This is my code, Why doesn't it work?

public static void main(String[] args) {
    int value = 19000101;
    SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
    Date date = originalFormat.parse(Integer.toString(value));
    SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
    String formatedDate = newFormat.format(date);

}

I am getting an error on this line.

    Date date = originalFormat.parse(Integer.toString(value));

The error message is

Type mismatch: cannot convert from java.util.Date to java.sql.Date

I don't know what it means.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
임빈영
  • 11
  • 6
  • 2
    If that is your _exact_ code, do you mean it doesn't compile? Because you're not catching / declaring the `ParseException` from calling `parse`. – BeUndead Jun 04 '20 at 15:25
  • 2
    What isn't working exactly? I ran the code and it runs fine and shows the correct date. – libanbn Jun 04 '20 at 15:26
  • I am getting an error on this line. " Date date = originalFormat.parse(Integer.toString(value));" The error message is "Type mismatch: cannot convert from java.util.Date to java.sql.Date" I don't know what it means – 임빈영 Jun 04 '20 at 15:52
  • Oh, I solved. Thank you very much – 임빈영 Jun 04 '20 at 15:59
  • 1
    I recommend you neither use `SimpleDateFormat`, `java.sql.Date` nor `java.util.Date`. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 04 '20 at 17:22
  • 1
    Welcome to Stack Overflow. (1) When asking “why doesn’t it work”, please be precise about the problem, including error message and/or stacktrace where relevant.. (2) Adding information later is very welcome. Please do it in the question itself rather than in a comment. – Ole V.V. Jun 04 '20 at 17:27
  • In case you thought you needed an old-fashioned `java.sql.Date` for your SQL database, you don’t (by all probability). Since JDBC 4.2 use `LocalDate`. See [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Jun 05 '20 at 04:43

2 Answers2

1

The error you get:

Type mismatch: cannot convert from java.util.Date to java.sql.Date

tells that you have imported a wrong Date object. Please check at the top of the class. You will need to replace import java.sql.Date; with import java.util.Date;

Alexander Pushkarev
  • 1,075
  • 6
  • 19
0

I don't know much about the Date and SimpleDateFormat classes in java, but this should be fairly easy to do anyway without it.

String date_str = ""+value; String formattedDate = date_str.substring(0,4)+"-"+date_str.substring(4,6)+"-"+date_str.substring(6,8);