7

我需要CountDown Days从一个日期到第二个日期

例如

CURRENT_DATE:3/1/2013 NEXT_DATE:21/01/2013

然后它显示::17 DAYS LEFT

我实现了这样的代码

String inputDateString = "01/22/2013";
Calendar calCurr = Calendar.getInstance();
Calendar calNext = Calendar.getInstance();
calNext.setTime(new Date(inputDateString)); 

if(calNext.after(calCurr)) 
{
    long timeDiff = calNext.getTimeInMillis() - calCurr.getTimeInMillis();
    int daysLeft = (int) (timeDiff/DateUtils.DAY_IN_MILLIS);
    dni.setText("Days Left: "+daysLeft);
}
else
{
    long timeDiff = calCurr.getTimeInMillis() - calNext.getTimeInMillis();
    timeDiff = DateUtils.YEAR_IN_MILLIS - timeDiff;
    int daysLeft = (int) (timeDiff/DateUtils.DAY_IN_MILLIS);
}

有没有更好的方法来实现这些?

4

6 回答 6

5

使用Calendar方法:

String inputDateString = "01/22/2013";
Calendar calCurr = Calendar.getInstance();
Calendar day = Calendar.getInstance();
day.setTime(new SimpleDateFormat("MM/dd/yyyy").parse(inputDateString));
if(day.after(calCurr)){
               System.out.println("Days Left: " + (day.get(Calendar.DAY_OF_MONTH) -(calCurr.get(Calendar.DAY_OF_MONTH))) );
}

输出:剩余天数:17

并且要将年份增加 1 ,您可以使用 Calendar.add() 方法

        day.add(Calendar.YEAR, 1);
于 2013-01-04T11:10:57.323 回答
2

有几个库可以将日期转换为n days格式:

于 2013-01-04T11:09:10.647 回答
1

我用这个类

import android.text.format.DateUtils;

import java.util.Date;

import static android.text.format.DateUtils.FORMAT_NUMERIC_DATE;
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
import static android.text.format.DateUtils.FORMAT_SHOW_YEAR;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;

/**
 * Utilities for dealing with dates and times
 */
public class TimeUtils {

    /**
     * Get relative time for date
     *
     * @param date
     * @return relative time
     */
    public static CharSequence getRelativeTime(final Date date) {
        long now = System.currentTimeMillis();
        if (Math.abs(now - date.getTime()) > 60000)
            return DateUtils.getRelativeTimeSpanString(date.getTime(), now,
                    MINUTE_IN_MILLIS, FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR
                    | FORMAT_NUMERIC_DATE);
        else
            return "Just now";
    }
}

TimeUtils.getRelativeTime(date)返回文本,如
Just now,
2 min. 前,
2 小时前,
2 天前,
04.11.2013

于 2013-11-11T13:15:42.950 回答
1
于 2018-01-28T22:42:06.250 回答
0

我在互联网上搜索了此代码,但没有找到。虽然我回复晚了,但这将是一段有用的代码。

public static String getTimeLeft(String date) { // dateFormat = "yyyy-MM-dd"
    String[] DateSplit = date.split("-");
    int month = Integer.parseInt(DateSplit[1]) - 1, // if month is november  then subtract by 1
    year = Integer.parseInt(DateSplit[0]), day = Integer
            .parseInt(DateSplit[2]), hour = 0, minute = 0, second = 0;
    Calendar now = Calendar.getInstance();

    int sec = second - Calendar.getInstance().get(Calendar.SECOND), min = minute
            - Calendar.getInstance().get(Calendar.MINUTE), hr = hour
            - Calendar.getInstance().get(Calendar.HOUR_OF_DAY), dy = day
            - Calendar.getInstance().get(Calendar.DATE), mnth = month
            - Calendar.getInstance().get(Calendar.MONTH), daysinmnth = 32 - dy;

    Calendar end = Calendar.getInstance();

    end.set(year, month, day);

    if (mnth != 0) {
        if (dy != 0) {
            if (sec < 0) {
                sec = (sec + 60) % 60;
                min--;
            }
            if (min < 0) {
                min = (min + 60) % 60;
                hr--;
            }
            if (hr < 0) {
                hr = (hr + 24) % 24;
                dy--;
            }
            if (dy < 0) {
                dy = (dy + daysinmnth) % daysinmnth;
                mnth--;
            }
            if (mnth < 0) {
                mnth = (mnth + 12) % 12;
            }
        }
    }

    String hrtext = (hr == 1) ? "hour" : "hours", dytext = (dy == 1) ? "day"
            : "days", mnthtext = (mnth == 1) ? "month" : "months";

    if (now.after(end)) {
        return "";
    } else {
        String months = "", days = "", hours = "";
        months = (mnth > 0) ? mnth + " " + mnthtext : "";
        if (mnth <= 0) {
            days = (dy > 0) ? dy + " " + dytext : "";
            if (dy <= 0) {
                hours = (hr > 0) ? hr + " " + hrtext : "";
            }
        }
        //Log.d("DATE", months + " 1 " + days + " 2 " + hours);
        return months + days + hours;
    }
}
于 2014-03-01T15:05:55.173 回答
0

其他答案忽略时区,这可能至关重要,具体取决于您希望倒计时的准确程度。如果您不指定时区,您将获得JVM的默认值。

java.util.Date & .Calendar 类是出了名的麻烦。避开他们。使用Joda-Time或Java 8中的新java.time 包

Joda-Time 2.3中的一些代码,未经测试(在我的脑海中)。搜索 StackOverflow 以获取更多示例。

String input = "01/22/2013";
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "MM/dd/yyyy" ).withZone( timeZone );
DateTime future = formatterInput.parseDateTime( input );

DateTime now = new DateTime( timeZone );

int days = Days.daysBetween( now, future ).getDays();
Interval interval = new Interval( now, future );
Period period = new Period( interval );

对这些变量进行一些System.out.println调用并感到惊讶。

您将看到的字符串格式是ISO 8601。您也可以创建其他格式。

于 2014-03-02T11:35:22.457 回答