Java Code Examples for java.util.GregorianCalendar#isLeapYear()

The following examples show how to use java.util.GregorianCalendar#isLeapYear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskBirthday.java    From L2jOrg with GNU General Public License v3.0 6 votes vote down vote up
private void checkBirthday(int year, int month, int day) {
    var charactersData = getDAO(PlayerDAO.class).findBirthdayCharacters(year, month, day);
    charactersData.forEach(characterData -> {
        var name = PlayerNameTable.getInstance().getNameById(characterData.getCharId());
        if(isNull(name)) {
            return;
        }

        var age = year - characterData.getCreateDate().getYear();
        var text = Config.ALT_BIRTHDAY_MAIL_TEXT.replace("$c1", name).replace("$s1", String.valueOf(age));

        final var mail = MailData.of(characterData.getCharId(), Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, MailType.BIRTHDAY);
        final Attachment attachments = new Attachment(mail.getSender(), mail.getId());
        attachments.addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
        mail.attach(attachments);
        MailEngine.getInstance().sendMail(mail);
        _count++;
    });

    // If character birthday is 29-Feb and year isn't leap, send gift on 28-feb
    final GregorianCalendar calendar = new GregorianCalendar();
    if ((month == Calendar.FEBRUARY) && (day == 28) && !calendar.isLeapYear(_today.get(Calendar.YEAR))) {
        checkBirthday(year, month, 29);
    }
}
 
Example 2
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check isLeapYear for BC years.
 */
