简体   繁体   English

如何倒计时到UTC的毫秒数

[英]How to countdown to milliseconds in UTC

I'm using an api which fetches all upcoming movies and all dates in the api are stored in milliseconds, when I try to countdown from today's date to the movie's release date, the countdown is in my local timezone. 我正在使用api来获取所有即将上映的电影,api中的所有日期都以毫秒为单位存储,当我尝试从今天的日期倒计时到电影的发布日期时,倒计时在我当地的时区。 For example, I'm in the east coast and when the countdown reaches 8 PM it counts as a new day and the number of days left decrements 例如,我在东海岸,当倒计时到达晚上8点时,它算作新的一天,剩下的天数减少了

Here's a screenshot: 这是一个截图:

在此输入图像描述

As you can see, this was the countdown when it was around 9 PM in the east coast, so at 8 PM it would have been 24 hours left, how can I change this? 正如你所看到的那样,这是在东海岸的晚上9点左右的倒计时,所以在晚上8点还剩下24小时,我怎么能改变这个呢? Of course I want it to reset the day at midnight, not at 8 PM. 当然我希望它重置午夜的一天,而不是晚上8点。 Thank you! 谢谢!

Here's my code: 这是我的代码:

  public long getMillisInGMT(long date) {
    // We don't care what date format we're going to format to
    SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    String formatted = formatter.format(new Date(date));
    try {
        return formatter.parse(formatted).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return 0;
}

public void initCountdown(_Release release) {
    long releaseDate = release.getDate();
    long timeTillRelease = getMillisInGMT(releaseDate) - getMillisInGMT(Calendar.getInstance().getTimeInMillis());
    mCountdownTimer = new CountDownTimer(timeTillRelease, 1000) { // adjust the milli seconds here
        public void onTick(long millisUntilFinished) {
            // 1 sec = 1000 millis
            long seconds = millisUntilFinished / 1000;
            long minutes = seconds / 60;
            long hours = minutes / 60;
            long days = hours / 24;
            mDaysTxt.setText(String.valueOf(days));
            mHoursText.setText(String.valueOf(hours % 24));
            mMinutesText.setText(String.valueOf(minutes % 60));
            mSecondsText.setText(String.valueOf(seconds % 60));
        }

        public void onFinish() {
            mHeaderReleasesTitle.setText("Released on");
            mOutNowTxt.setVisibility(View.VISIBLE);
            mCountdownLayout.setVisibility(View.GONE);
        }
    };
    mCountdownTimer.start();
}

I'm not 100% sure where your error is coming from, but I know that I've run into a lot of problems using the Date and Calendar libraries. 我不是100%肯定你的错误来自哪里,但我知道我使用日期和日历库遇到了很多问题。 By far the best time library out there is the Joda-Time library. 到目前为止,最好的时间库是Joda-Time库。 If it were me, I'd replace the library and see if that fixed things before troubleshooting further. 如果是我,我会更换磁带库,看看是否修复了这些问题,然后再进行故障排除。

To get the current UTC (GMT) time you would use the DateTime library like below: 要获得当前的UTC(GMT)时间,您可以使用DateTime库,如下所示:

DateTime.now().withZone(DateTimeZone.UTC)

To convert the Release millis to UTC you would use the DateTime library like below: 要将Release millis转换为UTC,您可以使用DateTime库,如下所示:

new DateTime(dateInMillis).withZone(DateTimeZone.UTC);

I know it's not a direct answer to your question, but I can't tell you how many odd issues I ran into while using the Date and Calendar libraries. 我知道这不是你问题的直接答案,但我无法告诉你在使用日期和日历库时遇到了多少奇怪的问题。 Joda-Time fixes most issues. Joda-Time解决了大多数问题。 It's an amazing library. 这是一个了不起的图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM