D日计数器Java

时间:2016-08-03 08:25:45

标签: java android

我正在尝试开发一个Android应用程序,显示指定的未来日期剩余的年数,月数和天数。但是,我正在使用值:/

此处,计算2018/06/18年之前的剩余年,月和日数:计算剩余月数非常困难,因为每个月可以有不同的天数。所以我相信我在尝试获得剩下的几个月时犯了一个错误。然而,我的结果令人沮丧:我有41年,01个月,消极的价值天......

public void countDownStart() {
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            long diff;
            long years=0;
            long months=0;
            long  days=0;
            long temp_days=0;
            int current_month;

            Calendar futureDate = Calendar.getInstance();
            futureDate.set(Calendar.DAY_OF_MONTH, 18);
            futureDate.set(Calendar.MONTH, 6);
            futureDate.set(Calendar.YEAR, 2018);
            Calendar referenceDate = Calendar.getInstance();

            Calendar currentDate = Calendar.getInstance();

            if (futureDate.after(currentDate)) {
                diff = futureDate.getTimeInMillis() - currentDate.getTimeInMillis();
                days = diff/(1000*60*60*24);
                years = diff / (1000 * 60 * 60 * 24 * 365);
                diff = diff - years * 1000 * 60 * 60 * 24 * 365;
                months = 0;
                boolean flag = true;
                current_month = referenceDate.get(Calendar.MONTH);
                while (flag) {
                    switch (current_month) {
                        case 0:
                        case 2:
                        case 4:
                        case 6:
                        case 7:
                        case 9:
                        case 11:
                            diff = diff - 1000 * 60 * 60 * 24 * 31;
                            break;
                        case 1:
                            diff = diff - 1000 * 60 * 60 * 24 * 28;
                            break;
                        case 3:
                        case 5:
                        case 8:
                        case 10:
                            diff = diff - 1000 * 60 * 60 * 24 * 31;
                            break;
                    }
                    months = months + 1;
                    referenceDate.add(Calendar.MONTH, 1);
                    current_month=referenceDate.get(Calendar.MONTH);
                    temp_days = diff / 1000 * 60 * 60 * 24;

                    if (temp_days <= 31) {
                        int next_month = referenceDate.get(Calendar.MONTH);
                        int days_in_month;

                        if (next_month == 0 || next_month == 2 || next_month == 4 || next_month == 6 ||
                                next_month == 7 || next_month == 9 || next_month == 11) {
                            days_in_month = 31;
                        } else if (next_month == 1) {
                            days_in_month = 28;
                        } else {
                            days_in_month = 30;
                        }

                        if (temp_days < days_in_month) {
                            flag = false;
                            //days = temp_days;
                        }
                    }
                }


            }
            else
            {
                years=0;
                months=0;
                days=0;
            }
            txtTimerYear.setText(""+String.format("%02d",years));
            txtTimerMonth.setText(""+String.format("%02d",months));
            txtTimerDay.setText(""+String.format("%03d",days));

        }
    };
    handler.postDelayed(runnable,1*1000);
}

}

我真的很感谢你的帮助!! 谢谢!

1 个答案:

答案 0 :(得分:1)

使用Joda Time库。它有一些方法可以直接为您提供数月,数天等等。并且它会关注30或31天的月份。