From 81adcaddbe0c5c63d36734e4142fa2d37e6e3374 Mon Sep 17 00:00:00 2001 From: Salakar Date: Tue, 25 Sep 2018 18:58:30 +0100 Subject: [PATCH] [android][utils] timestampToUTC method --- .../main/java/io/invertase/firebase/Utils.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/io/invertase/firebase/Utils.java b/android/src/main/java/io/invertase/firebase/Utils.java index f71eebcc..03eb1b0c 100644 --- a/android/src/main/java/io/invertase/firebase/Utils.java +++ b/android/src/main/java/io/invertase/firebase/Utils.java @@ -12,8 +12,13 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.common.LifecycleState; import com.facebook.react.modules.core.DeviceEventManagerModule; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.TimeZone; import javax.annotation.Nullable; @@ -22,6 +27,14 @@ import javax.annotation.Nullable; public class Utils { private static final String TAG = "Utils"; + public static String timestampToUTC(long timestamp) { + Calendar calendar = Calendar.getInstance(); + Date date = new Date((timestamp + calendar.getTimeZone().getOffset(timestamp)) * 1000); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format.format(date); + } + /** * send a JS event **/ @@ -156,7 +169,7 @@ public class Utils { try { reactContext = (ReactContext) context; - } catch(ClassCastException exception) { + } catch (ClassCastException exception) { // Not react context so default to true return true; }