android - 从当前时区转换为 UTC 减量 2 小时而不是 1 小时

标签 android datetime datetimeoffset android-date java-time

我想将日期从我当前的时区转换为 UTC。

我无法理解结果。

代码:

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 回来?

最佳答案

问题是夏令时。 UTC 没有它,如果你有,它会在一年中的部分时间增加 1 小时的差异。

关于android - 从当前时区转换为 UTC 减量 2 小时而不是 1 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735863/

相关文章:

android - 如何使 Android 搜索栏顺利移动?

android - 单元测试 : How to verify and mock onCompleted for an Observable in RxJava within Android

c# - 查找特定时间范围内的时间发生和持续时间

java - 将具有给定偏移量的日期时间转换为 UTC Java

asp.net - 当 DataBind 到 Nullable DateTimeOffset 并且字段为 NULL 时,指定的强制转换无效

android - 测量 ViewPager

android - ionic 标签菜单

datetime - 带有时区信息的 OpenAPI DateTime

python - 为什么在调用 '.values' 时 pd.Timestamp 转换为 np.datetime64 ?

c# - 具有特定时区的 Javascript 整数到日期时间