| Author |
java.util.date into java.sql.timestamp
|
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I have a java.util.date:
which represents this date:
I convert this date to a java.sql.timestamp:
which represents this timestamp:
You see the 802 ms! How can I set a date which represents this timestamp?:
2010-09-01 08:00:00.000000
|
 |
Ninad Kuchekar
Ranch Hand
Joined: Jan 05, 2010
Posts: 64
|
|
nimo frey wrote:
You see the 802 ms!
Firstly, I think those are nanoseconds.
Secondly, you could try,
This gives you..
-Ninad
|
Don't walk as if you rule the world, walk as if you don't care who rules it...
|
 |
Ninad Kuchekar
Ranch Hand
Joined: Jan 05, 2010
Posts: 64
|
|
FYI, this needed a simple search to lookup the Java Documentation for java.sql.Timestamp
It might have solved your query before you got any help from anyone else.
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
thanks ninad,
t.setNanos(0) works.
but the real problem is another thing:
Is there a way to achieve this without using timestamps?
I have only a date-instance and do define no nanos, so it should normally 0ns:
but when representing this as a timestamp, then it shows me that I do not set my date properly.
I want that date:
year: 2010
month: sept
day: 1
hour: 8
minutes: 0
(with ms=0, ns=0)
Am I am forced to convert this as a timestamp and set the ns and convert that back to a date?
This is annoying:
|
 |
Ninad Kuchekar
Ranch Hand
Joined: Jan 05, 2010
Posts: 64
|
|
I am guessing you are using Timestamp to communicate with the JDBC API to identify it as a "SQL TIMESTAMP" value.
If you are not using any database why go for Timestamp at all. The thing is, the java.sql.Timestamp is essentially a type of java.util.Date except for the nanoseconds part.
If you do not want the fractional seconds and not using database operations you can go for methods from java.util.Date.
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
java.util.Date has no method for defining the nanoseconds part. I have no clue why my date is returning such a timestamp (with 802 nanoseconds):
2010-09-01 08:00:00.802
instead of that:
2010-09-01 08:00:00.000
I have not explicitly defined the nanoseconds part when creating a new instance of java.util.date.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
nimo frey wrote:
The first line creates a calendar with the current moment, including milliseconds (Calendar doesn't know about nanoseconds). You then set the date, hour, minute and second, but the number of milliseconds remains the same.
Simply add the following call to reset the number of milliseconds:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
Thank you, Rob. It works!
I am eager to wait for http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2335
|
|
Actually you do not have to wait. You can include JodaTime library in your project today. Among other benefits, having an immutable date is a blessing. I work with dates extensively in my project and JodaTime was a life-saver.
Unfortunatelly, when I last checked it seemed to me it won't make it into JDK7, but I may be mistaken as I don't know the details of JDK development politicies.
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
|
thank you, I will try it.
|
 |
 |
|
|
subject: java.util.date into java.sql.timestamp
|
|
|