@Test public void testMonthsBetween() throws Exception { Assert.assertEquals(2, Timestamps.getMonthsBetween( TimestampFormatUtils.parseDateTime("2014-05-12T23:45:51.045Z"), TimestampFormatUtils.parseDateTime("2014-07-15T23:45:51.045Z") ) ); Assert.assertEquals(2, Timestamps.getMonthsBetween( TimestampFormatUtils.parseDateTime("2014-07-15T23:45:51.045Z"), TimestampFormatUtils.parseDateTime("2014-05-12T23:45:51.045Z") ) ); Assert.assertEquals(26, Timestamps.getMonthsBetween( TimestampFormatUtils.parseDateTime("2014-05-12T23:45:51.045Z"), TimestampFormatUtils.parseDateTime("2016-07-15T23:45:51.045Z") ) ); Assert.assertEquals(1, Timestamps.getMonthsBetween(TimestampFormatUtils.parseDateTime("2014-05-12T23:45:51.045Z"), TimestampFormatUtils.parseDateTime("2014-07-03T23:45:51.045Z")) ); Assert.assertEquals(109 * 12 + 1, Timestamps.getMonthsBetween(TimestampFormatUtils.parseDateTime("1905-05-12T23:45:51.045Z"), TimestampFormatUtils.parseDateTime("2014-07-03T23:45:51.045Z")) ); }
public static long getMonthsBetween(long a, long b) { if (b < a) { return getMonthsBetween(b, a); } int aYear = getYear(a); int bYear = getYear(b); boolean aLeap = isLeapYear(aYear); boolean bLeap = isLeapYear(bYear); int aMonth = getMonthOfYear(a, aYear, aLeap); int bMonth = getMonthOfYear(b, bYear, bLeap); long aResidual = a - yearMicros(aYear, aLeap) - monthOfYearMicros(aMonth, aLeap); long bResidual = b - yearMicros(bYear, bLeap) - monthOfYearMicros(bMonth, bLeap); long months = 12 * (bYear - aYear) + (bMonth - aMonth); if (aResidual > bResidual) { return months - 1; } else { return months; } }
Timestamps { public static long getMonthsBetween(long a, long b) { if (b < a) { return getMonthsBetween(b, a); } int aYear = getYear(a); int bYear = getYear(b); boolean aLeap = isLeapYear(aYear); boolean bLeap = isLeapYear(bYear); int aMonth = getMonthOfYear(a, aYear, aLeap); int bMonth = getMonthOfYear(b, bYear, bLeap); long aResidual = a - yearMicros(aYear, aLeap) - monthOfYearMicros(aMonth, aLeap); long bResidual = b - yearMicros(bYear, bLeap) - monthOfYearMicros(bMonth, bLeap); long months = 12 * (bYear - aYear) + (bMonth - aMonth); if (aResidual > bResidual) { return months - 1; } else { return months; } } }
Timestamps { public static long getMonthsBetween(long a, long b) { if (b < a) { return getMonthsBetween(b, a); } int aYear = getYear(a); int bYear = getYear(b); boolean aLeap = isLeapYear(aYear); boolean bLeap = isLeapYear(bYear); int aMonth = getMonthOfYear(a, aYear, aLeap); int bMonth = getMonthOfYear(b, bYear, bLeap); long aResidual = a - yearMicros(aYear, aLeap) - monthOfYearMicros(aMonth, aLeap); long bResidual = b - yearMicros(bYear, bLeap) - monthOfYearMicros(bMonth, bLeap); long months = 12 * (bYear - aYear) + (bMonth - aMonth); if (aResidual > bResidual) { return months - 1; } else { return months; } } private  Timestamps(); }
Timestamps { public static long getMonthsBetween(long a, long b) { if (b < a) { return getMonthsBetween(b, a); } int aYear = getYear(a); int bYear = getYear(b); boolean aLeap = isLeapYear(aYear); boolean bLeap = isLeapYear(bYear); int aMonth = getMonthOfYear(a, aYear, aLeap); int bMonth = getMonthOfYear(b, bYear, bLeap); long aResidual = a - yearMicros(aYear, aLeap) - monthOfYearMicros(aMonth, aLeap); long bResidual = b - yearMicros(bYear, bLeap) - monthOfYearMicros(bMonth, bLeap); long months = 12 * (bYear - aYear) + (bMonth - aMonth); if (aResidual > bResidual) { return months - 1; } else { return months; } } private  Timestamps(); static long addDays(long micros, int days); static long addHours(long micros, int hours); static long addMinutes(long micros, int minutes); static long addMonths(final long micros, int months); static long addPeriod(long lo, char type, int period); static long addSeconds(long micros, int seconds); static long addWeeks(long micros, int weeks); static long addYear(long micros, int years); static long ceilDD(long micros); static long ceilMM(long micros); static long ceilYYYY(long micros); static long endOfYear(int year); static long floorDD(long micros); static long floorHH(long micros); static long floorMI(long micros); static long floorMM(long micros); static long floorYYYY(long micros); static int getDayOfMonth(long micros, int year, int month, boolean leap); static int getDayOfWeek(long micros); static int getDayOfWeekSundayFirst(long micros); static long getDaysBetween(long a, long b); static long getWeeksBetween(long a, long b); static long getMicrosBetween(long a, long b); static long getMillisBetween(long a, long b); static long getSecondsBetween(long a, long b); static long getMinutesBetween(long a, long b); static long getHoursBetween(long a, long b); static long getPeriodBetween(char type, long start, long end); static int getDaysPerMonth(int m, boolean leap); static int getHourOfDay0(long micros); static int getHourOfDay(long micros); static int getMicrosOfSecond(long micros); static int getMillisOfSecond(long micros); static int getMinuteOfHour(long micros); static int getMonthOfYear(long micros, int year, boolean leap); static long getMonthsBetween(long a, long b); static int getSecondOfMinute(long micros); static int getYear(long micros); static long getYearsBetween(long a, long b); static boolean isLeapYear(int year); static long monthOfYearMicros(int month, boolean leap); static long nextOrSameDayOfWeek(long millis, int dow); static long parseOffset(CharSequence in, int lo, int hi); static long previousOrSameDayOfWeek(long micros, int dow); static long toMicros(int y, int m, int d, int h, int mi); static long toMicros(int y, boolean leap, int m, int d, int h, int mi); static String toString(long micros); static long yearMicros(int year, boolean leap); }
Timestamps { public static long getMonthsBetween(long a, long b) { if (b < a) { return getMonthsBetween(b, a); } int aYear = getYear(a); int bYear = getYear(b); boolean aLeap = isLeapYear(aYear); boolean bLeap = isLeapYear(bYear); int aMonth = getMonthOfYear(a, aYear, aLeap); int bMonth = getMonthOfYear(b, bYear, bLeap); long aResidual = a - yearMicros(aYear, aLeap) - monthOfYearMicros(aMonth, aLeap); long bResidual = b - yearMicros(bYear, bLeap) - monthOfYearMicros(bMonth, bLeap); long months = 12 * (bYear - aYear) + (bMonth - aMonth); if (aResidual > bResidual) { return months - 1; } else { return months; } } private  Timestamps(); static long addDays(long micros, int days); static long addHours(long micros, int hours); static long addMinutes(long micros, int minutes); static long addMonths(final long micros, int months); static long addPeriod(long lo, char type, int period); static long addSeconds(long micros, int seconds); static long addWeeks(long micros, int weeks); static long addYear(long micros, int years); static long ceilDD(long micros); static long ceilMM(long micros); static long ceilYYYY(long micros); static long endOfYear(int year); static long floorDD(long micros); static long floorHH(long micros); static long floorMI(long micros); static long floorMM(long micros); static long floorYYYY(long micros); static int getDayOfMonth(long micros, int year, int month, boolean leap); static int getDayOfWeek(long micros); static int getDayOfWeekSundayFirst(long micros); static long getDaysBetween(long a, long b); static long getWeeksBetween(long a, long b); static long getMicrosBetween(long a, long b); static long getMillisBetween(long a, long b); static long getSecondsBetween(long a, long b); static long getMinutesBetween(long a, long b); static long getHoursBetween(long a, long b); static long getPeriodBetween(char type, long start, long end); static int getDaysPerMonth(int m, boolean leap); static int getHourOfDay0(long micros); static int getHourOfDay(long micros); static int getMicrosOfSecond(long micros); static int getMillisOfSecond(long micros); static int getMinuteOfHour(long micros); static int getMonthOfYear(long micros, int year, boolean leap); static long getMonthsBetween(long a, long b); static int getSecondOfMinute(long micros); static int getYear(long micros); static long getYearsBetween(long a, long b); static boolean isLeapYear(int year); static long monthOfYearMicros(int month, boolean leap); static long nextOrSameDayOfWeek(long millis, int dow); static long parseOffset(CharSequence in, int lo, int hi); static long previousOrSameDayOfWeek(long micros, int dow); static long toMicros(int y, int m, int d, int h, int mi); static long toMicros(int y, boolean leap, int m, int d, int h, int mi); static String toString(long micros); static long yearMicros(int year, boolean leap); static final long WEEK_MICROS; static final long DAY_MICROS; static final long HOUR_MICROS; static final long MINUTE_MICROS; static final long SECOND_MICROS; static final int SECOND_MILLIS; static final int MILLI_MICROS; static final int STATE_INIT; static final int STATE_UTC; static final int STATE_GMT; static final int STATE_HOUR; static final int STATE_DELIM; static final int STATE_MINUTE; static final int STATE_END; static final int STATE_SIGN; static final TimestampFloorMethod FLOOR_DD; static final TimestampAddMethod ADD_DD; static final TimestampFloorMethod FLOOR_YYYY; static final TimestampFloorMethod FLOOR_MM; static final TimestampAddMethod ADD_MM; static final TimestampAddMethod ADD_YYYY; }