Example usage for java.util GregorianCalendar isLeapYear

List of usage examples for java.util GregorianCalendar isLeapYear

Introduction

In this page you can find the example usage for java.util GregorianCalendar isLeapYear.

Prototype

public boolean isLeapYear(int year) 

Source Link

Document

Determines if the given year is a leap year.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    GregorianCalendar cal = new GregorianCalendar();
    boolean b = cal.isLeapYear(1998); // false
    b = cal.isLeapYear(2000); // true
    b = cal.isLeapYear(0); // true
}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // check if it is a leap year
    boolean isLeapYear = cal.isLeapYear(cal.get(GregorianCalendar.YEAR));
    System.out.println("Is leap year:" + isLeapYear);

    // check if 2014 is a leap year
    isLeapYear = cal.isLeapYear(2014);//from w ww.  ja  va 2s  . c o  m
    System.out.println("Is leap year:" + isLeapYear);

}

From source file:MainClass.java

public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);//from  w ww  .ja  v a2 s.co  m
    System.out.println(gc.isLeapYear(year));
    System.out.println("Month = " + gc.get(Calendar.MONTH));
    System.out.println("Week of year = " + gc.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Week of month = " + gc.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Day of year = " + gc.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of week = " + gc.get(Calendar.DAY_OF_WEEK));
}

From source file:GregorianCalendarDemo.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int year;/*from   w  ww. j  av a2s.c o  m*/

    GregorianCalendar gcalendar = new GregorianCalendar();

    System.out.print("Date: ");
    System.out.print(months[gcalendar.get(Calendar.MONTH)]);
    System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
    System.out.println(year = gcalendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(gcalendar.get(Calendar.HOUR) + ":");
    System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
    System.out.println(gcalendar.get(Calendar.SECOND));

    if (gcalendar.isLeapYear(year)) {
        System.out.println("The current year is a leap year");
    } else {
        System.out.println("The current year is not a leap year");
    }
}

From source file:MainClass.java

public static boolean isLeapYear(int year) {

    GregorianCalendar gcal = new GregorianCalendar();
    return gcal.isLeapYear(year);
}

From source file:Main.java

/**
 * Get the coming leap year next to <code>year</code>
 * @param year year//  w w w .  j av a 2 s  .  c o m
 * @return a leap year
 */
public static int getNextLeap(int year) {
    int nYear = year;
    GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
    if (calendar.isLeapYear(year)) {
        nYear = year;
    } else {
        while (!calendar.isLeapYear(year++)) {
            nYear = year;
        }
    }
    return nYear;
}

From source file:Main.java

/**
 * Given a calendar (possibly containing only a day of the year), returns the earliest possible
 * anniversary of the date that is equal to or after the current point in time if the date
 * does not contain a year, or the date converted to the local time zone (if the date contains
 * a year.//from   w w  w.ja  va  2  s .  c o  m
 *
 * @param target The date we wish to convert(in the UTC time zone).
 * @return If date does not contain a year (year < 1900), returns the next earliest anniversary
 * that is after the current point in time (in the local time zone). Otherwise, returns the
 * adjusted Date in the local time zone.
 */
public static Date getNextAnnualDate(Calendar target) {
    final Calendar today = Calendar.getInstance();
    today.setTime(new Date());

    // Round the current time to the exact start of today so that when we compare
    // today against the target date, both dates are set to exactly 0000H.
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    final boolean isYearSet = isYearSet(target);
    final int targetYear = target.get(Calendar.YEAR);
    final int targetMonth = target.get(Calendar.MONTH);
    final int targetDay = target.get(Calendar.DAY_OF_MONTH);
    final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29);
    final GregorianCalendar anniversary = new GregorianCalendar();
    // Convert from the UTC date to the local date. Set the year to today's year if the
    // there is no provided year (targetYear < 1900)
    anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay);
    // If the anniversary's date is before the start of today and there is no year set,
    // increment the year by 1 so that the returned date is always equal to or greater than
    // today. If the day is a leap year, keep going until we get the next leap year anniversary
    // Otherwise if there is already a year set, simply return the exact date.
    if (!isYearSet) {
        int anniversaryYear = today.get(Calendar.YEAR);
        if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) {
            // If the target date is not Feb 29, then set the anniversary to the next year.
            // Otherwise, keep going until we find the next leap year (this is not guaranteed
            // to be in 4 years time).
            do {
                anniversaryYear += 1;
            } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear));
            anniversary.set(anniversaryYear, targetMonth, targetDay);
        }
    }
    return anniversary.getTime();
}

From source file:org.celllife.idart.gui.patient.AddPatient.java

/**
 * checks if the given date is valid/*from w  w  w  .  ja v a  2  s  . c  om*/
 * 
 * @param strDay
 *            String
 * @param strMonth
 *            String
 * @param strYear
 *            String
 * @return true if the date is valid else false
 */
public boolean dateOkay(String strDay, String strMonth, String strYear) {

    boolean result = false;

    try {

        int day = Integer.parseInt(strDay);

        // check the year
        if (strYear.length() != 4)
            return result;
        int year = Integer.parseInt(strYear);

        // get the int value for the string month (e.g. January)
        // int month = Integer.parseInt(strMonth);
        int month = -1;
        for (int i = 0; i < cmbDOBMonth.getItemCount(); i++) {
            if (strMonth.equals(cmbDOBMonth.getItem(i))) {
                month = i;
            }
        }

        switch (month) {
        case -1:
            result = false;
            break;
        case Calendar.FEBRUARY:
            if (day <= 29) {
                GregorianCalendar greg = new GregorianCalendar();
                if (day == 29 & greg.isLeapYear(year)) {
                    result = true;
                } else {
                    if (day == 29) {
                        result = false;
                    } else {
                        result = true;
                    }
                }
            } else {
                result = false;
            }
            break;
        case Calendar.JANUARY | Calendar.MARCH | Calendar.MAY | Calendar.JULY | Calendar.AUGUST
                | Calendar.OCTOBER | Calendar.DECEMBER:
            if (day <= 31) {
                result = true;
            } else {
                result = false;
            }
            break;
        default:
            result = true;
            break;
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }

    return result;

}