1
votes

I am writing a program in which i am using count down until a specific date and time, and in result i am getting countdown in hours,minutes,seconds format, but i also want to show number of days

getting this:

       Time remaining: 630 hours, 57 minutes, 25 seconds

need this:

       Time remaining: 26 days, 11 hours, 57 minutes, 25 seconds

Activity code:-

    public void onTick(long millisUntilFinished) {
                String result = ToReadableString(new org.joda.time.Period(millisUntilFinished));                   
                countdown.setText("Time remaining: " + result);
            }

    private String ToReadableString(Period period) {

        int days = period.get(DurationFieldType.days());

        int hours = period.get(DurationFieldType.hours());
        int minutes = period.getMinutes();
        int seconds = period.getSeconds();

        StringBuilder sb = new StringBuilder();

        if (days > 0) {
            sb.append(days);
            if (days == 1) {
                sb.append(" day, ");
            } else {
                sb.append(" days, ");
            }
        }

        if (hours > 0) {
            sb.append(hours);
            if (hours == 1) {
                sb.append(" hour, ");
            } else {
                sb.append(" hours, ");
            }
        }

        if (minutes > 0) {
            sb.append(minutes);
            if (minutes == 1) {
                sb.append(" minute, ");
            } else {
                sb.append(" minutes, ");
            }
        }

        if (seconds > 0) {
            sb.append(seconds);
            if (seconds == 1) {
                sb.append(" second, ");
            } else {
                sb.append(" seconds, ");
            }
        }

        String result = sb.toString().trim();

        if (result.endsWith(","))
            result = result.substring(0, result.length() - 1);

        return result;
    }
}
4
In your ToReadableString function you obtain the hours - cant you convert them into Days yourself and use the remainder to calculate the hours? - gavlaaaaaaaa

4 Answers

1
votes

EDIT:

I'm editing this to emphasize why letting Jodatime do the heavy lifting might be a better choice. You can remove ToReadableString completely, all you need is (i pulled some variables out into constants, this is not strictly necessary):

private static final PeriodType PERIOD_TYPE = PeriodType.dayTime().withMillisRemoved();
private static final PeriodFormatter PERIOD_FORMATTER = PeriodFormat.getDefault();

public void onTick(long millisUntilFinished) {
    Period period = new Period(millisUntilFinished, PERIOD_TYPE);
    countdown.setText("Time remaining: " + PERIOD_FORMATTER.print(period));
}

on top, you can get all the goodies of Jodatime with minimal effort. Less code, less bugs.

EDIT END;

Joda time's public Period(long duration) constructor uses the standard set of field and thus does not include years/days etc, you should use an the alternative constructor public Period(long duration, PeriodType type)

for your example replace:

String result = ToReadableString(new org.joda.time.Period(millisUntilFinished));

with:

String result = ToReadableString(new org.joda.time.Period(millisUntilFinished, org.joda.time.PeriodType.dayTime().withMillisRemoved()));

As a sidenote, Jodatime can take the pain out of the conversion of a period to a string. Use PeriodFormatter for that. e.g.:

String yourOutputString = PeriodFormat.getDefault().print(period);

you might want to fix your imports for readability.

Fyr: Joda time Javadoc

1
votes

In your ToReadableString function simply do the following to obtain the number of days from the hours - you will have to work out the other differences too:

int days = hours/24;
hours = hours % 24;


//rest of conversion code
0
votes

You can calculte by manually. I paste below method for calculate day hours min and second from difference of MillSecond.

public static String getDateHourMinSecond(long diffMillis) {

    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    diffMillis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(diffMillis);
    diffMillis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(diffMillis);
    diffMillis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(diffMillis);
    String dhms = days + " : " + hours + " : " + minutes + " : " + seconds;
    return dhms;
}
0
votes

Try this function

public void clockCounter(Date startTime) {
Date currentDate =null;
SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            Calendar cal = Calendar.getInstance();
            currentDate = dateFormat.parse(dateFormat.format(cal.getTime()));

        long diffInSec = startTime.getTime() - currentDate.getTime();

        long seconds = diffInSec % 60;
        diffInSec /= 60;
        long minutes = diffInSec % 60;
        diffInSec /= 60;
        long hours = diffInSec % 24;
        diffInSec /= 24;
        long days = diffInSec;

        Log.i("Difference", days+":"+hours+":"+minutes+":"+seconds);
    }