我想将我的当前时区的日期转换为UTC。从当前的TimeZone转换为UTC递减2小时而不是1
结果对我来说是不可理解的。
代码:
public static String convertToUTC(String dateStr) throws ParseException
{
Log.i("myDateFunctions", "the input param is:"+dateStr);
String uTCDateStr;
Date pickedDate = stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
Log.i("myDateFunctions", "the input param after it is converted to Date:"+pickedDate);
TimeZone tz = TimeZone.getDefault();
Date now = new Date();
Log.i("myDateFunctions:", "my current Timezone:"+tz.getDisplayName()+" +"+(tz.getOffset(now.getTime())/3600000));
// Convert to UTC
SimpleDateFormat converter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setTimeZone(TimeZone.getTimeZone("UTC"));
uTCDateStr = converter.format(pickedDate);
Log.i("myDateFunctions", "the output, after i converted to UTC timezone:"+uTCDateStr);
return uTCDateStr;
}
而且logcat的结果是:
03-29 20:31:46.804: I/myDateFunctions(18413): the input param is:2014-04-29 20:00:00
03-29 20:31:47.005: I/myDateFunctions(18413): the input param after it is converted to Date:Tue Apr 29 20:00:00 CEST 2014
03-29 20:31:47.005: I/myDateFunctions:(18413): my current Timezone:Central European Time +1
03-29 20:31:47.005: I/myDateFunctions(18413): the output, after i converted to UTC timezone:2014-04-29 18:00:00
正如你可以看到: 我的时区为CET (GMT + 1)
那么,为什么,如果我的输入是20:00我回到18:00而不是19:00?
您的时区是否有夏令时?如果是这样,那可能会增加1小时到转换(UTC不)。 –
我总是wan why为什么ppl在stackoverflw害怕给出答案。也许是因为downvotes?那么,你是对的。请将此作为答案写下,以便我可以接受。 –
我在猜测时使用了一条评论,因此查看列表的人不会认为它已被回答。 –