Java by API/java.util/GregorianCalendar

Материал из Java эксперт
Версия от 14:10, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

GregorianCalendar: get(int a)

 
/*
 * Output: 
2006
false
Month = 3
Week of year = 17
Week of month = 5
Day of year = 115
Day of week = 3 
 */
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
  public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);
    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));
  }
}





GregorianCalendar: getTime()

  
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    Date now = calendar.getTime();
    System.out.println(now);
  }
}





GregorianCalendar: isLeapYear()

 
/*
 * Output: 
2006
false
Month = 3
Week of year = 17
Week of month = 5
Day of year = 115
Day of week = 3 
 */
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MainClass {
  public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);
    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));
  }
}





GregorianCalendar: roll(int field, boolean up)

  
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.roll(Calendar.MONTH, false); // Go back a month
    System.out.println(calendar.get(Calendar.MONTH));
  }
}





GregorianCalendar: set(int field, int value)

  
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  }
}





GregorianCalendar: set(int year, int month, int date)

  
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(1995, 10, 29); // Date set to 29th November 1999
  }
}





GregorianCalendar: setTime(Date date)

  
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
  public static void main(String[] a) {
    Date date = new Date();
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
  }
}





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);
  }
}





new GregorianCalendar(int year, int month, int dayOfMonth)

 
/*
 * 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);
  }
}





new GregorianCalendar(Locale aLocale)

  
import java.util.GregorianCalendar;
import java.util.Locale;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar(Locale.UK);
  }
}





new GregorianCalendar(TimeZone zone, Locale aLocale)

  
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
  public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"),
        Locale.US);
    System.out.println(calendar);
  }
}