Tells if it's a Christmas. - Java java.util

Java examples for java.util:Calendar Holiday

Description

Tells if it's a Christmas.

Demo Code

/*//  w  w  w. jav a 2s .  c  om
 * Copyright (C) 2014 Celestibytes
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 */
//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(isChristmas());
    }

    /**
     * The current time.
     */
    private static Calendar curTime = Calendar.getInstance();
    /**
     * The specific event's starting time.
     */
    private static Calendar eventStart = Calendar.getInstance();
    /**
     * The specific event's ending time.
     */
    private static Calendar eventEnd = Calendar.getInstance();

    /**
     * Tells if it's a Christmas.
     *
     * @return {@code true} if it's a Christmas, otherwise {@code false}.
     */
    public static boolean isChristmas() {
        setDate(eventStart, Calendar.DECEMBER, 24);
        setDate(eventEnd, Calendar.DECEMBER, 27);

        curTime = Calendar.getInstance();

        return curTime.after(eventStart) && curTime.before(eventEnd);
    }

    /**
     * Sets the given {@link Calendar}'s instance to a specific date.
     *
     * @param calendar
     *            the {@link Calendar}'s instance.
     * @param month
     *            the month.
     * @param date
     *            the day of the month.
     */
    private static void setDate(Calendar calendar, int month, int date) {
        calendar.clear();
        calendar.set(Calendar.YEAR,
                Calendar.getInstance().get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
}

Related Tutorials