get Week Count - Java java.util

Java examples for java.util:Week

Description

get Week Count

Demo Code


//package com.java2s;

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

public class Main {

    public static int getWeekCount(final Date startDate,
            final Date countDate) {
        if (countDate.getTime() < startDate.getTime())
            return -1;
        final long CONST_DAY = 3600 * 1000 * 24;
        long s = startDate.getTime() - CONST_DAY;
        long c = countDate.getTime() - CONST_DAY;
        int weekCount = (int) ((c - s) / (7 * CONST_DAY) + 1);
        long mod = (c - s) % (7 * CONST_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(s);/*from   www. j  a va2  s . co  m*/
        int d1 = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.setTimeInMillis(s + mod);
        int d2 = calendar.get(Calendar.DAY_OF_WEEK);
        if (d2 < d1)
            weekCount++;
        return weekCount;
    }
}

Related Tutorials