Last active
August 29, 2015 14:28
-
-
Save recoverlee/fb5ce6a76757ee5d9cd6 to your computer and use it in GitHub Desktop.
Get Local Time
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
/* | |
기존의 획득한 시간 정보를 timezone이 변경이 되더라도 동일하게 표기하기 위해 사용되는 함수 | |
long beforeTime = System.currentTimeMillis(); | |
long beforeOffSet = TimeZone.getDefault().getOffset(beforeTime); | |
// 중간에 TimeZone이 변경되더라도 beforeTime과 beforeOffSet을 알고 있으면 변경된 timezone에서의 동일하게 표기되는 년/월/일/시/분/초 정보인 epoch time을 획득할 수 있다. | |
long afterTime = getCurrentLocalTime(beforeTime, beforeOffSet); | |
*/ | |
public static long getCurrentLocalTime(long localTime, long timeOffset) { | |
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); | |
calendar.setTimeInMillis(localTime + timeOffset); | |
int year = calendar.get(Calendar.YEAR); | |
int month = calendar.get(Calendar.MONTH); | |
int day = calendar.get(Calendar.DAY_OF_MONTH); | |
int hour = calendar.get(Calendar.HOUR_OF_DAY); | |
int minute = calendar.get(Calendar.MINUTE); | |
int second = calendar.get(Calendar.SECOND); | |
int milliSecond = calendar.get(Calendar.MILLISECOND); | |
calendar.setTimeZone(TimeZone.getDefault()); | |
calendar.set(Calendar.YEAR, year); | |
calendar.set(Calendar.MONTH, month); | |
calendar.set(Calendar.DAY_OF_MONTH, day); | |
calendar.set(Calendar.HOUR_OF_DAY, hour); | |
calendar.set(Calendar.MINUTE, minute); | |
calendar.set(Calendar.SECOND, second); | |
calendar.set(Calendar.MILLISECOND, milliSecond); | |
return calendar.getTimeInMillis(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment