简体   繁体   中英

Converting Oracle Timestamp datatype string to Java Date

I have a timestamp field of Oracle in Java String

String strDate = "24.12.12 03:30:00,000"; //Timestamp field of Oracle

I need to convert it to Java.util.Date.

Please tell me a way to do this.

Note: I don't have first two digits for the year, how can java understand that this is year 1912 or 2012 and so on.

The class you want is SimpleDateFormat . You'll create a pattern that matches your expected input, and parse it. Here's an example, though it may be incorrect in some places (I used 0-23 hour, maybe you want 1-24, etc.)

String strDate = "24.12.12 03:30:00,000";
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy HH:mm:ss,SSS");
Date date = sdf.parse(strDate);

Regarding your 1912 vs 2012 problem, Java will make some assumptions when there are only 2 digits. Specifically:

For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created.

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