public void Test4125881() {
    Locale locale = Locale.getDefault();
    if (!TestUtils.usesGregorianCalendar(locale)) {
        logln("Skipping this test because locale is " + locale);
        return;
    }

    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    cal.clear();
    for (int y = -20; y <= 10; ++y) {
        cal.set(ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
        cal.set(YEAR, y < 1 ? 1 - y : y);
        logln(y + " = " + fmt.format(cal.getTime()) + " "
                + cal.isLeapYear(y));
        if (cal.isLeapYear(y) != ((y + 40) % 4 == 0)) {
            errln("Leap years broken");
        }
    }
}
 
Example 3
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prove that GregorianCalendar is proleptic (it used to cut off
 * at 45 BC, and not have leap years before then).
 */
public void Test4125892() {
    Locale locale = Locale.getDefault();
    if (!TestUtils.usesGregorianCalendar(locale)) {
        logln("Skipping this test because locale is " + locale);
        return;
    }

    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    cal.clear();
    cal.set(ERA, GregorianCalendar.BC);
    cal.set(YEAR, 81); // 81 BC is a leap year (proleptically)
    cal.set(MONTH, FEBRUARY);
    cal.set(DATE, 28);
    cal.add(DATE, 1);
    if (cal.get(DATE) != 29
            || !cal.isLeapYear(-80)) { // -80 == 81 BC
        errln("Calendar not proleptic");
    }
}
 
Example 4
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reported bug is that a GregorianCalendar with a cutover of Date(Long.MAX_VALUE)
 * doesn't behave as a pure Julian calendar.
 * CANNOT REPRODUCE THIS BUG
 */
public void Test4149677() {
    TimeZone[] zones = {TimeZone.getTimeZone("GMT"),
        TimeZone.getTimeZone("PST"),
        TimeZone.getTimeZone("EAT")};
    for (int i = 0; i < zones.length; ++i) {
        GregorianCalendar calendar = new GregorianCalendar(zones[i]);

        // Make sure extreme values don't wrap around
        calendar.setTime(new Date(Long.MIN_VALUE));
        if (calendar.get(ERA) != GregorianCalendar.BC) {
            errln("Fail: Date(Long.MIN_VALUE) has an AD year in " + zones[i]);
        }
        calendar.setTime(new Date(Long.MAX_VALUE));
        if (calendar.get(ERA) != GregorianCalendar.AD) {
            errln("Fail: Date(Long.MAX_VALUE) has a BC year in " + zones[i]);
        }

        calendar.setGregorianChange(new Date(Long.MAX_VALUE));
        // to obtain a pure Julian calendar

        boolean is100Leap = calendar.isLeapYear(100);
        if (!is100Leap) {
            errln("test failed with zone " + zones[i].getID());
            errln(" cutover date is Date(Long.MAX_VALUE)");
            errln(" isLeapYear(100) returns: " + is100Leap);
        }
    }
}
 
Example 5
Source File: AstroUtil.java    From Astrosoft with GNU General Public License v2.0 5 votes vote down vote up
public static Date decimalYearToDate(double year){
 
 int yr = (int) year;
 double days;
 GregorianCalendar cal = new GregorianCalendar(yr, Calendar.JANUARY, 1);
 if ( cal.isLeapYear(yr)) {
        days = (year - yr) * 366;

    } else {
 	   days = (year - yr) * 365;
    }
 cal.add(Calendar.DATE, (int)days);//Math.ceil(days));
 return cal.getTime();
}
 
Example 6
Source File: AstroUtil.java    From Astrosoft with GNU General Public License v2.0 5 votes vote down vote up
public static double dateToDecimalYear(GregorianCalendar cal){

       int days = cal.get( Calendar.DAY_OF_YEAR );
       double year = cal.get(Calendar.YEAR);
       double dayFrac;

       if ( cal.isLeapYear((int)year) ) {
           dayFrac = ( double ) days / 366;

       } else {
           dayFrac = ( double ) days / 365;
       }

       return year + dayFrac;
   }
 
Example 7
Source File: DateUtils.java    From wasindoor with Apache License 2.0 5 votes vote down vote up
/**
 * 获取任意两个日期间的天数
 * @param tStartDate 起始日期,
 * @param tEndDate  结束日期
 * @return 天数
 */
public static int getDayCount(Date tStartDate, Date tEndDate) {
    int iRetVal = 0;
    GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.
                                 getInstance();
    calendar.setTime(tStartDate);
    GregorianCalendar calendar2 = (GregorianCalendar) GregorianCalendar.
                                  getInstance();
    calendar2.setTime(tEndDate);
    int iMaxDays = 0;

    while (calendar.before(calendar2)) {
        if (calendar.isLeapYear(calendar.get(GregorianCalendar.YEAR))) {
            iMaxDays = 366;
        } else {
            iMaxDays = 365;
        }
        ++iRetVal;
        calendar.roll(GregorianCalendar.DAY_OF_YEAR, true);

        if (calendar.get(GregorianCalendar.DAY_OF_YEAR) == iMaxDays) {
            calendar.roll(GregorianCalendar.YEAR, 1);
            calendar.set(GregorianCalendar.MONTH, GregorianCalendar.JANUARY);
            calendar.set(GregorianCalendar.DAY_OF_MONTH, 1);
        }
    }
    return iRetVal;
}
 
Example 8
Source File: daysInMonth.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private static int determineDays(Calendar _cal) {
	int month = _cal.get(Calendar.MONTH);

	if (month == 8 || month == 3 || month == 5 || month == 10)
		return 30;
	else if (month == 1) {
		GregorianCalendar greg = new GregorianCalendar(_cal.get(Calendar.YEAR), _cal.get(Calendar.MONTH), _cal.get(Calendar.DATE));
		if (greg.isLeapYear(greg.get(Calendar.YEAR)))
			return 29;
		else
			return 28;
	} else
		return 31;
}
 
Example 9
Source File: DateChooser.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
private int days(GregorianCalendar date) {
  return date.get(Calendar.MONTH) == 1 ? 
    (date.isLeapYear(date.get(Calendar.YEAR)) ? 29 : 28) :
    DAYS[date.get(Calendar.MONTH)];
}
 
Example 10
Source File: IDCardUtil.java    From anyline with Apache License 2.0 4 votes vote down vote up
/** 
 * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。 
 *  
 * @param idcard  idcard
 * @return return
 */ 
public static boolean validate15(String idcard) { 
	// 非15位为假 
	if (idcard.length() != 15) { 
		return false; 
	} 

	// 是否全都为数字 
	if (isDigital(idcard)) { 
		String provinceid = idcard.substring(0, 2); 
		String birthday = idcard.substring(6, 12); 
		int year = Integer.parseInt(idcard.substring(6, 8)); 
		int month = Integer.parseInt(idcard.substring(8, 10)); 
		int day = Integer.parseInt(idcard.substring(10, 12)); 

		// 判断是否为合法的省份 
		boolean flag = false; 
		for (String id : CITY_CODE) { 
			if (id.equals(provinceid)) { 
				flag = true; 
				break; 
			} 
		} 
		if (!flag) { 
			return false; 
		} 
		// 该身份证生出日期在当前日期之后时为假 
		Date birthdate = null; 
		try { 
			birthdate = new SimpleDateFormat("yyMMdd").parse(birthday); 
		} catch (ParseException e) { 
			e.printStackTrace(); 
		} 
		if (birthdate == null || new Date().before(birthdate)) { 
			return false; 
		} 

		// 判断是否为合法的年份 
		GregorianCalendar curDay = new GregorianCalendar(); 
		int curYear = curDay.get(Calendar.YEAR); 
		int year2bit = Integer.parseInt(String.valueOf(curYear) 
				.substring(2)); 

		// 判断该年份的两位表示法,小于50的和大于当前年份的,为假 
		if ((year < 50 && year > year2bit)) { 
			return false; 
		} 

		// 判断是否为合法的月份 
		if (month < 1 || month > 12) { 
			return false; 
		} 

		// 判断是否为合法的日期 
		boolean mflag = false; 
		curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay 
		switch (month) { 
		case 1: 
		case 3: 
		case 5: 
		case 7: 
		case 8: 
		case 10: 
		case 12: 
			mflag = (day >= 1 && day <= 31); 
			break; 
		case 2: // 公历的2月非闰年有28天,闰年的2月是29天。 
			if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) { 
				mflag = (day >= 1 && day <= 29); 
			} else { 
				mflag = (day >= 1 && day <= 28); 
			} 
			break; 
		case 4: 
		case 6: 
		case 9: 
		case 11: 
			mflag = (day >= 1 && day <= 30); 
			break; 
		} 
		if (!mflag) { 
			return false; 
		} 
	} else { 
		return false; 
	} 
	return true; 
}
 
Example 11
Source File: GregorianCalendarExample.java    From tutorials with MIT License 4 votes vote down vote up
public  boolean isLeapYearExample(int year) {
    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
    return cal.isLeapYear(year);
}
 
Example 12
Source File: GridCellAdapter.java    From JKCalendar with Apache License 2.0 4 votes vote down vote up
/**
     * Prints Month
     *
     * @param mm
     * @param yy
     */
    private void printMonth(int mm, int yy) {
//        //JKLog.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
        // The number of days to leave blank at
        // the start of this month.
        int trailingSpaces = 0;
        int leadSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
        String currentMonthName = getMonthAsString(currentMonth);
        daysInMonth = getNumberOfDaysOfMonth(_calendar);

//        //JKLog.d(tag, "Current Month: " + " " + currentMonthName + " having " + daysInMonth + " days.");

        // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        //JKLog.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

        Calendar prevC = Calendar.getInstance();

        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            prevC.set(Calendar.MONTH,prevMonth);
            daysInPrevMonth = getNumberOfDaysOfMonth(prevC);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;
            //JKLog.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            prevC.set(Calendar.MONTH,prevMonth);
            daysInPrevMonth = getNumberOfDaysOfMonth(prevC);
            nextMonth = 1;
            //JKLog.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            prevC.set(Calendar.MONTH,prevMonth);
            daysInPrevMonth = getNumberOfDaysOfMonth(prevC);
            //JKLog.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:" + prevMonth + " NextMonth: " + nextMonth + " NextYear: " + nextYear);
        }

        // Compute how much to leave before before the first day of the
        // month.
        // getDay() returns 0 for Sunday.
        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        trailingSpaces = currentWeekDay;

        //JKLog.d(tag, "Week Day:" + currentWeekDay + " is " + getWeekDayAsString(currentWeekDay));
        //JKLog.d(tag, "No. Trailing space to Add: " + trailingSpaces);
        //JKLog.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

        if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 1) {
            ++daysInMonth;
        }

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
//            //JKLog.d(tag, "PREV MONTH:= " + prevMonth + " => " + getMonthAsString(prevMonth) + " " + String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i));
            list.add("GREY-"+ String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i)  + "-" + getMonthAsString(prevMonth) + "-" + prevYear);
        }

        //紀錄Month是第幾格開始
        thisMonthStartIndex = list.size();

        // Current Month Days
        for (int i = 1; i <= daysInMonth; i++) {
            //JKLog.d(currentMonthName, String.valueOf(i) + " " + getMonthAsString(currentMonth) + " " + yy);
            if (i == getCurrentDayOfMonth()) {
                list.add("BLACK-" + String.valueOf(i) +  "-" + getMonthAsString(currentMonth) + "-" + yy);
            } else {
                list.add("WHITE-" + String.valueOf(i) +  "-" + getMonthAsString(currentMonth) + "-" + yy);
            }
        }

        //紀錄Month是第幾格結束
        thisMonthEndIndex = list.size();

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++) {
            //JKLog.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
            list.add( "GREY-" + String.valueOf(i + 1) +"-" + getMonthAsString(nextMonth) + "-" + nextYear);
        }
    }
 
