Skip to content

Instantly share code, notes, and snippets.

@li2
Last active May 6, 2018 11:40
Show Gist options
  • Save li2/74208d573a4f8b42d94124628c2b3541 to your computer and use it in GitHub Desktop.
Save li2/74208d573a4f8b42d94124628c2b3541 to your computer and use it in GitHub Desktop.
UTC #tags: java
public Date utcDate(String utcTimestamp) {
try {
String format = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.parse(utcTimestamp);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public long diffToCurrent(String utcTimestamp) {
Date date = utcDate(utcTimestamp);
if (date != null) {
long signalUtcDateMillis = date.getTime();
long currentTimeMillis = System.currentTimeMillis();
return (currentTimeMillis - signalUtcDateMillis);
}
return 0;
}
Log.d(TAG, "diff " + diffToCurrent("2018-04-23T05:14:00.000"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment