Home > Blockchain >  java.sql.Date.valueOf(java.time.LocalDate) not recognized and LocalDate formatting
java.sql.Date.valueOf(java.time.LocalDate) not recognized and LocalDate formatting

Time:11-27

The code below gives me the error 'valueOf(java.lang.String)' in 'java.sql.Date' cannot be applied to '(java.time.LocalDate)', which I understand means that valueOf is expecting a string... but according to this post, it should also be able to accept a LocalDate.

LocalDate releaseDate = LocalDate.of(year, month, day);
Date sqlDate = Date.valueOf(releaseDate);

Here is the same code running on an online compiler: https://onecompiler.com/java/3yq5zkf6d

What am I doing wrong?

My second problem is that I would like to format my month in MMM without having to use java.util.Date (I read somewhere that LocalDate is now preferred and so I have been trying to avoid java.util.Date):

int year = selectedYear;
int month = selectedMonth;
int day = selectedDay;

binding.buttonReleaseDate.setText(** formatted date string would go here **);

My end goal is to then pass my sqlDate to a database, hopefully I am taking the correct approach for doing so.

Appreciate any help I can get with this.

CodePudding user response:

A real mystery

Your code should indeed work in Java 8 .

Your error message:

valueOf(java.lang.String)' in 'java.sql.Date' cannot be applied to '(java.time.LocalDate)

… does indeed seem to be a true mystery.

Some other folks here suggested you were using java.util.Date rather than java.sql.Date. That cannot be the case. The java.util.Date class has no valueOf methods.

See these two lines:

LocalDate localDate = LocalDate.of( 2023 , 1 , 23 ) ;
java.sql.Date sqlDate = java.sql.Date.valueOf( localDate ) ;

run at Ideone.com:

2023-01-23

2023-01-23

I am sorry your legitimate Question has suffered multiple down-votes. Stack Overflow is an imperfect place.

IDE confused?

My only wild guess is that your IDE is having a brain spasm.

On occasion, any of the major IDEs (IntelliJ, Eclipse, NetBeans) can become frightfully confused. I suggest you first restart the IDE and your computer. Then search for tips on doing a reset. At the very least, look for ways to do a "clean", then rebuild your project. Perhaps you'll need to go further, looking for ways to "flush caches" or some such option.

Database

You said:

My end goal is to then pass my sqlDate to a database, hopefully I am taking the correct approach for doing so.

No, never use the java.sql.Date class. It was replaced years ago by java.time.LocalDate. As of JDBC 4.2 and later, support for LocalDate is required of all JDBC drivers.

Writing to database:

myPreparedStatement.setObject( … , localDate ) ;

Retrieval:

LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;

You commented:

Should I be storing LocalDate into the database directly then?

Avoid the legacy date-time classes wherever you can. Use only java.time classes wherever you can. The java.time framework is industry-leading.

Among the many flaws in java.sql.Date is that it pretends to represent a date-only but actually has a time-of-day with an implied offset-from-UTC. And I think you'll find a separate time zone buried in that source code as well. A terrible mess.

In contrast, a java.time.LocalDate truly represents a date only value. Just a year, month, day, with no time-of-day, no offset, and no time zone.

If handed a java.sql.Date object, immediately convert to LocalDate. Use the new conversion methods added to the old classes.

LocalDate localDate = myJavaSqlDate.toLocalDate() ;

You commented:

Or do I convert the LocalDate into a string and then store that instead?

No, avoid dumb strings where you have smart types available. If you use the java.time classes with a JDBC driver for JDBC 4.2 , you should have no problems.

But always do extensive experimentation and testing to be sure that (a) you understand how it all works, and (b) that all seems to be operating correctly. I recommend always having a few basic stupid-simple tests in your code-base as a sanity-check, to confirm the basic operations are working.

Generating text

You said:

My second problem is that I would like to format my month in MMM

You can hard-code a desired format for a LocalDate using DateTimeFormatter. Search Stack Overflow to learn more as this has been covered many many times already.

I suggest alternatively letting java.time automatically localize. Again, search to learn more. Use a Locale language to specify the human language and cultural norms of localization.

Locale locale = Locale.UK ; 
DateTimeFormatter f = 
    DateTimeFormatter
    .ofLocalizedDate ( FormatStyle.LONG ) 
    .withLocale( locale ) ;
String output = localDate.format( f ) ;
  • Related