Example 13
Source File: ActionExampleResolution.java    From livingdoc-core with GNU General Public License v3.0 4 votes vote down vote up
public boolean isLeapYear() {
    GregorianCalendar calendar = new GregorianCalendar();
    return calendar.isLeapYear(fieldYear);
}
 
Example 14
Source File: OgnlExpectationFixture.java    From livingdoc-core with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unused")
public boolean isLeapYear(int year) {
    GregorianCalendar calendar = new GregorianCalendar();
    return calendar.isLeapYear(year);
}
 
Example 15
Source File: ActionExampleResolution.java    From livingdoc-core with GNU General Public License v3.0 4 votes vote down vote up
public boolean isLeapYear() {
    GregorianCalendar calendar = new GregorianCalendar();
    return calendar.isLeapYear(fieldYear);
}
 
Example 16
Source File: CalendarPage.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
/** Compute which days to put where, in the Cal panel */
public void print(int mm, int yy) {
	/** The number of days to leave blank at the start of this month */
	int leadGap = 0;

	System.out.print(months[mm]);		// print month and year
	System.out.print(" ");
	System.out.print(yy);
	System.out.println();

	if (mm < 0 || mm > 11)
		throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");
	GregorianCalendar calendar = new GregorianCalendar(yy, mm, 1);

	System.out.println("Su Mo Tu We Th Fr Sa");

	// Compute how much to leave before the first.
	// get(DAY_OF_WEEK) returns 0 for Sunday, which is just right.
	leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;

	int daysInMonth = CalUtils.getDaysInMonth(mm);
	if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)
		++daysInMonth;

	// Blank out the labels before 1st day of month
	for (int i = 0; i < leadGap; i++) {
		System.out.print("   ");
	}

	// Fill in numbers for the day of month.
	for (int i = 1; i <= daysInMonth; i++) {

		// This "if" statement is simpler than fiddling with NumberFormat
		if (i<=9)
			System.out.print(' ');
		System.out.print(i);

		if ((leadGap + i) % 7 == 0)		// wrap if end of line.
			System.out.println();
		else
			System.out.print(' ');
	}
	System.out.println();
}
 
