new GregorianCalendar() : GregorianCalendar « java.util « Java by API






new GregorianCalendar()

 
/*
 * Output:
The number of days till Christmas is 244
 */

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MainClass {

  public static void main(String args[]) {
    GregorianCalendar today = new GregorianCalendar();

    int todayMonth = today.get(Calendar.MONTH);

    int todayDayOfMonth = today.get(Calendar.DAY_OF_MONTH);

    int todayYear = today.get(Calendar.YEAR);

    int todayDayOfYear = today.get(Calendar.DAY_OF_YEAR);

    int numDays;
    if (todayMonth == 11 && todayDayOfMonth > 25) {
      GregorianCalendar xmas = new GregorianCalendar(todayYear + 1, 11, 25);
      int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
      numDays = xmasDayOfYear + 31 - todayDayOfMonth;
    } else {
      GregorianCalendar xmas = new GregorianCalendar(todayYear, 11, 25);
      int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
      numDays = xmasDayOfYear - todayDayOfYear;
    }

    System.out.println("The number of days till Christmas is " + numDays);
  }
}
           
         
  








Related examples in the same category

1.new GregorianCalendar(int year, int month, int dayOfMonth)
2.new GregorianCalendar(Locale aLocale)
3.new GregorianCalendar(TimeZone zone, Locale aLocale)
4.GregorianCalendar: get(int a)
5.GregorianCalendar: getTime()
6.GregorianCalendar: isLeapYear()
7.GregorianCalendar: roll(int field, boolean up)
8.GregorianCalendar: set(int field, int value)
9.GregorianCalendar: set(int year, int month, int date)
10.GregorianCalendar: setTime(Date date)