compare Month - Java java.util

Java examples for java.util:Month

Description

compare Month

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1 = Calendar.getInstance();
        System.out.println(compareMonth(calendar, calendar1));
    }//w  ww  . j  a va 2  s  .  co m

    public static int compareMonth(Calendar calendar, Calendar calendar1) {
        int i = compareYear(calendar, calendar1);
        if (i != 0)
            return i;
        else
            return calendar.get(Calendar.MONTH)
                    - calendar1.get(Calendar.MONTH);
    }

    public static int compareYear(Calendar calendar, Calendar calendar1) {
        int i = compareEra(calendar, calendar1);
        if (i != 0)
            return i;
        else
            return calendar.get(Calendar.YEAR)
                    - calendar1.get(Calendar.YEAR);
    }

    public static int compareEra(Calendar calendar, Calendar calendar1) {
        if (calendar.getClass() != calendar1.getClass())
            throw new IllegalArgumentException(
                    "Cannot compare calendars of dissimilar classes: "
                            + calendar + ", " + calendar1);
        else
            return calendar.get(Calendar.ERA) - calendar1.get(Calendar.ERA);
    }
}

Related Tutorials