• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Passing a dd-MM-yyyy date as a parameter in a procedure

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi!

When I execute this in SQL, it works fine:


Note 1: The third field ('18-04-2024) is a DATE in SQL.
Note 2: The third field has the format DD-MM-YYYY.

However, when I try to execute with a CallableStatement, it returns ORA-01843: not a valid month:


I don't understand why it returns  ORA-01843: not a valid month once the setString is being set with a String and the field is a DATE in SQL, I read it was supposed to work with the setString().

I tried also with setDate() passing a Date as params, but it didn't work also.


Could you help me?
 
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

luize casaretti wrote:I tried also with setDate() passing a Date as params, but it didn't work also.


That's the correct way to do it, passing in java.sql.Date.
 
Marshal
Posts: 4810
602
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you create the Date object used with setDate?  If you used the Date#valueOf method, the string would need to be formatted as yyyy-MM-dd.

For example:

Edit: fixed typo
 
Marshal
Posts: 80751
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What number were you passing for month? Remember that 11 means December.
 
Carey Brown
Bartender
Posts: 11103
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What number were you passing for month? Remember that 11 means December.


Documentation for SimpleDateFormat:
MM    July ; Jul ; 07
 
Campbell Ritchie
Marshal
Posts: 80751
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Date has 0‑based month numbers. Why are we stuck with java.sql.Date following that convention?
 
Saloon Keeper
Posts: 28709
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But Date has 0‑based month numbers. Why are we stuck with java.sql.Date following that convention?


Because it inherits that method from java.util.Date. And, incidentally, it's a deprecated method.

More pædantically, getMonth()returns the month as an index, and Java indices are zero-based. If you want a type-safe month, you use the Calendar month.
 
Master Rancher
Posts: 5173
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:f you want a type-safe month, you use the Calendar month.


Are you referring to java.util.Calendar?  That doesn't seem very type-safe to me, so maybe you mean something else.  And calendar.get(Calendar.MONTH) still returns 0 for January.
 
luize casaretti
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with:



but this new java.sql.Date(DateUtils.parseDate(formatDate, "ddMMyyyy").getTime() still returns yyyyMMdd

I just need to save the dd-MM-yyyy format
 
Sheriff
Posts: 28409
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A java.sql.Date object doesn't have a format. But when you change it to a String then the changing process uses a default format, which naturally is the ISO standard format. If you're so unlucky as to have a different format requirement, then just provide a String which uses your desired format. Don't use a java.sql.Date object.
 
Mike Simmons
Master Rancher
Posts: 5173
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

luize casaretti wrote:but this new java.sql.Date(DateUtils.parseDate(formatDate, "ddMMyyyy").getTime() still returns yyyyMMdd


No, it doesn't.  It returns a java.sql.Date object, which has a toString() method which is what you see if you try to print it out.  That toString() method is documented to always return a string in the format yyyy-mm-dd.  (For consistency with SimpleDateFormat it should say yyyy-MM-dd, but that's not what the API actually says; oh well.). Anyway, that object still represents the date you want.  It's just that when you look at it using toString(), it will print using the format yyyy-mm-dd.

Alternately, look at what gets stored in the database.  Is it the correct date, or not?  The way it gets represented in the DB will depend on your database.  If you don't like it there, you may have to store the field as a varchar in order to be able to specify the format you want.  But in a DB, it's usually best to store a date as a date, and stop worrying about the format.
 
Mike Simmons
Master Rancher
Posts: 5173
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curses, foiled by Dr. C!
 
Paul Clapham
Sheriff
Posts: 28409
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note 1: The third field ('18-04-2024) is a DATE in SQL.


I see you have been receiving contradictory advice. The problem is this note, I think. In the version of SQL that I use (MySQL), that field isn't a DATE. I have to write "DATE(2024-18-04)" in order to get the date value which you use. If I write the string value '18-04-2024' then it's just a string value and it isn't interpreted as a date.

So maybe your stored procedure includes the processing which converts the string value to a date value?
 
Ron McLeod
Marshal
Posts: 4810
602
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:A java.sql.Date object doesn't have a format. ...


But the class does specify the format to use when creating an object based on a String representation: java.sql.Date#valueOf

The documentation wrote:public static Date valueOf(String s)
Converts a string in JDBC date escape format to a Date value.
Parameters:
s - a String object representing a date in in the format "yyyy-[m]m-[d]d". The leading zero for mm and dd may also be omitted.
Returns:
a java.sql.Date object representing the given date
Throws:
IllegalArgumentException - if the date given is not in the JDBC date escape format (yyyy-[m]m-[d]d)

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic