Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java.util.date into java.sql.timestamp

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, I will try it.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic