• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Number of slices to send:
    Optional 'thank-you' note:

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?
 
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

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: 4525
572
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
What number were you passing for month? Remember that 11 means December.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

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: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
But Date has 0‑based month numbers. Why are we stuck with java.sql.Date following that convention?
 
Saloon Keeper
Posts: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:

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: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
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: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

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: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
Curses, foiled by Dr. C!
 
Paul Clapham
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:

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: 4525
572
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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)

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |