【问题标题】:DayLightSavingMode issue in AndroidAndroid 中的 DayLightSavingMode 问题
【发布时间】:2016-10-19 04:20:32
【问题描述】:

我知道这个问题问了很多次,但我没有从中得到解决方案。

我正在尝试将 Datetime 转换为 UTC-0。

我的后端是 microsoft sharepoint O365。

我正在共享点列表中添加记录。

我在注册时存储用户时区。因此,现在每当用户登录时,他们都可以看到在注册时存储的所选时区中包含日期和时间的所有数据。

现在我的意思是用户注册的时区是亚洲/加尔各答。

现在,如果他使用其他国家/地区设备(例如伦敦)登录。然后他可以看到在注册时选择的亚洲/加尔各答的所有数据日期时间。

例如用户在注册时注册的时区,如 GMT+5:30,那么如果用户移动时区是他们在 GMT+5:30 DateTime 中可以看到的任何时区

如果他从应用程序添加任何数据,那么它也会存储为亚洲/加尔各答时区,如果他在伦敦使用 Mobile,则日期时间不是当前的移动时区。

如何解决?

我的代码如下:

    public  String getDateAndTimeToUserFormat()
{

    String date;
    TimeZone timeZone = TimeZone.getTimeZone("Indian Standard Time");

    Calendar calendar = Calendar.getInstance(timeZone);


    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat(serverdateFormat, Locale.US);
   simpleDateFormat.setTimeZone(timeZone);

date= simpleDateFormat.format(calendar.getTime());

Log.i("TAG","UserTimezone=>"+this.timeZone);
Log.i("TAG","Time zone: " + timeZone.getID());

Log.i("TAG","default time zone: " + TimeZone.getDefault());
Log.i("TAG","default time : " + Calendar.getInstance().getTime());
Log.i("TAG","UTC:     " + date);
Log.i("TAG","Default: " + calendar.getTime());



    return date;

}

输出: UserTimezone=>印度标准时间 时区: GMT

默认时区:非洲/卡萨布兰卡

默认时间: 2016 年 6 月 17 日星期五 12:53:55 GMT+00:00

UTC: 2016-06-17T12:53:55Z

默认值: 2016 年 6 月 17 日星期五 12:53:55 GMT+00:00

【问题讨论】:

  • 你应该显示你得到了什么时间,哪些不是你所期望的。猜测一下,如果您想要 UTC,那么与伦敦相差 1 小时是正确的;如果您在伦敦想要“观看时间”,那么您不想要UTC:大概您需要将印度时间(到UTC)转换,然后再转换伦敦时间。
  • @TripeHound 实际上我正在尝试在共享点中添加记录,因为我现在有 UTC-0 时区,当我在那里添加它的显示 1 小时加时间但我想显示相同的时间。意思是如果它的夏令时模式,那为什么我不能得到准确的时间?
  • @如果我将共享点的时区设置更改为 UTC+5.30,那么它会显示我在其中插入的确切时间。但如果我将其更改为 UTC-0,那么它应该显示我正在添加的时间时间。
  • 再次,为清楚起见,在您的问题中编辑一些具体示例...例如,我不知道您是否说问题在于上面的代码没有生成正确的 UTC 时间,或者 - 生成正确的 UTC 时间 - 当您将其存储在 Sharepoint 中(问题中根本没有提到!)然后 Sharepoint 显示它“错误”。如果是后者,Sharepoint 可能会以“本地时间”显示它,其中 [正确] 包括 DST 偏移量。
  • @TripeHound 简而言之,我正在添加 UTC 时间,那么它应该显示与我正在添加的相同。

标签: android datetime sharepoint utc dst


【解决方案1】:

我终于有了满足我需求的代码。

 public String getDateTimeToUTC() {

        String result=convertToUTCDateTime(convertToUserDateTime(this.timeZone));

    return result;

}

/**
 *
 * @param userTimeZone : is contain String Of User Selected TimeZone
 * @return Date: it contain DateTime of UserSelected TimeZone
 */


 public Date convertToUserDateTime(String userTimeZone)
{ Date parsed = null;
TimeZone timeZone = TimeZone.getTimeZone(userTimeZone);

Calendar calendar = Calendar.getInstance(timeZone);

SimpleDateFormat simpleDateFormat =
        new SimpleDateFormat(serverdateFormat, Locale.US);
simpleDateFormat.setTimeZone(timeZone);
String date = simpleDateFormat.format(calendar.getTime());
try {
parsed=simpleDateFormat.parse(date);

    Log.i("TAG", "default time zone: " + TimeZone.getDefault().getID());
    Log.i("TAG", "User Selected Timezone=>" + timeZone.getID());
    Log.i("TAG", "default Device DateTime : " + Calendar.getInstance().getTime());
    Log.i("TAG","User selected  DateTime=>"+date);


} catch (ParseException e) {
    e.printStackTrace();
}
return parsed ;
}    /**
 * @param userZoneDateTime : is contain User Selected TimeZone Date
 * @return String : Return UTC-0 DateTime String From User Selected DateTimeZone
 */
public  String convertToUTCDateTime(Date userZoneDateTime)
{
    String result;
    TimeZone timeZone1 = TimeZone.getTimeZone("UTC");

    SimpleDateFormat simpleDateFormat1 =
            new SimpleDateFormat(serverdateFormat, Locale.US);
    simpleDateFormat1.setTimeZone(timeZone1);


        result = simpleDateFormat1.format(userZoneDateTime);
        Log.i("TAG", "UTC Time zone: " + timeZone1.getID());
        Log.i("TAG", "UTC:     " + result);

    return result;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 2011-11-16
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多