Skip to content

Instantly share code, notes, and snippets.

@amostajo
Created March 31, 2018 16:55
Show Gist options
  • Save amostajo/c7ae46e296221c1f0f2ffea1cf6360bc to your computer and use it in GitHub Desktop.
Save amostajo/c7ae46e296221c1f0f2ffea1cf6360bc to your computer and use it in GitHub Desktop.
Android's date time value fix based on timezone differences.
// Needed
import java.util.Calendar;
import java.util.TimeZone;
// Initialize calendar instance and set your datetime value
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(dateTimeValue);
// Initialize timezone with your timezone value
TimeZone tz = TimeZone.getTimeZone("America/New_York");
// Get the correct time
// Time - TimeZone difference - TimeZone Daylight Savings difference
long fixedDateTime = cal.getTimeInMillis() - tz.getRawOffset() - tz.getDSTSavings();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment