• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date conversion returns wrong value  RSS feed

 
Deniz Atak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code like this:



Do you have any opinion about how can I fix this problem?
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you dividing by 1000? The argument when constructing java.util.Date is milliseconds and not seconds.

P.S. Is there any reason you need to use 'Long' rather than 'long' ?
 
Deniz Atak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am dividing by 1000 because this calculation:
licenseExpireNumber = Long.valueOf(currentTime + license_duration)
returns 1305817327000. When I convert this number from Unix time to Date it gives wrong result. When I divide it to 1000 it gives the right result.
There is no particular reason for using Long instead of long.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java works with a number of milliseconds since 01-01-1970, 00:00:00 GMT for dates. 1305817327 milliseconds since that date is Fri Jan 16 04:43:37 EET 1970, so that's why you get that answer.

Dividing (currentTime + license_duration) is probably not correct. Maybe one has milliseconds, the other has seconds? If that's the case, then you have to multiply one of the two numbers (the one that contains seconds instead of milliseconds) by 1000 (and not divide the sum of the numbers by 1000).

Use long instead of Long, because primitive types are more efficient.
 
Deniz Atak
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right I did not need to divide by 1000. When I don't divide by 1000, I got the right answer from the Date function. Thanks for your answer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!