compare Year - Java java.util

Java examples for java.util:Year

Description

compare Year

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(compareYear(calendar, calendar1));
    }/*ww  w  .ja  va2  s .  c  o  m*/

    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