Skip to content

Instantly share code, notes, and snippets.

@SquirrelMobile
Last active April 19, 2018 06:56
Show Gist options
  • Save SquirrelMobile/42ff6584b037d153e514c0c894c5bc5b to your computer and use it in GitHub Desktop.
Save SquirrelMobile/42ff6584b037d153e514c0c894c5bc5b to your computer and use it in GitHub Desktop.
Get the Timezone including daylight saving time on Android Java
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
// Get the "real" offset timezone in hours
public static int getTimeZone(){
Calendar mCalendar = new GregorianCalendar();
TimeZone mTimeZone = mCalendar.getTimeZone();
// Get the timezone offset with the function getRawOffset and add the daylight savings
int mGMTOffset = mTimeZone.getRawOffset() + (mTimeZone.inDaylightTime(new Date()) ? mTimeZone.getDSTSavings() : 0);
// Return the converted offset to hours
return (int) TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment