java - 我如何编写一个每个人都看到的公共(public)计时器?

标签 java android timer long-integer

我想在我的应用程序中实现一个计时器,它会显示新更新的发布时间。 重要的是,使用我的应用程序的每个人都会看到相同的计时器以及相同的剩余时间。

有人知道如何存档吗?

更新:

到目前为止我尝试过:

SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
        formatter.setLenient(false);


        String endTime = "11.10.2019, 15:23:00";

        Date endDate;
        try {
            endDate = formatter.parse(endTime);
            timer = endDate.getTime();

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        startTime = System.currentTimeMillis();

        diff = timer - startTime;


        new CountDownTimer(timer, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                startTime=startTime-1;
                long serverUptimeSeconds =
                        (millisUntilFinished - startTime) / 1000;




                String  hms =  (String.format("%d", serverUptimeSeconds / 86400)+"d "+String.format("%d", (serverUptimeSeconds % 86400) / 3600)+"h "+ String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60))+"m " + String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60)+"s ";
                counter.setTitle(hms);

            }

它根据我写入的日期和时间显示剩余时间

String endTime = "11.10.2019, 15:23:00";

以及用户时区。

我怎样才能让每个人看到的剩余时间不是基于他们的时区,而是基于我的时区:GMT+2?

最佳答案

您只能获取一个时区的时间。所有用户都会看到相同的时间结果,与用户的时区无关

TimeZone tz = TimeZone.getTimeZone("GMT+02:00");
Calendar c = Calendar.getInstance(tz);
String time = String.format("%02d" , c.get(Calendar.HOUR_OF_DAY))+":"+
        String.format("%02d" , c.get(Calendar.MINUTE))+":"+
        String.format("%02d" , c.get(Calendar.SECOND))+":"+
        String.format("%03d" , c.get(Calendar.MILLISECOND));

关于java - 我如何编写一个每个人都看到的公共(public)计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334953/

相关文章:

java - Spring Data Neo4j @GraphId 和 @Index

Android actionBar导航模式列表样式

javascript - 如何在 2 次之间运行 js 函数

java - 通过热键执行相应操作时动画按钮

java - JPA OneToOne 加入问题

java - 继承的方法是否计入 Android 中的 Dex 方法限制?

java - 如何在 JSONObject 中处理来自 RESTful API 的 `null` 字符串?

java - 在 Android 应用程序中如何从 google appengine 下载 blob

ios - 因 NSException 类型的未捕获异常而终止 Timer Swift 崩溃

ios - 如何在 onReceive 定时器关闭 SwiftUI iOS 中导航另一个 View