Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.date into java.sql.timestamp

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:

You see the 802 ms!


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

This gives you..


-Ninad

 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, I will try it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic