【问题标题】:correct format of time正确的时间格式
【发布时间】:2018-09-16 09:45:44
【问题描述】:

我写了一个倒数计时器,我为此设置了时间,当我显示它的设置小时+1的时间时,我如何在此处更正它是我的代码

    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
    future=dateFormat.parse("2018-09-25 15:00:00");   
    Date now=new Date();

    if (!now.after(future)) {
        long diff = future.getTime() - now.getTime();
        long days = diff / (24 * 60 * 60 * 1000);
        diff -= days * (24 * 60 * 60 * 1000);
        long hours = diff / (60 * 60 * 1000);
        diff -= hours *( 60 * 60 * 1000);
    }

…… 例如,如果它应该从 1 天 13:00:00 开始计算,则从 14 开始计算

【问题讨论】:

  • 如果你想要一个倒数计时器,你应该使用 CountDownTimer 类。更多信息请参考:link
  • 顺便考虑扔掉长期过时且臭名昭著的麻烦SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。
  • 我无法重现您的问题。在我的时区(欧洲/哥本哈根)的 9 月 16 日 12:40,我有 9 天 2 小时,这似乎是 9 月 25 日 15:00 之前的正确时间。我猜这是一个时间区问题。您所在时区的夏令时(夏令时)是否在 9 月 25 日之前(或当天)结束?您您希望您的计时器在夏季结束时表现如何?
  • 如果我将时区设置为亚洲/德黑兰,例如,我似乎重现了您的问题,我得到的时钟时间比预期的多 1 小时。不过,问题是这是否不正确,因为当时钟倒转时,晚上还有 1 小时。伊朗的夏令时将于今年 9 月 22 日结束。
  • 感谢哥们,我的代码中没有考虑到这一点

标签: java android timer countdown


【解决方案1】:

希望这可以帮助你所有你需要做的就是将时间转换为毫秒.. 你所要做的就是使用计时器。更新标签中剩余的时间。 这是将 dateTimet 转换为毫秒的示例代码。

String myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();


            CountDownTimer    countDownTimer = new CountDownTimer(millis , 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {

                            if (getActivity() != null && !getActivity().isFinishing()) {
                                expiryLabel.setText(getResources().getString(R.string.expiresIn,
                                        " " +formatTime(millisUntilFinished));
                            }
                        }

                        @Override
                        public void onFinish() {

                            if (getActivity() != null && !getActivity().isFinishing()) {

                            // do what ever you want
                            }
                        }
                    };
                    countDownTimer.start();

                }




 public static String formatTime(long millis) {
        String output = "00:00:00";
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 60;

        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);
        String hoursD = String.valueOf(hours);

        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
        if (hours < 10)
            hoursD = "0" + hours;

        output = hoursD + ":" + minutesD + ":" + secondsD;



        return output;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多