save date in oracle db in format MM/DD/YYYY HH:MM:SS AM/PM

Posted on 2008-11-16
Medium Priority
Last Modified: 2013-12-29
how can i save a java.sql.Date/java.sql.Date object in db as in format MM/DD/YYYY HH:MM:SS AM/PM?

When i tried saveToDb( new Date() ). The time part is being lost and only saving MM/DD/YYYY. How can i solve this problem.
Question by:komlaaa
  • 3
  • 2
  • 2
LVL 15

Expert Comment

ID: 22973299
sample code
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Timestamp myTime = new Timestamp(sdf.parse(myTimeString));
PreparedStatement pstmt = con.prepareStatement(mysql);
pstmt.setTimestamp(1, myTime);

Open in new window


Author Comment

ID: 22973467
Sorry if i did not elaborate my question but i am using hibernate  session.save( myObject ) method to db, and myObject has to take a date field, something like:

Begning of my code:
Calendar startCal = request.getStartDate();
Date startDate = startCal.getTime();
 myObject.set ( assetStartDate )
session.save ( myObject )

Note: This database column has Date dataType  and not TimeStamp.

LVL 15

Accepted Solution

quincydude earned 500 total points
ID: 22973582
java.sql.Timestamp is suitable for setting date with time in DB. May be you try to adjust your myObject a bit for that?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Assisted Solution

CEHJ earned 500 total points
ID: 22974127
>>Note: This database column has Date dataType  and not TimeStamp.

java.sql.Date or java.util.Date? If the former, time is not supported

Author Comment

ID: 22976165
the database dataType is java.sql.Date,  and i am not allow the change it
LVL 86

Expert Comment

ID: 22976284
That doesn't support time
LVL 15

Expert Comment

ID: 22980916
Just change the configuration in the hibernate xml file

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question