天天看点

date java sql 日期_Java:SQL日期格式(Java: SQL date format)

Is there any to assign SQL date directly instead of converting through SimpleDateFormat and parse to sql date. I am reading few date fields from a file lets say 03/09/2017 and I also have its oracle equivalent format defined as a field definition mm/dd/yyyy.

I am reading the date and its format through Java and inserting to Database. Currently I am assigning the date format to SimpleDateFormat and parsing the date read from file and converting to SQL date as follows.

SimpleDateFormat YMDFmt = new SimpleDateFormat("mm/dd/yyyy");

// This is not hardcoded field.

// Its field definition will be directly assigned as defined. In this case it is mm/dd/yyyy

Date date;

String datefield="03/09/2017";

date=YMDFmt.parse(datefield);

java.sql.Date sqldt= new java.sql.Date(date.getTime());

Here my problem is due to varying formats, mm is treated as minutes in java where as it is month in oracle. I will have to change to 'MM' to make it work in java but in my scenario i dont know the original format of date. i will have to read the format and parse accordingly? Please advise if there is anyway to just convert oracle format to equivalent java format (mm/dd/yyyy to MM/dd/yyyy and so on) or any other solution. Thank you.