3

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.

2
  • Are you fetching this String from a java.sql.ResultSet? If so, why not call ResultSet.getTimestamp() instead of ResultSet.getString()? Commented Apr 18, 2012 at 16:29
  • The right answer is already posted, but you could also select via to_timestamp() to Oracle itself and have JDBC convert it for you, but SimpleDateFormat is correct. Commented Apr 18, 2012 at 16:37

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.