在我的应用程序中,我需要将数据转换为位置时间。我有javaDate
和TimeZoneId
当我从服务中获取数据时 - 它以 UTC 的形式出现,我也得到了时区。需要转换成那个时区的时间。我创建的功能:
private static Date getDateConvertedToTimeZone(Date utcDate, String timeZoneId)
{
TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
if (timeZone.getID().equalsIgnoreCase(timeZoneId))
{
Log.d(LOG_TAG, "Good: " + timeZoneId + " " + Long.toString(utcDate.getTime()));
return new Date(utcDate.getTime() + timeZone.getRawOffset() + (timeZone.inDaylightTime(utcDate) ? timeZone.getDSTSavings() : 0));
}
Log.d(LOG_TAG, "Bad?: " + timeZoneId + " " + Long.toString(utcDate.getTime()));
LogData.d(LOG_TAG, "getDateConvertedToTimeZone bad time zone: " + timeZoneId, LogData.Priority.None);
return utcDate;
}
简单的?对。只需通过 ID 获取时区并对 Date 对象进行一些数学运算。这就是我的想法......当我解析重复调用的数据函数时,输出如下:
07-24 21:31:06.038: DEBUG/com.me.data.TripData(18886): Good: MST 1319896800000
07-24 21:31:06.042: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000
07-24 21:31:06.053: DEBUG/com.me.data.TripData(18886): Bad?: PST 1320949800000
07-24 21:31:06.057: DEBUG/com.me.data.TripData(18886): Bad?: CST 1320685200000
07-24 21:31:06.061: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.073: DEBUG/com.me.data.TripData(18886): Bad?: CST 1320645600000
07-24 21:31:06.077: DEBUG/com.me.data.TripData(18886): Good: EST 1320350400000
07-24 21:31:06.081: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.085: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.093: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321268400000
07-24 21:31:06.096: DEBUG/com.me.data.TripData(18886): Bad?: PST 1320957000000
07-24 21:31:06.108: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.116: DEBUG/com.me.data.TripData(18886): Bad?: PST 1321542000000
07-24 21:31:06.120: DEBUG/com.me.data.TripData(18886): Good: MST 1321500600000
07-24 21:31:06.124: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321286400000
07-24 21:31:06.128: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.135: DEBUG/com.me.data.TripData(18886): Good: EST 1321981200000
07-24 21:31:06.139: DEBUG/com.me.data.TripData(18886): Bad?: CST 1321884000000
07-24 21:31:06.143: DEBUG/com.me.data.TripData(18886): Bad?: PST 1321646400000
07-24 21:31:06.151: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.159: DEBUG/com.me.data.TripData(18886): Bad?: CST 1322485200000
07-24 21:31:06.163: DEBUG/com.me.data.TripData(18886): Good: EST 1321966800000
07-24 21:31:06.163: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.175: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322595000000
07-24 21:31:06.178: DEBUG/com.me.data.TripData(18886): Bad?: CST 1322244000000
07-24 21:31:06.182: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.194: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322755200000
07-24 21:31:06.194: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322683440000
07-24 21:31:06.206: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.218: DEBUG/com.me.data.TripData(18886): Good: EST 1323190800000
07-24 21:31:06.218: DEBUG/com.me.data.TripData(18886): Good: EST 1323176400000
07-24 21:31:06.221: DEBUG/com.me.data.TripData(18886): Bad?: PST 1322845200000
07-24 21:31:06.229: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323356400000
07-24 21:31:06.233: DEBUG/com.me.data.TripData(18886): Good: EST 1323212400000
07-24 21:31:06.237: DEBUG/com.me.data.TripData(18886): Good: EST 1323194400000
07-24 21:31:06.241: DEBUG/com.me.data.TripData(18886): Good: EST 1323183600000
07-24 21:31:06.245: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.311: DEBUG/com.me.data.TripData(18886): Bad?: PST 1323705600000
07-24 21:31:06.319: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323351000000
07-24 21:31:06.323: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.331: DEBUG/com.me.data.TripData(18886): Good: EST 1324054800000
07-24 21:31:06.335: DEBUG/com.me.data.TripData(18886): Good: EST 1324040400000
07-24 21:31:06.339: DEBUG/com.me.data.TripData(18886): Bad?: CST 1323961200000
07-24 21:31:06.343: DEBUG/com.me.data.TripData(18886): Bad?: PST 1323730800000
07-24 21:31:06.346: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.354: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324303200000
07-24 21:31:06.358: DEBUG/com.me.data.TripData(18886): Good: EST 1324062000000
07-24 21:31:06.362: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.370: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324603800000
07-24 21:31:06.374: DEBUG/com.me.data.TripData(18886): Bad?: PST 1324306800000
07-24 21:31:06.378: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.385: DEBUG/com.me.data.TripData(18886): Good: EST 1324962000000
07-24 21:31:06.389: DEBUG/com.me.data.TripData(18886): Bad?: CST 1324620000000
07-24 21:31:06.393: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.405: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325163600000
07-24 21:31:06.413: DEBUG/com.me.data.TripData(18886): Good: EST 1324962000000
07-24 21:31:06.413: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.421: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325768400000
07-24 21:31:06.425: DEBUG/com.me.data.TripData(18886): Good: EST 1325633400000
07-24 21:31:06.428: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.436: DEBUG/com.me.data.TripData(18886): Good: EST 1325599200000
07-24 21:31:06.440: DEBUG/com.me.data.TripData(18886): Good: EST 1325178000000
07-24 21:31:06.444: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.448: DEBUG/com.me.data.TripData(18886): Bad?: PST 1326290400000
07-24 21:31:06.456: DEBUG/com.me.data.TripData(18886): Bad?: CST 1325829600000
07-24 21:31:06.460: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.468: DEBUG/com.me.data.TripData(18886): Good: MST 1326178800000
07-24 21:31:06.471: DEBUG/com.me.data.TripData(18886): Bad?: PST 1326121200000
07-24 21:31:06.475: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.483: DEBUG/com.me.data.TripData(18886): Good: EST 1326603600000
07-24 21:31:06.487: DEBUG/com.me.data.TripData(18886): Good: MST 1326265200000
07-24 21:31:06.491: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000
07-24 21:31:06.499: DEBUG/com.me.data.TripData(18886): Good: EST 1326801600000
07-24 21:31:06.507: DEBUG/com.me.data.TripData(18886): Good: EST 1326690060000
07-24 21:31:06.510: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.518: DEBUG/com.me.data.TripData(18886): Bad?: PST 1327064400000
07-24 21:31:06.522: DEBUG/com.me.data.TripData(18886): Good: MST 1327019400000
07-24 21:31:06.526: DEBUG/com.me.data.TripData(18886): Bad?: CST 1326837600000
07-24 21:31:06.530: DEBUG/com.me.data.TripData(18886): Bad?: CST 1326834060000
07-24 21:31:06.585: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.608: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327672800000
07-24 21:31:06.616: DEBUG/com.me.data.TripData(18886): Good: EST 1327496400000
07-24 21:31:06.620: DEBUG/com.me.data.TripData(18886): Good: EST -2208970800000
07-24 21:31:06.628: DEBUG/com.me.data.TripData(18886): Good: EST 1327467600000
07-24 21:31:06.632: DEBUG/com.me.data.TripData(18886): Good: MST 1327042800000
07-24 21:31:06.632: DEBUG/com.me.data.TripData(18886): Bad?: PST -2208960000000
07-24 21:31:06.643: DEBUG/com.me.data.TripData(18886): Good: MST 1327926600000
07-24 21:31:06.647: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327616100000
07-24 21:31:06.651: DEBUG/com.me.data.TripData(18886): Bad?: CST -2208967200000
07-24 21:31:06.659: DEBUG/com.me.data.TripData(18886): Good: EST 1328245200000
07-24 21:31:06.663: DEBUG/com.me.data.TripData(18886): Good: MST 1327906800000
07-24 21:31:06.667: DEBUG/com.me.data.TripData(18886): Good: MST -2208963600000
07-24 21:31:06.678: DEBUG/com.me.data.TripData(18886): Good: EST 1327060800000
07-24 21:31:06.678: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327060800000
07-24 21:31:06.682: DEBUG/com.me.data.TripData(18886): Bad?: CST 1327060800000