Android倒数基于GMT / UTC而非用户的时区 [英] Android countdown based on GMT/UTC and not the user's timezone

查看:67
本文介绍了Android倒数基于GMT / UTC而非用户的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小问题在这里。我有一个运行良好的android倒数计时器,但是问题是倒数计时器在用户/时区之间会有所不同。我的意思是,对于每个时区,我的应用程序的用户将看到一个不同的倒计时,该倒计时可能会相对于其时区更早或更晚结束。我不想要这样做,所以我希望所有这倒计时在同一时间结束发布。

small problem here. I have an android countdown timer that works fine, but the issue is the countdown timer will be different between users/timezone. By this I mean, for every timezone, users of my app will see a different countdown that may end earlier or later relative to their timezone.I don't want this, I want it so all countdowns end at the same time for THIS movie release.

我的代码:
注意:为简便起见,一些代码被省略了,请查看下面的注释以获取代码的一些解释

My code: Note: some code is omitted for brevity and please check out the comments below for some explanation of the code

        // the current date and time
        Calendar today = Calendar.getInstance();


    // release.date is in milliseconds at 12am and in GMT, example: GMT: Friday, September 29, 2017 12:00:00 AM
    long currentTimeMillis = today.getTimeInMillis();
    long expiryTime = releaseDate.date - currentTimeMillis;


    holder.mTextCountdown.setText("");
    if (holder.timer != null) {
        // Cancel if not null to stop flickering
        holder.timer.cancel();
    }
    holder.timer = new CountDownTimer(expiryTime, 500) {
        public void onTick(long millisUntilFinished) {
            long seconds = millisUntilFinished / 1000; // reminder: 1 sec = 1000 millis
            long minutes = seconds / 60;
            long hours = minutes / 60;
            long days = hours / 24;

            String dayFormat = "days";
            String hoursFormat = "hours";
            if (days == 1) {
                dayFormat = "day";
            }
            if (hours == 1) {
                hoursFormat = "hour";
            }
            String time = days + " " + dayFormat + " : " + hours % 24 + " " + hoursFormat +" : " + minutes % 60 + " : " + seconds % 60;
            holder.mTextCountdown.setText(time);
        }

        // Finished: counted down to 0
        public void onFinish() {
            holder.mTextCountdown.setText("Now out!");
        }
    }.start();

我该如何在GMT / UTC中倒数计时器,对于每个用户来说肯定会在此结束同时?

How can I count down a timer in GMT/UTC, where for every user it's sure to end at the same time?

谢谢

推荐答案

我建议您执行以下操作2件事情:

I'd recommend you to do following 2 things:


  1. 将所有时间都存储在UTC中,并将其转换为仅用于UI输出的本地时区

  2. 从服务器端同步时间(并设置时间)。无论如何,您无法控制客户端设备现在的几点钟-它可能在错误的时区,或者由于某种原因使时间服务器关闭,并且本地时间可能不正确。只需将用户设备上的时间与服务器时间同步,存储增量(如果有的话),并根据服务器时间显示倒计时。

这篇关于Android倒数基于GMT / UTC而非用户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