Java : Calendar (カレンダー) - API使用例
Calendar (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
注意
- Calendar クラスは、非推奨(@Deprecated) にこそなっていませんが、古い API です。
代わりに、java.time パッケージにある LocalDateTime や ZonedDateTime などの新しい API を使うことをおすすめします。
概要
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.DECEMBER, 31)
.setTimeOfDay(14, 30, 59)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-12-31 14:30:59
System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.MINUTE)); //30
System.out.println(calendar.get(Calendar.SECOND)); // 59
フィールド
static final int ALL_STYLES
final var calendar = new Calendar.Builder().build();
final var names = calendar.getDisplayNames(
Calendar.MONTH, Calendar.ALL_STYLES, Locale.US);
// {September=8, December=11, February=1, November=10, January=0, October=9,
// August=7, April=3, March=2, July=6, June=5, Apr=3, Aug=7, Dec=11, Feb=1,
// Jan=0, Jul=6, Jun=5, Mar=2, May=4, Nov=10, Oct=9, Sep=8}
System.out.println(names);
static final int AM
final var calendar = new Calendar.Builder()
.setTimeOfDay(10, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 10
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // true
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // false
final var calendar = new Calendar.Builder()
.setTimeOfDay(17, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 17
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // false
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // true
static final int AM_PM
final var calendar = new Calendar.Builder()
.setTimeOfDay(10, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 10
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // true
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // false
final var calendar = new Calendar.Builder()
.setTimeOfDay(17, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 17
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // false
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // true
static final int APRIL
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.APRIL, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.APRIL); // true
protected boolean areFieldsSet
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
static final int AUGUST
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.AUGUST); // true
static final int DATE
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.FEBRUARY, 15)
.build();
System.out.printf("%tF%n", calendar); // 2100-02-15
System.out.println(calendar.get(Calendar.DATE)); // 15
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 15
static final int DAY_OF_MONTH
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 15)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-15
System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 15
static final int DAY_OF_WEEK
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 3)
.build();
{
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Sunday
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 1
}
calendar.add(Calendar.DATE, 1);
{
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Monday
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 2
}
calendar.add(Calendar.DATE, 5);
{
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Saturday
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 7
}
static final int DAY_OF_WEEK_IN_MONTH
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 7);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 8);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 14);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 15);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)); // 3
static final int DAY_OF_YEAR
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 1
calendar.set(2100, Calendar.JANUARY, 31);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 31
calendar.set(2100, Calendar.FEBRUARY, 1);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 32
calendar.set(2100, Calendar.DECEMBER, 31);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 365
static final int DECEMBER
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.DECEMBER, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true
static final int DST_OFFSET
final var zone = TimeZone.getTimeZone("America/Los_Angeles");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 1)
.setTimeZone(zone)
.build();
System.out.println(calendar.get(Calendar.DST_OFFSET)); // 3600000
static final int ERA
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.get(Calendar.ERA)); // 1
calendar.set(Calendar.YEAR, -100);
System.out.println(calendar.get(Calendar.ERA)); // 0
static final int FEBRUARY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.FEBRUARY, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.FEBRUARY); // true
static final int FIELD_COUNT
System.out.println(Calendar.FIELD_COUNT); // 17
protected int[] fields
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
static final int FRIDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.FRIDAY); // true
static final int HOUR
final var calendar = new Calendar.Builder()
.setTimeOfDay(14, 30, 59, 999)
.build();
System.out.printf("%tT%n", calendar); // 14:30:59
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2
System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999
static final int HOUR_OF_DAY
final var calendar = new Calendar.Builder()
.setTimeOfDay(14, 30, 59, 999)
.build();
System.out.printf("%tT%n", calendar); // 14:30:59
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2
System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999
protected boolean[] isSet
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected boolean isTimeSet
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
static final int JANUARY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true
static final int JULY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JULY, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JULY); // true
static final int JUNE
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JUNE, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JUNE); // true
static final int LONG
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name1 = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(name1); // January
final var name2 = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(name2); // Jan
final var name3 = calendar.getDisplayName(
Calendar.MONTH, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name3); // J
static final int LONG_FORMAT
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name1 = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(name1); // January
final var name2 = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG_FORMAT, Locale.US);
System.out.println(name2); // January
static final int LONG_STANDALONE
final var calendar = new Calendar.Builder().build();
final var name1 = calendar.getDisplayNames(
Calendar.ERA, Calendar.LONG, Locale.US);
System.out.println(name1); // {BC=0, AD=1}
final var name2 = calendar.getDisplayNames(
Calendar.ERA, Calendar.LONG_STANDALONE, Locale.US);
System.out.println(name2); // {Before Christ=0, Anno Domini=1}
static final int MARCH
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.MARCH, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.MARCH); // true
static final int MAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.MAY, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.MAY); // true
static final int MILLISECOND
final var calendar = new Calendar.Builder()
.setTimeOfDay(14, 30, 59, 999)
.build();
System.out.printf("%tT%n", calendar); // 14:30:59
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2
System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999
static final int MINUTE
final var calendar = new Calendar.Builder()
.setTimeOfDay(14, 30, 59, 999)
.build();
System.out.printf("%tT%n", calendar); // 14:30:59
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2
System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999
static final int MONDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 4)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Monday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.MONDAY); // true
static final int MONTH
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 15)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-15
System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 15
static final int NARROW_FORMAT
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name1 = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(name1); // January
final var name2 = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(name2); // Jan
final var name3 = calendar.getDisplayName(
Calendar.MONTH, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name3); // J
static final int NARROW_STANDALONE
final var calendar = new Calendar.Builder().build();
final var name1 = calendar.getDisplayNames(
Calendar.ERA, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name1); // {A=1, B=0}
final var name2 = calendar.getDisplayNames(
Calendar.ERA, Calendar.NARROW_STANDALONE, Locale.US);
System.out.println(name2); // {A=1, B=0}
static final int NOVEMBER
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.NOVEMBER, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.NOVEMBER); // true
static final int OCTOBER
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.OCTOBER, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.OCTOBER); // true
static final int PM
final var calendar = new Calendar.Builder()
.setTimeOfDay(10, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 10
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // true
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // false
final var calendar = new Calendar.Builder()
.setTimeOfDay(17, 0, 0)
.build();
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 17
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.AM); // false
System.out.println(calendar.get(Calendar.AM_PM) == Calendar.PM); // true
static final int SATURDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Saturday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.SATURDAY); // true
static final int SECOND
final var calendar = new Calendar.Builder()
.setTimeOfDay(14, 30, 59, 999)
.build();
System.out.printf("%tT%n", calendar); // 14:30:59
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.HOUR)); // 2
System.out.println(calendar.get(Calendar.MINUTE)); // 30
System.out.println(calendar.get(Calendar.SECOND)); // 59
System.out.println(calendar.get(Calendar.MILLISECOND)); // 999
static final int SEPTEMBER
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.SEPTEMBER, 1)
.build();
System.out.println(calendar.get(Calendar.MONTH) == Calendar.SEPTEMBER); // true
static final int SHORT
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name1 = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(name1); // January
final var name2 = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(name2); // Jan
final var name3 = calendar.getDisplayName(
Calendar.MONTH, Calendar.NARROW_FORMAT, Locale.US);
System.out.println(name3); // J
static final int SHORT_FORMAT
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var name1 = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(name1); // Jan
final var name2 = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.US);
System.out.println(name2); // Jan
static final int SHORT_STANDALONE
final var calendar = new Calendar.Builder().build();
final var name1 = calendar.getDisplayNames(
Calendar.ERA, Calendar.SHORT, Locale.US);
System.out.println(name1); // {AD=1, BC=0}
final var name2 = calendar.getDisplayNames(
Calendar.ERA, Calendar.SHORT_STANDALONE, Locale.US);
System.out.println(name2); // {AD=1, BC=0}
static final int SUNDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 3)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Sunday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.SUNDAY); // true
static final int THURSDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 7)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Thursday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.THURSDAY); // true
protected long time
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
static final int TUESDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 5)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Tuesday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.TUESDAY); // true
static final int UNDECIMBER
System.out.println(Calendar.UNDECIMBER); // 12
static final int WEDNESDAY
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 6)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Wednesday
final var value = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(value == Calendar.WEDNESDAY); // true
static final int WEEK_OF_MONTH
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getFirstDayOfWeek() == Calendar.SUNDAY); // true
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1
final Supplier<String> dayOfWeekName = () -> {
return calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
};
System.out.println(dayOfWeekName.get()); // Friday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 2);
System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 3);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 9);
System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 10);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 3
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 3
calendar.set(Calendar.DAY_OF_MONTH, 31);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 6
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(dayOfWeekName.get()); // Monday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6
calendar.set(Calendar.DAY_OF_MONTH, 7);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 7
static final int WEEK_OF_YEAR
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getFirstDayOfWeek() == Calendar.SUNDAY); // true
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1
final Supplier<String> dayOfWeekName = () -> {
return calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
};
System.out.println(dayOfWeekName.get()); // Friday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 2);
System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.set(Calendar.DAY_OF_MONTH, 3);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 9);
System.out.println(dayOfWeekName.get()); // Saturday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 2
calendar.set(Calendar.DAY_OF_MONTH, 10);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 3
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 3
calendar.set(Calendar.DAY_OF_MONTH, 31);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 6
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(dayOfWeekName.get()); // Monday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 6
calendar.set(Calendar.DAY_OF_MONTH, 7);
System.out.println(dayOfWeekName.get()); // Sunday
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 7
static final int YEAR
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 15)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-15
System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.JANUARY); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 15
static final int ZONE_OFFSET
final var zone = TimeZone.getTimeZone("America/Los_Angeles");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setTimeZone(zone)
.build();
System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // -28800000
コンストラクタ
Calendar ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
Calendar (TimeZone zone, Locale aLocale)
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
メソッド
abstract void add (int field, int amount)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-01
calendar.add(Calendar.DATE, 15);
System.out.printf("%tF%n", calendar); // 2100-01-16
calendar.add(Calendar.DATE, 30);
System.out.printf("%tF%n", calendar); // 2100-02-15
calendar.add(Calendar.MONTH, 100);
System.out.printf("%tF%n", calendar); // 2108-06-15
boolean after (Object when)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar2 = new Calendar.Builder()
.setDate(2099, Calendar.JANUARY, 1)
.build();
final var calendar3 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var calendar4 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02
System.out.println(calendar.after(calendar2)); // true
System.out.println(calendar.after(calendar3)); // false
System.out.println(calendar.after(calendar4)); // false
boolean before (Object when)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar2 = new Calendar.Builder()
.setDate(2099, Calendar.JANUARY, 1)
.build();
final var calendar3 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var calendar4 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02
System.out.println(calendar.before(calendar2)); // false
System.out.println(calendar.before(calendar3)); // false
System.out.println(calendar.before(calendar4)); // true
final void clear ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 10)
.setTimeOfDay(14, 30, 59)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-10 14:30:59
calendar.clear();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
final void clear (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 5)
.build();
System.out.printf("%tF%n", calendar); // 2100-08-05
calendar.clear(Calendar.YEAR);
System.out.printf("%tF%n", calendar); // 1970-08-05
calendar.clear(Calendar.MONTH);
System.out.printf("%tF%n", calendar); // 1970-01-05
calendar.clear(Calendar.DATE);
System.out.printf("%tF%n", calendar); // 1970-01-01
Object clone ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 5)
.setTimeOfDay(14, 30, 59)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-05 14:30:59
if (calendar.clone() instanceof Calendar cloned) {
System.out.printf("%tF %tT%n", cloned, cloned); // 2100-08-05 14:30:59
}
int compareTo (Calendar anotherCalendar)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar2 = new Calendar.Builder()
.setDate(2099, Calendar.JANUARY, 1)
.build();
final var calendar3 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var calendar4 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02
System.out.println(calendar.compareTo(calendar2)); // 1
System.out.println(calendar.compareTo(calendar3)); // 0
System.out.println(calendar.compareTo(calendar4)); // -1
protected void complete ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected abstract void computeFields ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
protected abstract void computeTime ()
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
boolean equals (Object obj)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar2 = new Calendar.Builder()
.setDate(2099, Calendar.JANUARY, 1)
.build();
final var calendar3 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var calendar4 = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
System.out.printf("%tF%n", calendar2); // 2099-01-01
System.out.printf("%tF%n", calendar3); // 2100-01-01
System.out.printf("%tF%n", calendar4); // 2100-01-02
System.out.println(calendar.equals(calendar2)); // false
System.out.println(calendar.equals(calendar3)); // true
System.out.println(calendar.equals(calendar4)); // false
int get (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.DECEMBER, 31)
.setTimeOfDay(14, 30, 59)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-12-31 14:30:59
System.out.println(calendar.get(Calendar.YEAR)); // 2100
System.out.println(calendar.get(Calendar.MONTH) == Calendar.DECEMBER); // true
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 14
System.out.println(calendar.get(Calendar.MINUTE)); //30
System.out.println(calendar.get(Calendar.SECOND)); // 59
int getActualMaximum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
int getActualMinimum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
static Set<String> getAvailableCalendarTypes ()
final var types = Calendar.getAvailableCalendarTypes();
for (final var type : types) {
System.out.println(type);
}
// 結果
// ↓
//gregory
//buddhist
//japanese
static Locale[] getAvailableLocales ()
final var locales = Calendar.getAvailableLocales();
Arrays.sort(locales, Comparator.comparing(Locale::toLanguageTag));
for (final var locale : locales) {
System.out.println(locale);
}
// 結果
// ↓
//af
//af_ZA_#Latn
//af_NA
//af_ZA
//agq
//agq_CM
//agq_CM_#Latn
//ak
//ak_GH
//ak_GH_#Latn
//am
//...
String getCalendarType ()
final var calendar = new Calendar.Builder().build();
final var type = calendar.getCalendarType();
System.out.println(type); // gregory
String getDisplayName (int field, int style, Locale locale)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var longName = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.US);
System.out.println(longName); // January
final var shortName = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.US);
System.out.println(shortName); // Jan
final var jpName = calendar.getDisplayName(
Calendar.MONTH, Calendar.LONG, Locale.JAPAN);
System.out.println(jpName); // 1月
Map<String,Integer> getDisplayNames (int field, int style, Locale locale)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
final var longNames = calendar.getDisplayNames(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
// {Monday=2, Thursday=5, Friday=6, Sunday=1, Wednesday=4, Tuesday=3, Saturday=7}
System.out.println(longNames);
final var shortNames = calendar.getDisplayNames(
Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
// {Thu=5, Tue=3, Wed=4, Sat=7, Fri=6, Sun=1, Mon=2}
System.out.println(shortNames);
final var jpNames = calendar.getDisplayNames(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.JAPAN);
// {木曜日=5, 日曜日=1, 火曜日=3, 金曜日=6, 土曜日=7, 月曜日=2, 水曜日=4}
System.out.println(jpNames);
int getFirstDayOfWeek ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setLocale(Locale.US)
.build();
final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.SUNDAY); // true
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setLocale(Locale.FRANCE)
.build();
final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.MONDAY); // true
abstract int getGreatestMinimum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
static Calendar getInstance ()
final var calendar = Calendar.getInstance();
System.out.printf("%tF %tT%n", calendar, calendar); // 2024-06-29 16:23:37
static Calendar getInstance (Locale aLocale)
final var calendar = Calendar.getInstance(Locale.US);
final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.SUNDAY); // true
final var calendar = Calendar.getInstance(Locale.FRANCE);
final var ret = calendar.getFirstDayOfWeek();
System.out.println(ret == Calendar.MONDAY); // true
static Calendar getInstance (TimeZone zone)
final var zone = TimeZone.getTimeZone("America/Los_Angeles");
final var calendar = Calendar.getInstance(zone);
System.out.printf("%tz%n", calendar); // -0700
System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // -28800000
static Calendar getInstance (TimeZone zone, Locale aLocale)
各パラメータについては、下記の使用例をご参照ください。
- zone パラメータ : getInstance(TimeZone zone)
- aLocale パラメータ : getInstance(Locale aLocale)
abstract int getLeastMaximum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
abstract int getMaximum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
int getMinimalDaysInFirstWeek ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
{
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.setMinimalDaysInFirstWeek(7);
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 7
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 52
}
abstract int getMinimum (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
System.out.printf("%tF%n", calendar); // 2100-01-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
System.out.printf("%tF%n", calendar); // 2100-02-01
{
System.out.println(calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 28
System.out.println(calendar.getMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getMaximum(Calendar.DAY_OF_MONTH)); // 31
System.out.println(calendar.getGreatestMinimum(Calendar.DAY_OF_MONTH)); // 1
System.out.println(calendar.getLeastMaximum(Calendar.DAY_OF_MONTH)); // 28
}
final Date getTime ()
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000
final var date = calendar.getTime();
System.out.println(date.getTime()); // 4102444800000
long getTimeInMillis ()
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000
final var date = calendar.getTime();
System.out.println(date.getTime()); // 4102444800000
TimeZone getTimeZone ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 1)
.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
.build();
final var zone1 = calendar.getTimeZone();
System.out.println(zone1.getID()); // America/Los_Angeles
calendar.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
final var zone2 = calendar.getTimeZone();
System.out.println(zone2.getID()); // Asia/Tokyo
int getWeeksInWeekYear ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
if (calendar.isWeekDateSupported()) {
System.out.println(calendar.getWeeksInWeekYear()); // 52
System.out.println(calendar.getWeekYear()); // 2100
}
int getWeekYear ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
if (calendar.isWeekDateSupported()) {
System.out.println(calendar.getWeeksInWeekYear()); // 52
System.out.println(calendar.getWeekYear()); // 2100
}
int hashCode ()
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
System.out.println(calendar.hashCode()); // 577755
calendar.set(2100, Calendar.JANUARY, 1);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.hashCode()); // 751504224
protected final int internalGet (int field)
protectedです。
独自にサブクラスを作ることは少ないと思いますので、コード例は割愛します。
boolean isLenient ()
final var calendar = new Calendar.Builder().build();
System.out.println(calendar.isLenient()); // true
{
calendar.set(2100, Calendar.JANUARY, 100);
final var date = calendar.getTime();
System.out.printf("%tF%n", date); // 2100-04-10
}
calendar.setLenient(false);
System.out.println(calendar.isLenient()); // false
try {
calendar.set(2100, Calendar.JANUARY, 100);
final var date = calendar.getTime();
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException! : " + e.getMessage());
}
// 結果
// ↓
//IllegalArgumentException! : DAY_OF_MONTH
final boolean isSet (int field)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 15)
.build();
System.out.printf("%tF%n", calendar); // 2100-08-15
System.out.println(calendar.isSet(Calendar.YEAR)); // true
System.out.println(calendar.isSet(Calendar.MONTH)); // true
System.out.println(calendar.isSet(Calendar.DAY_OF_MONTH)); // true
calendar.clear(Calendar.YEAR);
System.out.printf("%tF%n", calendar); // 1970-08-15
System.out.println(calendar.isSet(Calendar.YEAR)); // false
System.out.println(calendar.isSet(Calendar.MONTH)); // true
System.out.println(calendar.isSet(Calendar.DAY_OF_MONTH)); // true
boolean isWeekDateSupported ()
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
System.out.println(calendar.getCalendarType()); // gregory
if (calendar.isWeekDateSupported()) {
System.out.println(calendar.getWeeksInWeekYear()); // 52
System.out.println(calendar.getWeekYear()); // 2100
}
abstract void roll (int field, boolean up)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.OCTOBER, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-10-01
calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-11-01
calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-12-01
calendar.roll(Calendar.MONTH, true);
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 2)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-02
calendar.roll(Calendar.DAY_OF_MONTH, false);
System.out.printf("%tF%n", calendar); // 2100-01-01
calendar.roll(Calendar.DAY_OF_MONTH, false);
System.out.printf("%tF%n", calendar); // 2100-01-31
void roll (int field, int amount)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JULY, 1)
.build();
System.out.printf("%tF%n", calendar); // 2100-07-01
calendar.roll(Calendar.MONTH, 3);
System.out.printf("%tF%n", calendar); // 2100-10-01
calendar.roll(Calendar.MONTH, 2);
System.out.printf("%tF%n", calendar); // 2100-12-01
calendar.roll(Calendar.MONTH, 1);
System.out.printf("%tF%n", calendar); // 2100-01-01
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 3)
.build();
System.out.printf("%tF%n", calendar); // 2100-01-03
calendar.roll(Calendar.DAY_OF_MONTH, -2);
System.out.printf("%tF%n", calendar); // 2100-01-01
calendar.roll(Calendar.DAY_OF_MONTH, -1);
System.out.printf("%tF%n", calendar); // 2100-01-31
void set (int field, int value)
final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
calendar.set(Calendar.YEAR, 2100);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
calendar.set(Calendar.MONTH, Calendar.AUGUST);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-01 00:00:00
calendar.set(Calendar.DAY_OF_MONTH, 15);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 00:00:00
calendar.set(Calendar.HOUR_OF_DAY, 14);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:00:00
calendar.set(Calendar.MINUTE, 30);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:00
calendar.set(Calendar.SECOND, 59);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:59
final void set (int year, int month, int date)
final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
calendar.set(2100, Calendar.AUGUST, 15);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 00:00:00
final void set (int year, int month, int date, int hourOfDay, int minute)
final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
calendar.set(2100, Calendar.AUGUST, 15, 14, 30);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:00
final void set (int year, int month, int date, int hourOfDay, int minute, int second)
final var calendar = new Calendar.Builder().build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
calendar.set(2100, Calendar.AUGUST, 15, 14, 30, 59);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-08-15 14:30:59
void setFirstDayOfWeek (int value)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 3)
.setLocale(Locale.US)
.build();
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Sunday
System.out.println(calendar.getFirstDayOfWeek() == Calendar.SUNDAY); // true
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 2
calendar.setFirstDayOfWeek(Calendar.MONDAY);
System.out.println(calendar.getFirstDayOfWeek() == Calendar.MONDAY); // true
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH)); // 1
void setLenient (boolean lenient)
final var calendar = new Calendar.Builder().build();
System.out.println(calendar.isLenient()); // true
{
calendar.set(2100, Calendar.JANUARY, 100);
final var date = calendar.getTime();
System.out.printf("%tF%n", date); // 2100-04-10
}
calendar.setLenient(false);
System.out.println(calendar.isLenient()); // false
try {
calendar.set(2100, Calendar.JANUARY, 100);
final var date = calendar.getTime();
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException! : " + e.getMessage());
}
// 結果
// ↓
//IllegalArgumentException! : DAY_OF_MONTH
void setMinimalDaysInFirstWeek (int value)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.build();
{
final var name = calendar.getDisplayName(
Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
System.out.println(name); // Friday
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 1
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 1
calendar.setMinimalDaysInFirstWeek(7);
System.out.println(calendar.getMinimalDaysInFirstWeek()); // 7
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // 52
}
final void setTime (Date date)
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
final var date = new Date(4102444800000L);
calendar.setTime(date);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000
void setTimeInMillis (long millis)
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 1970-01-01 00:00:00
calendar.setTimeInMillis(4102444800000L);
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.getTimeInMillis()); // 4102444800000
void setTimeZone (TimeZone value)
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.AUGUST, 1)
.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
.build();
final var zone1 = calendar.getTimeZone();
System.out.println(zone1.getID()); // America/Los_Angeles
calendar.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
final var zone2 = calendar.getTimeZone();
System.out.println(zone2.getID()); // Asia/Tokyo
void setWeekDate (int weekYear, int weekOfYear, int dayOfWeek)
final var calendar = new Calendar.Builder().build();
System.out.printf("%tF%n", calendar); // 1970-01-01
calendar.setWeekDate(2100, 1, Calendar.FRIDAY);
System.out.printf("%tF%n", calendar); // 2100-01-01
calendar.setWeekDate(2100, 1, Calendar.SATURDAY);
System.out.printf("%tF%n", calendar); // 2100-01-02
calendar.setWeekDate(2100, 2, Calendar.SATURDAY);
System.out.printf("%tF%n", calendar); // 2100-01-09
calendar.setWeekDate(2100, 10, Calendar.MONDAY);
System.out.printf("%tF%n", calendar); // 2100-03-01
final Instant toInstant ()
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setTimeZone(zone)
.build();
System.out.printf("%tF %tT%n", calendar, calendar); // 2100-01-01 00:00:00
System.out.println(calendar.get(Calendar.ZONE_OFFSET)); // 0
final var instant = calendar.toInstant();
System.out.println(instant); // 2100-01-01T00:00:00Z
String toString ()
final var zone = TimeZone.getTimeZone("UTC");
final var calendar = new Calendar.Builder()
.setDate(2100, Calendar.JANUARY, 1)
.setTimeZone(zone)
.build();
final var str = calendar.toString();
// java.util.GregorianCalendar[time=4102444800000,areFieldsSet=true,
// areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo
// [id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],
// firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2100,MONTH=0,
// WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=6,
// DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,
// MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
System.out.println(str);
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock (時計)
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- DayOfWeek (曜日)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalDateTime (日時・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- Month (月)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)