Monday, March 7, 2011

Difference in two Calendar Dates

There are many ways to calculate the difference between two Calendar dates in Year, Months, weeks or days by simply dividing them with a fixed constant. DateUtil.java below has a method for getting the difference in Days. this method can similarly be modified to get in Weeks, Months or Years.

But some times the challenge is to display the difference in two java.util.Calendar  or java.util.Date in more user friendly manner. Like in format X Years, X months, X weeks, X days. Where X is positive number greater than zero.

Example: If the number of Full Years < 1, the format should be X months, X weeks, X days. If the number of Full Years >= 1 but the number of Full Months < 1, the format should be X Years, X weeks, X days and so on. Values of 0 is not written. Like "2 months, 2 days" rather than "2 months, 0 weeks, 2 days"


In sample java source below, method named generate() in DateUtil.java will generate the required string or phrase based on two dates provided. Start date need not be less than the end date. It can be other way also. The method generates the difference in positive always.

DateUtil.java

package org.mbm.playgound;

import java.util.Calendar;


/**
 * @author Mohammed Bin Mahmood
 * 
 */
public class DateUtil {
    private static final String YEARS = "Years", MONTHS = "months", WEEKS = "weeks", DAYS = "days", COMMA = ", ";
    private static final char SPACE = ' ';
    private static final long DAY_IN_MILLI = 24 * 60 * 60 * 1000;
    
    private static int getDays(Calendar start, Calendar end) {
        // get positive value so that the difference is calculated as positive number.
        return (int) (Math.abs(start.getTimeInMillis() - end.getTimeInMillis()) / DAY_IN_MILLI);
    }
    
    private static String generate(Calendar start, Calendar end) {
        int y = 0, m = 0, w = 0;
        int d = getDays(start, end);
        
        y = d / 365;
        // remaining days in a year.
        if (y != 0)
            d = d % (y * 365);
        
        m = d / 30;
        // remaining days in a month.
        if (m != 0)
            d = d % (m * 30);
        
        w = d / 7;
        // remaining days in a week.
        if (w != 0)
            d = d % (w * 7);
        
        StringBuilder sb = new StringBuilder(16);
        if (y > 0)
            sb.append(y).append(SPACE).append(YEARS);
        if (m > 0) {
            if (sb.length() > 0)
                sb.append(COMMA);
            sb.append(m).append(SPACE).append(MONTHS);
        }
        if (w > 0) {
            if (sb.length() > 0)
                sb.append(COMMA);
            sb.append(w).append(SPACE).append(WEEKS);
        }
        if (d > 0) {
            if (sb.length() > 0)
                sb.append(COMMA);
            sb.append(d).append(SPACE).append(DAYS);
        }
        
        return sb.length() == 0 ? null : sb.toString();
    }
    
    public static void main(String a[]) {
        Calendar now = Calendar.getInstance();
        System.out.println(now.getTime());
        
        Calendar end = (Calendar) now.clone();
        // add 1000days or 5 calendar year.
        // end.add(Calendar.YEAR, 5);
        end.add(Calendar.DAY_OF_YEAR, 1000);
        System.out.println(end.getTime());
        
        System.out.println(generate(now, end));
    }
    
}



No comments:

Post a Comment

Was this article useful?