How do I get date and time in the same format from database using gettimestamp() in Java
In my database, I have value set as 9/28/2010 11:30:00 PM
.
Now I am making use of dataResultSet.getTimestamp(3)
which gives me a value as 2010-09-28 23:30:00.0
. I want my result to be same as that of database values.i.e, I want the time to be 11:30
.
What could be solution for this? Can anyone help me with this?
开发者_如何学编程Thanks in Advance.
Actually, I suspect that in your database, the value is set as 1285716600
. What you likely mean is that your database client is displaying this value as "9/28/2010 11:30:00 PM".
This is entirely a matter of formatting - a Date represents a specific instant in time, and is not a String. Rather, there are multiple potential ways of converting a Date into a String representation of it. In particular, note that you don't get the date in any particular format from the database; it arrives as a Date, and it's up to you to format it.
How are you converting this date into a String? If you want a particular output style, you should use the SimpleDateFormat class (or equivalent).
Your resulting Timestamp
is the same as the database value. The difference is how they're displayed, but that has nothing to do with the value itself. In the database, it depends on the settings (e.g. NLS settings in Oracle). In Java, it depends on the DateFormat
used to format the date for output. If you don't use one explicitly, the default depends on the system Locale.
Use SimpleDateFormat to transform your Date
object into the string you need.
If your problem is just the String representation, you can use this:
// date is of type java.util.Date
String dateStr = new SimpleDateFormat("d/M/yyyy hh:mm:ss a").format(date);
Good Luck.
精彩评论