• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert unix milli seconds to java date :-)

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Anyone know how to convert unix milliseconds to java date/time variable?

i.e.
unix milliseconds = 1189579707
converts to = Wed, 12 Sep 2007 06:48:27 UTC

how can this be done in java?

Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the constructors of java.util.Date.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aye ive tried out using java.util.Date constructor:

Date d = new Date(long);

but using unix milliseconds: 1189579707

it converts d to year 1970

if i try doing:

Date date = new Date(1189579707 * 1000);

it goes into year 1969

this is because (1189579707 * 1000) is not a correct long value, so a minus random value is created

so any idea how i can convert the unix milliseconds to Date? Please :-)

Cheers in advance
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can force the multiplication to be done as a long by making sure at least one of the numbers being multiplied is long:

new Date(1189579707 * 1000L)
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sound thanks a million thats sorted it

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic