-1

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.

dan
  • 347
  • 2
  • 14
  • 2
    you have to provide recreatable example with type of expection you got in order to get some answer about 1st question. See how you can format LocalDate: https://stackoverflow.com/questions/43605744/java-localdate-formatting – rostIvan Nov 26 '22 at 23:23
  • [Please read this](https://www.baeldung.com/java-convert-localdate-sql-date) to convert LocalDate to java.sql.Date and visa versa. – DevilsHnd - 退職した Nov 26 '22 at 23:33
  • 1
    @DevilsHnd So from what I can tell, method 2 is what I am doing, but getting the error. – dan Nov 27 '22 at 00:06
  • Make sure your **import** is `java.sql.Date` and **not** the antique `java.util.Date`. What error? – DevilsHnd - 退職した Nov 27 '22 at 00:12
  • @rostIvan Thanks, looks like DateTimeFormatter is exactly what I need. As for question 1, I am not sure what else I can provide. I went ahead and tested the exact lines of code using this online compiler and it seems to work fine (I also removed the java.sql.Date import to double check it was using the correct one): https://onecompiler.com/java/3yq5ym4f6 – dan Nov 27 '22 at 00:13
  • @DevilsHnd I can confirm I am using import java.sql.Date . The error is in the first sentence of the question where it does not recognize valueOf(java.time.LocalDate) – dan Nov 27 '22 at 00:15
  • I can not reproduce the Error. Code works for me. – DevilsHnd - 退職した Nov 27 '22 at 00:20
  • @DevilsHnd I wish I could say the same haha. I added a direct screenshot to the error in case I had didn't copy it correctly. – dan Nov 27 '22 at 00:26
  • @DevilsHnd `java.util.Date` has no `valueOf` methods. So that cannot be the cause. The OP's error message does seem to be a true mystery. – Basil Bourque Nov 27 '22 at 01:16
  • 1
    You should not use `java.sql.Date`. Since JDBC 4.2 your database can readily accept a modern `LocalDate` object. 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. Nov 27 '22 at 08:29
  • Which is your java version? Up until Java 7 `java.sql.Date` only had a `valueOf(String)` method, which might explain your error message. (And yes, in the end this comment may contradict my previous comment, depending on your answer. JDBC 4.2 requires at least Java 8.) – Ole V.V. Nov 27 '22 at 08:29
  • 1
    @OleV.V. in my build.gradle I am seeing `sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8` – dan Nov 27 '22 at 16:41

1 Answers1

2

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

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 ) ;
E_net4
  • 27,810
  • 13
  • 101
  • 139
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Ah, I see... well if I shouldn't use java.sql.Date either, then it's not too bad if I ignore this error. Should I be storing LocalDate into the database directly then? Or do I convert the LocalDate into a string and then store that instead? (I was going to take a similar approach with java.sql.Date) – dan Nov 27 '22 at 01:43
  • 1
    @dan See my edits addressing your points. – Basil Bourque Nov 27 '22 at 05:10
  • Very thorough, thank you. I too was not sure why there were many downvotes. I tried a few things in Intellij to address the error: restarted, invalidated caches. Both of these did not work unfortunately. I don't want to go through the trouble of reinstalling, but since I will be using only LocalDate now I am not too worried. I have been reading up on a few posts regarding the interaction between LocalDate and DATE columns in database so hopefully I can get it all working from here. – dan Nov 27 '22 at 05:28