서버에서 일부 데이터를 얻고 있으며 해당 데이터의 일부는 날짜입니다. 날짜는 UTC 형식입니다. 을 사용하여 상대적인 시간으로 사용자에게 시간을 표시하고 getRelativeTimeSpanString
있습니다.
질문
UTC 시간을 사용자 장치에 설정된 현지 시간대로 어떻게 변환 할 수 있습니까?
이것이 내가 서버에서 날짜를 얻고 사용하는 방법입니다. relativeTimeSpanString
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).
parse("2014-05-21 12:21:41");
Date currentDate = new Date();
CharSequence cs = DateUtils.getRelativeTimeSpanString(date.getTime(), currentDate.getTime(), DateUtils.SECOND_IN_MILLIS);
이와 같은 것을 시도하십시오. 시간대와 일광 절약 시간을 고려합니다.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = sdf.parse("2014-05-21 12:21:41");
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(date) ? tz.getDSTSavings() : 0);
String result = sdf.format(date.getTime() + currentOffsetFromUTC);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다