get Day Duration between two date - Android java.util

Android examples for java.util:Date Algorithm

Description

get Day Duration between two date

Demo Code


//package com.java2s;

import android.text.format.DateUtils;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int getDayDuration(int year1, int month1, int day1,
            int year2, int month2, int day2) {
        Calendar src = Calendar.getInstance();
        src.set(year1, month1, day1, 0, 0, 0);
        Calendar dest = Calendar.getInstance();
        dest.set(year2, month2, day2, 0, 0, 0);

        return getDayDuration(src, dest);
    }/*from  w w w. ja  va2 s . c o  m*/

    public static int getDayDuration(Calendar src, Calendar dest) {
        if (dest == null) {
            dest = Calendar.getInstance();
            dest.setTime(new Date());
        }

        long duration = getDayDuration(src.getTimeInMillis(),
                dest.getTimeInMillis());

        return (int) (duration / DateUtils.DAY_IN_MILLIS);
    }

    public static int getDayDuration(long src, long dest) {
        long duration = Math.abs(src - dest);

        return (int) (duration / DateUtils.DAY_IN_MILLIS);
    }
}

Related Tutorials