Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In my java project I'm trying to create a timestamp that will be stored later in the database (Oracle Database). After many trials I found that the timestamp my java code generates is in the following format:

2017-06-20 14:38:12.296

and the timestamp format in Oracle is like:

20-JUN-19 02.38.55.059000000 PM

Is there a way to convert the java format of timestamp to oracle's format?

My code for creating the timestamp:

Date date = new Date();
long time = date.getTime();
Timestamp timeStamp = new Timestamp(time);

I have tried so many timestamps conversions but with no use.

The format of timestamp is irrelevant (in both Oracle and Java). Please show us the full classname of the Timestamp from your example and also tell us how would youlike to write the timestamp into database. – Michal Jun 20, 2019 at 16:06 This is the java class for the timestamp: java.sql.Timestamp, and I would like to convert the first format above to this format "20-JUN-19 02.38.55.059000000 PM" ,as an example, in order to insert it into the database. – Basma M. Mostafa Jun 20, 2019 at 16:09 That is the correct type. You do not need to reformat it in order to put in into Oracle. Just write into database (e.g., via JDBC) and you will see Oracle shows it in the correct format. – Michal Jun 20, 2019 at 16:11 You need to understand - the formats you are showing are not relevant when storing the timestamp in database. The timestamp is just a number and that number is all what is needed to store it in database. – Michal Jun 20, 2019 at 16:14 Another way to tell you what Michal is saying, is that the timestamp is always the same value no matter what format you give it, the format only matters for visual purpose to show to the user. – BugsForBreakfast Jun 20, 2019 at 16:16

To add from QuaternionsRock's answer, since it's not tested, you can always use DateFormat.

From his link, you can see the following example, which I am able to test with a date such as 20-JUN-19 06.06.05.112 PM.

DateFormat df = new SimpleDateFormat("dd-MMM-yy HH.mm.ss.SSS a");
Date date = df.parse(oracleTS);
Timestamp ts = new Timestamp(date.getTime());

There does not need to be two a in the DateFormat string, contrary to what is posted in QuaternionsRock' link.

This is how to convert from an oracle timeStamp to a java timeStamp; OP requested the inverse. – Greg Apr 21, 2022 at 18:50

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.