Java by API/java.util/GregorianCalendar — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:10, 31 мая 2010
Содержание
- 1 GregorianCalendar: get(int a)
- 2 GregorianCalendar: getTime()
- 3 GregorianCalendar: isLeapYear()
- 4 GregorianCalendar: roll(int field, boolean up)
- 5 GregorianCalendar: set(int field, int value)
- 6 GregorianCalendar: set(int year, int month, int date)
- 7 GregorianCalendar: setTime(Date date)
- 8 new GregorianCalendar()
- 9 new GregorianCalendar(int year, int month, int dayOfMonth)
- 10 new GregorianCalendar(Locale aLocale)
- 11 new GregorianCalendar(TimeZone zone, Locale aLocale)
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);
}
}