0

I'm getting time from a server in milliseconds and trying to convert it in local time. I want to pass it to my PrettyTime Using Java like this:

PrettyTime p = new PrettyTime();
long millisec = 1522034539973;

    String time = p.format(new Date(millisec));

But the result is the relative time from UTC time provided to currentTime; What I need is to convert millisec to local Time in milliseconds. Please Help!

PS: I'm using Android

Constantin N.
  • 2,739
  • 2
  • 16
  • 27

3 Answers3

0

Your code is correct.

If you are indeed directly using the millisecond value that you get from the server, then I suspect that something is coded incorrectly on the server side.

Keep in mind that Dates represent instants in time, and do not contain time zones.

Current "milliseconds" represents, as the documentation says, "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC(coordinated universal time)" - thus, the offset from the "epoch date" in milliseconds.

Dates don't have timezones until you format them for a particular zone - they just contain this millisecond offset.

So, if you are getting a time other than the one that you expect, either the server is sending the incorrect value, or PrettyTime is inferring the incorrect local timezone when you call format(). PrettyTime appears to use the currently configured Locale, so that's probably not the issue.

I suggest that you test whether the server time is the problem by testing PrettyTime with a locally generated date, something like this:

Date now = new Date();
System.out.println(p.format(now));

If you really want to convince yourself that everything is ok client-side, try this:

Date now = new Date();
long msec = now.getTime();
Date theDate = new Date(msec);
System.out.println(p.format(theDate));

also, to find out what the server is actually sending you, https://currentmillis.com can show you the current time in millis, and convert millis to human-readable formats.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Than you @GreyBeardedGeek for your help! the problem is that even when i pass directly Cureentime from http://curentmillis.com, I'm getting relative time from UTC to device CurrentTime but device timeZone can be UTC+....or UTC-..... What i need is to convert UTC to local timeZone – Constantin N. Mar 25 '18 at 19:39
0

You can try to get time from java.util.Calendar. Here is a example:

Calendar.getInstance(TimeZone.getDefault()).getTimeInMillis()

Also you can get time by specific locale:

Calendar.getInstance(Locale.getDefault()).getTimeInMillis()
Link182
  • 733
  • 6
  • 15
  • Thank you for your answer! What I need is to get user Local Timezone then I can convert UTC time to Local Time – Constantin N. Mar 25 '18 at 20:52
  • @user8773560 you can get timezone offset and add it to your timestamp. Check that answer https://stackoverflow.com/a/15068851/6193843 – Link182 Mar 25 '18 at 21:34
0

answer from Link182 not work. it still get mili of UTC.

this code work:

long mili_UTC = System.currentTimeMillis(); // ex: 1687067040633

TimeZone tz = TimeZone.getDefault();

long offsetFromUtc = tz.getOffset(mili_UTC);

// will get miliseconds of local timezone
long mili_Of_Local = mili_UTC + offsetFromUtc; // ex: 1687092240633
kawasaki
  • 1
  • 1