Here you can find the source of weeksBetween(Date early, Date late)
Parameter | Description |
---|---|
early | the "first date" |
late | the "second date" |
public static final int weeksBetween(Date early, Date late)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { /**/* w w w. j av a 2 s .c o m*/ * Returns the weeks between two dates. * * @param early * the "first date" * @param late * the "second date" * @return the weeks between the two dates */ public static final int weeksBetween(Date early, Date late) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(early); c2.setTime(late); int days = daysBetween(c1, c2) + 1; int earlyweek = c1.get(Calendar.DAY_OF_WEEK); int lateweek = c2.get(Calendar.DAY_OF_WEEK); int weeks = days / 7; int weekst = days % 7; if (weekst == 0) { return weeks; } else if (lateweek >= earlyweek) { return weeks + 1; } else { return weeks + 2; } } public static final int daysBetween(Calendar early, Calendar late) { return (int) (toJulian(late) - toJulian(early)); } public static final int daysBetween(Date early, Date late) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(early); c2.setTime(late); return daysBetween(c1, c2); } /** * Return a Julian date based on the input parameter. This is based from * calculations found at * * <pre> * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day * Calculations (Gregorian Calendar)</a>, provided by Bill Jeffrys. * </pre> * * @param c * a calendar instance * @return the julian day number */ public static final float toJulian(Calendar c) { int Y = c.get(Calendar.YEAR); int M = c.get(Calendar.MONTH); int D = c.get(Calendar.DATE); int A = Y / 100; int B = A / 4; int C = 2 - A + B; float E = (int) (365.25f * (Y + 4716)); float F = (int) (30.6001f * (M + 1)); float JD = C + D + E + F - 1524.5f; return JD; } }