-2

I have UTC time in long value = 1555415100000L. Now I want to convert to local time in different time zone.

example:

1555415100000L = 2019/04/16 18:45 (GMT+7)

1555415100000L = 2019/04/16 14:45 (GMT+3) ...

Do you have any suggestion to solve this problem?

Zappy.Mans
  • 551
  • 1
  • 7
  • 19
  • The long value are the time represented in milliseconds. here is what you need: https://stackoverflow.com/questions/31292032/converting-from-milliseconds-to-utc-time-in-java – acarlstein Apr 16 '19 at 15:50
  • you can create a `Date` object out of that and then you can use `Calendar` object in Android with a `Locale` of your choosing, after which you can use `DateFormat` to properly display it on the screen. A bit of research using these keywords will help. – Vucko Apr 16 '19 at 15:50
  • @acarlstein: your solution doesn't work for me – Zappy.Mans Apr 16 '19 at 16:16

1 Answers1

1

I wrote this method. This method returns specific GMT as formatted String. You should give time in millisecond and GMT value to this method.

private String getSpecificGmtDate(long timeMillis, int gmt) {
    long time = timeMillis + (gmt * 1000 * 60 * 60);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    return (sdf.format(new Date(time)) + " (GMT " + gmt + ")");
}

output:

System.out.println(getSpecificGmtDate(1555415100000L, 0));
16/04/2019 11:45 (GMT 0)
System.out.println(getSpecificGmtDate(1555415100000L, 3));
16/04/2019 14:45 (GMT 3)
System.out.println(getSpecificGmtDate(1555415100000L, -3));
16/04/2019 08:45 (GMT -3)
Beyazid
  • 1,795
  • 1
  • 15
  • 28
  • How about time zone: GMT+5:30 – Zappy.Mans Apr 17 '19 at 02:05
  • you can add easily. Its basic programming logic. `private String getSpecificGmtDate(long timeMillis, int gmtHour, int gmtMin) { if(gmtMin==0){gmtMin=60;} long time = timeMillis + (gmtHour * 1000 * 60 * gmtMin); ....` – Beyazid Apr 17 '19 at 06:39