Forums Register Login

java.util.date into java.sql.timestamp

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

nimo frey wrote:

You see the 802 ms!



Firstly, I think those are nanoseconds.
Secondly, you could try,

This gives you..


-Ninad

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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:


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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:
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

nimo frey wrote:
I am eager to wait for http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html



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.
+Pie Number of slices to send: Send
thank you, I will try it.
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 35135 times.
Similar Threads
MY month is shown 1 month ahead.
Printing all days of the week
difference between two timestamps
Expressiveness using implicit conversions
Timer That should never end and trigger daily
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:59:37.