简体   繁体   中英

Converting String date to SQL Date

I have a string date in the format DD/MM/YY (16/07/13)
How do I convert this string date to SQL Date format (maintaining the same DD/MM/YY format)
for storing in my Oracle DB.. ??

Use a SimpleDateFormat :

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String stringWithDate = "16/07/13";
Date theDate = sdf.parse(stringWithDate);
//store theDate variable in your database...

Note that SimpleDateFormat#parse throws ParseException .

I don't know Java and I'm not super familiar with Oracle, but this may help.

In MS SQL Server I've seen all kinds of bad stuff happen when people try to post stuff using specific date formats. Windows date settings can vary so problems are almost guaranteed. From that point of view your

(maintaining the same DD/MM/YY format)

rings alarm bells

I'm yet to have issues as I usually fall back on handling dates in a "standardised" way.

            'yyyymmdd'  
select CAST('19900506' AS DATETIME)

is very predictable, so if you want predictable you need the oracle equivalent

it also works for date + time

            'yyyymmdd hh:mi:ss.mmm'  
select CAST('19900506 01:01:01.000' AS DATETIME)

Oracle/PLSQL will have similar functions that work in an controllable way. Use these functions to save values correctly then your data can be reliably output in whatever format you specify at the time of retrieval

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM