1

我目前正在尝试在我的应用程序中加入一个用户可以输入日期的功能,然后当他们保存它时,我想计算从今天到该日期还剩多少天。我的代码如下:

public void daysLeft(String dateSent){

    String []ar = dateSent.split("[/]");
    int mDay = Integer.parseInt(ar[1]);
    int mMonth = Integer.parseInt(ar[0]);
    int mYear = Integer.parseInt(ar[2]);

    Time TimerSet = new Time();
    TimerSet.set(0, 5, 0, mDay, mMonth, mYear); //day month year
    TimerSet.normalize(true);
    long millis = TimerSet.toMillis(true);

    Time TimeNow = new Time();
    TimeNow.setToNow(); // set the date to Current Time
    TimeNow.normalize(true);
    long millis2 = TimeNow.toMillis(true);

    long millisset = millis - millis2; //subtract current from future to set the time remaining

    final int smillis = (int) (millis); //convert long to integer to display conversion results
    final int smillis2 = (int) (millis2);

    new CountDownTimer(millisset, 1000) {
        public void onTick(long millisUntilFinished) {

            mText = (TextView)findViewById(R.id.DateData);

            // decompose difference into days, hours, minutes and seconds 
            int weeks = (int) ((millisUntilFinished / 1000) / 604800);
            int days = (int) ((millisUntilFinished / 1000) / 86400);
            int hours = (int) (((millisUntilFinished / 1000) - (days * 86400)) / 3600);
            int minutes = (int) (((millisUntilFinished / 1000) 
                            - ((days * 86400) + (hours * 3600))) / 60);
            int seconds = (int) ((millisUntilFinished / 1000) % 60);
            int millicn = (int) (millisUntilFinished / 1000);

            mText.setText(" " + days);
            saveDate(String.valueOf(days));
        }

        public void onFinish() {}
    }.start();
}

当我运行此代码并输入日期 19/07/2015 时,剩余的天数会返回 760 天,它们应该是 730 天。我认为有一些计算略有偏差,非常感谢您对此的帮助。

谢谢

4

2 回答 2

1

错误在月份计算中。在 java 中,月份是从 0 开始的(0 是一月)。

因此,

int mMonth = Integer.parseInt(ar[0]);

与您的示例一样不正确,它被解释为八月。

您需要减去 1 :

int mMonth = Integer.parseInt(ar[0]) - 1;

编辑 - 更多细节

TimerSet.set(0, 5, 0, mDay, mMonth, mYear);

需要从 0 开始的一个月。7,在您的示例中,被理解为 August,这就是为什么您需要将 1 减去您解析的基于 1 的值。

于 2013-07-19T14:14:17.643 回答
0

您可以使用我的TimeSpan课程:

Calendar future = Calendar.getInstance();
future.set(2015, 6, 19); // month is zero based

Date now = Calendar.getInstance().getTime();

TimeSpan timeSpan = TimeSpan.fromMiliseconds(future.getTimeInMillis() - now.getTime());

Log.d(TAG, "Days left : " + timeSpan.getDays()); // "Days left : 730"

TimeSpan班级:

public class TimeSpan implements Comparable<TimeSpan> {
    Long days;
    Long hours;
    Long minutes;
    Long seconds;
    Long milliseconds;

    public TimeSpan() {
        days = hours = minutes = seconds = milliseconds = 0l;
    }

    public TimeSpan(long _days, long _hours, long _minutes, long _seconds, long _milliseconds) {

        long totalMS = _days * 86400000 + _hours * 3600000 + _minutes * 60000 + _seconds * 1000 + _milliseconds;

        days = Long.valueOf(totalMS / 86400000);
        totalMS -= days * 86400000;

        hours = Long.valueOf((int) totalMS / 3600000);
        totalMS -= hours * 3600000;

        minutes = Long.valueOf((int) totalMS / 60000);
        totalMS -= minutes * 60000;

        seconds = Long.valueOf((int) totalMS / 1000);
        totalMS -= seconds * 1000;

        milliseconds = Long.valueOf((int) totalMS);
    }

    public static TimeSpan fromSeconds(int _seconds) {
        return new TimeSpan(0, 0, 0, _seconds, 0);
    }

    public static TimeSpan fromMiliseconds(long _miliseconds) {
        return new TimeSpan(0, 0, 0, 0, _miliseconds);
    }

    @Override
    public String toString() {
        return Long.toString(getTotalMilliseconds());
    }

    public String toSimpleShortString() {
        if (hours > 0)
            return String.format("%d:%d:%02d", hours, minutes, seconds);
        else
            return String.format("%d:%02d", minutes, seconds);
    }

    public String toShortString() {
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    public String toLongString() {
        return String.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds);
    }

    public long getHours() {
        return hours;
    }

    public long getDays() {
        return days;
    }

    public long getMinutes() {
        return minutes;
    }

    public long getSeconds() {
        return seconds;
    }

    public long getMilliseconds() {
        return milliseconds;
    }

    public long getTotalMilliseconds() {
        return days * 86400000 + hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds;
    }

    @Override
    public int compareTo(TimeSpan another) {
        return (int) (getTotalMilliseconds() - another.getTotalMilliseconds());
    }
}
于 2013-07-19T14:37:21.973 回答