0

I must convert a linux timestamp to android date. i get this number from server

1386889262

I have written a small code snippet.

Date d = new Date(jsonProductData.getLong(MTIME));
SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy");
.setTimeZone(TimeZone.getTimeZone("GMT"));
formatTime = f.format(d);

but it doesen't convert right, this is my result

17.01.1970

EDIT: Normally i must get this here

12.12.2013

Is there an another method to get the right date???

fecub
  • 945
  • 10
  • 27
  • possible duplicate of [Unix epoch time to Java Date object](http://stackoverflow.com/questions/535004/unix-epoch-time-to-java-date-object) – Basil Bourque Aug 03 '14 at 04:22

4 Answers4

4

if your UNIX time stamp is of 10 digit then it does not include milliseconds so do this first 1386889262*1000 and if its 13 digit then it includes milliseconds also then you do not have to multiply unix timestamp with 1000. In Kotlin we can use this function:

val unix=1386889262*1000 /*if time stamp is of 10 digit*/
val dateFormat = SimpleDateFormat("dd-MM-yy HH:mm:ss");
val dt =  Date(unix);
textview.settext(dateFormat.format(dt))
  • 1. Incorrect. The 10 digit value exactly denotes *seconds* since the epoch. 2. Prefer to use a library method for time conversions — even one as simple as converting from seconds to milliseconds. And never ever use `SimpleDateFormat`. That class is a notorious troublemaker and long outdated. – Ole V.V. Dec 18 '19 at 16:59
  • then what should we user other than SimpleDateFormat?@OleV.V. – Anshul Garg Jan 21 '20 at 07:14
  • Thanks for the interest. Today it’s recommended (as I think most professionals also do) to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) as in [the answer by Basil Bourque](https://stackoverflow.com/a/47140404/5772882). The class for formatting is `DateTimeFormatter`. In short: `Instant.ofEpochSecond(1_386_889_262L) .atZone(ZoneId.of("Asia/Kolkata")) .format(DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss"))` yields `13-12-13 04:31:02`. Most (not all) format patterns used with `SimpleDateFormat` work with `DateTimeFormatter` too. – Ole V.V. Jan 21 '20 at 17:28
  • For me, I must make it double. `unix = 1386889262 * 1000L` – Rami Alloush Sep 04 '20 at 03:27
3

UNIX timestamp should be in milliseconds so multiply the Long value by 1000. So your value 1386889262 would be 1386889262000:

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
2

tl;dr

Instant.ofEpochSecond( 1386889262L )
       .atZone( ZoneId.of( "Pacific/Auckland" ) )
       .toLocalDate()
       .toString()

java.time

You appear to have a count of whole seconds from the epoch reference date of first moment of 1970 in UTC, 1970-01-01T00:00:00Z.

The modern approach uses the java.time classes that supplant the troublesome old date-time classes bundled with the earliest versions of Java. For older Android see the ThreeTen-Backport and ThreeTenABP projects.

An Instant represents a point on the timeline in UTC with a resolution of nanoseconds (up to nine digits of decimal fraction).

Instant instant = Instant.ofEpochSecond( 1386889262L ) ; 

To generate a String representing this moment, call toString.

String output = instant.toString() ; 

Determining a date requires a time zone. For any given moment, the date varies around the globe by zone. Assign a ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Extract a date-only value for your purposes.

LocalDate ld = zdt.toLocalDate() ;

Generate a String.

String output = ld.toString() ;

For other formats in your String, search Stack Overflow for DateTimeFormatter.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Your timestamp or epoch time seems in sec "1386889262". You have to do something like this:

long date1 =  1386889262*1000;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm");
Date dt = new Date(date1);
datedisplay.setText(dateFormat.format(dt));

You can also get timestamp in java via

new Date().getTime() ;

It returns a long value.

Manish Jaiswal
  • 442
  • 5
  • 19
  • 1
    This Code uses troublesome old date-time classes that are now legacy and should be avoided. Supplanted by the java.time classes. For older Android see the *ThreeTen-Backport* and *ThreeTenABP* projects. – Basil Bourque Nov 06 '17 at 15:33