8

I have following time stamp in Integer form

1333125342

I can convert it using SQL:

select DATEADD(ss, FlOOR(1333089223/86400)*86400, '1970-01-01 00:00:00') AS Date  

How to convert it in java? So that it would return value:

3/30/12 12:18:43 PM
kinkajou
  • 3,664
  • 25
  • 75
  • 128

3 Answers3

22

Assuming its the time since 1/1/1970 in seconds. you can try

String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                          .format(new Date(1333125342 * 1000L));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

if it is milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT

then simply use

new java.util.Date(millis);

and if you need it in particular format

3/30/12 12:18:43 PM

then use SimpleDateFormat to format the Date to desired formatted String

jmj
  • 237,923
  • 42
  • 401
  • 438
0

That timestamp contains the seconds elapsed since 1970-1-1 0:00 UTC.

To convert it to a Java Date instantiate a new Date Object (see Java doc) and invoke setTime() on that. Note, that setTime expects milliseconds instead of seconds, so you would have to multiply your timestamp by 1000.

The toString() method yields something readable.

ovenror
  • 552
  • 4
  • 11