Example 17
Source File: IdcardUtil.java    From SuperBoot with MIT License 4 votes vote down vote up
/**
 * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。
 *
 * @param idcard
 * @return
 */
public boolean isValidate15Idcard(String idcard) {
    // 非15位为假
    if (idcard.length() != 15) {
        return false;
    }

    // 是否全都为数字
    if (isDigital(idcard)) {
        String provinceid = idcard.substring(0, 2);
        String birthday = idcard.substring(6, 12);
        int year = Integer.parseInt(idcard.substring(6, 8));
        int month = Integer.parseInt(idcard.substring(8, 10));
        int day = Integer.parseInt(idcard.substring(10, 12));

        // 判断是否为合法的省份
        boolean flag = false;
        for (String id : cityCode) {
            if (id.equals(provinceid)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            return false;
        }
        // 该身份证生出日期在当前日期之后时为假
        Date birthdate = null;
        try {
            birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (birthdate == null || new Date().before(birthdate)) {
            return false;
        }

        // 判断是否为合法的年份
        GregorianCalendar curDay = new GregorianCalendar();
        int curYear = curDay.get(Calendar.YEAR);
        int year2bit = Integer.parseInt(String.valueOf(curYear)
                .substring(2));

        // 判断该年份的两位表示法,小于50的和大于当前年份的,为假
        if ((year < 50 && year > year2bit)) {
            return false;
        }

        // 判断是否为合法的月份
        if (month < 1 || month > 12) {
            return false;
        }

        // 判断是否为合法的日期
        boolean mflag = false;
        curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                mflag = (day >= 1 && day <= 31);
                break;
            case 2: // 公历的2月非闰年有28天,闰年的2月是29天。
                if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) {
                    mflag = (day >= 1 && day <= 29);
                } else {
                    mflag = (day >= 1 && day <= 28);
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                mflag = (day >= 1 && day <= 30);
                break;
        }
        if (!mflag) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}
 
Example 18
Source File: IdcardValidator.java    From RxJava2RetrofitDemo with Apache License 2.0 4 votes vote down vote up
/**
 * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。
 */
public boolean isValidate15Idcard(String idcard) {
    // 非15位为假
    if (idcard.length() != 15) {
        return false;
    }
    // 是否全都为数字
    if (isDigital(idcard)) {
        String provinceid = idcard.substring(0, 2);
        String birthday = idcard.substring(6, 12);
        int year = Integer.parseInt(idcard.substring(6, 8));
        int month = Integer.parseInt(idcard.substring(8, 10));
        int day = Integer.parseInt(idcard.substring(10, 12));
        // 判断是否为合法的省份
        boolean flag = false;
        for (String id : cityCode) {
            if (id.equals(provinceid)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            return false;
        }
        // 该身份证生出日期在当前日期之后时为假
        Date birthdate = null;
        try {
            birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (birthdate == null || new Date().before(birthdate)) {
            return false;
        }
        // 判断是否为合法的年份
        GregorianCalendar curDay = new GregorianCalendar();
        int curYear = curDay.get(Calendar.YEAR);
        int year2bit = Integer.parseInt(String.valueOf(curYear).substring(2));
        // 判断该年份的两位表示法,小于50的和大于当前年份的,为假
        if ((year < 50 && year > year2bit)) {
            return false;
        }
        // 判断是否为合法的月份
        if (month < 1 || month > 12) {
            return false;
        }
        // 判断是否为合法的日期
        boolean mflag = false;
        curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                mflag = (day >= 1 && day <= 31);
                break;
            case 2: // 公历的2月非闰年有28天,闰年的2月是29天。
                if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) {
                    mflag = (day >= 1 && day <= 29);
                } else {
                    mflag = (day >= 1 && day <= 28);
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                mflag = (day >= 1 && day <= 30);
                break;
        }
        if (!mflag) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}