Last active
April 19, 2018 06:56
-
-
Save SquirrelMobile/42ff6584b037d153e514c0c894c5bc5b to your computer and use it in GitHub Desktop.
Get the Timezone including daylight saving time on Android Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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