웹 응용 프로그램에서 모든 최종 사용자의 날짜 정보를 UTC 형식으로 데이터베이스에 저장하고 표시하기 전에 UTC 날짜를 시간대로 변환합니다. 그들의 선택의.로컬 시간을 UTC로 변환하는 방법 DayLightSaving 인수
나는 (저장 동안) UTC 시간에의 현지을 변환하려면이 방법을 사용하고 있습니다 :
public static Date getUTCDateFromStringAndTimezone(String inputDate, TimeZone timezone){
Date date
date = new Date(inputDate)
print("input local date ---> " + date);
//Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
long msFromEpochGmt = date.getTime()
//gives you the current offset in ms from GMT at the current date
int offsetFromUTC = timezone.getOffset(msFromEpochGmt)*(-1) //this (-1) forces addition or subtraction whatever is reqd to make UTC
print("offsetFromUTC ---> " + offsetFromUTC)
//create a new calendar in GMT timezone, set to this date and add the offset
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"))
gmtCal.setTime(date)
gmtCal.add(Calendar.MILLISECOND, offsetFromUTC)
return gmtCal.getTime()
}
그리고 지역에 UTC 날짜를 변환하는이 방법을 (보여 주면서) :
public static String getLocalDateFromUTCDateAndTimezone(Date utcDate, TimeZone timezone, DateFormat formatter) {
printf ("input utc date ---> " + utcDate)
//Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
long msFromEpochGmt = utcDate.getTime()
//gives you the current offset in ms from GMT at the current date
int offsetFromUTC = timezone.getOffset(msFromEpochGmt)
print("offsetFromUTC ---> " + offsetFromUTC)
//create a new calendar in GMT timezone, set to this date and add the offset
Calendar localCal = Calendar.getInstance(timezone)
localCal.setTime(utcDate)
localCal.add(Calendar.MILLISECOND, offsetFromUTC)
return formatter.format(localCal.getTime())
}
내 질문은 최종 사용자가 DST 영역 내에있는 경우 로컬 시계 시간을 완벽하게 수용 할 수있는 방법을 개선하는 방법입니다.
'TimeZone.getAvailableIDs()'목록에있는'America/Chicago'와 같은 시간대 ID를 사용하면 DST에 대해 걱정할 필요가 없다는 뜻입니까? – tusar
아니면,'timezone.inDaylightTime (date)'를 체크하고'timezone.getDSTSavings()'에서 오프셋을 늘리거나 줄여야합니까? – tusar
@tusar America/Chicago의 TimeZone을 가져 와서 Calendar/SimpleDateFormat로 설정하면 Calendar/SimpleDateFormat가 DST를 염두에두고 작업하게됩니다. –