Java : JapaneseDate with Examples

JapaneseDate (Java SE 20 & JDK 20) with Examples.
You will find code examples on most JapaneseDate methods.


Summary

A date in the Japanese Imperial calendar system.

Class diagram

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

final var era = date.getEra();
final var name = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 平成

final var isoDate = LocalDate.from(date);
System.out.println(isoDate); // 2000-04-15
final var date = JapaneseDate.of(JapaneseEra.REIWA, 1, 5, 10);
System.out.println(date); // Japanese Reiwa 1-05-10

final var era = date.getEra();
final var name = era.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 令和

final var isoDate = LocalDate.from(date);
System.out.println(isoDate); // 2019-05-10

Methods

final ChronoLocalDateTime<JapaneseDate> atTime (LocalTime localTime)

Combines this date with a time to create a ChronoLocalDateTime.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

{
    final var time = LocalTime.of(12, 30);
    System.out.println(time); // 12:30
    System.out.println(date.atTime(time)); // Japanese Heisei 12-04-15T12:30
}
{
    final var time = LocalTime.of(12, 30, 45);
    System.out.println(time); // 12:30:45
    System.out.println(date.atTime(time)); // Japanese Heisei 12-04-15T12:30:45
}
{
    final var time = LocalTime.of(12, 30, 45, 123456789);
    System.out.println(time); // 12:30:45.123456789
    System.out.println(date.atTime(time)); // Japanese Heisei 12-04-15T12:30:45.123456789
}

boolean equals (Object obj)

Compares this date to another date, including the chronology.

final var date1 = JapaneseDate.of(2000, 4, 15);
final var date2 = JapaneseDate.of(2000, 4, 15);

System.out.println(date1); // Japanese Heisei 12-04-15
System.out.println(date2); // Japanese Heisei 12-04-15

System.out.println(date1.equals(date2)); // true
final var date1 = JapaneseDate.of(1989, 10, 15);
final var date2 = JapaneseDate.of(2019, 10, 15);
final var date3 = JapaneseDate.of(2020, 3, 1);

System.out.println(date1); // Japanese Heisei 1-10-15
System.out.println(date2); // Japanese Reiwa 1-10-15
System.out.println(date3); // Japanese Reiwa 2-03-01

System.out.println(date1.equals(date2)); // false
System.out.println(date1.equals(date3)); // false
System.out.println(date2.equals(date3)); // false

static JapaneseDate from (TemporalAccessor temporal)

Obtains a JapaneseDate from a temporal object.

final var temporal = LocalDate.of(2000, 4, 15);
System.out.println(temporal); // 2000-04-15
System.out.println(JapaneseDate.from(temporal)); // Japanese Heisei 12-04-15
final var temporal = LocalDateTime.of(2019, 5, 10, 16, 30);
System.out.println(temporal); // 2019-05-10T16:30
System.out.println(JapaneseDate.from(temporal)); // Japanese Reiwa 1-05-10

JapaneseChronology getChronology ()

Gets the chronology of this date, which is the Japanese calendar system.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15
System.out.println(date.getChronology()); // Japanese
System.out.println(date.getEra()); // Heisei

JapaneseEra getEra ()

Gets the era applicable at this date.

Please see getChronology().

long getLong (TemporalField field)

Gets the value of the specified field as a long.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

final var year = date.getLong(ChronoField.YEAR);
System.out.println(year); // 2000

final var yearOfEra = date.getLong(ChronoField.YEAR_OF_ERA);
System.out.println(yearOfEra); // 12

final var month = date.getLong(ChronoField.MONTH_OF_YEAR);
System.out.println(month); // 4

final var day = date.getLong(ChronoField.DAY_OF_MONTH);
System.out.println(day); // 15

int hashCode ()

A hash code for this date.

final var date1 = JapaneseDate.of(1970, 1, 1);
System.out.println(date1.hashCode()); // -691981424

final var date2 = JapaneseDate.of(1970, 1, 2);
System.out.println(date2.hashCode()); // -691981421

final var date3 = JapaneseDate.of(2019, 5, 1);
System.out.println(date3.hashCode()); // -691815792

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

System.out.println("-- supported fields --");
for (final var field : ChronoField.values()) {
    if (date.isSupported(field)) {
        System.out.println(field);
    }
}

// Result
// ↓
//-- supported fields --
//DayOfWeek
//DayOfMonth
//DayOfYear
//EpochDay
//MonthOfYear
//ProlepticMonth
//YearOfEra
//Year
//Era

int lengthOfMonth ()

Returns the length of the month represented by this date.

final var date = JapaneseDate.of(2019, 1, 1);
System.out.println(date); // Japanese Heisei 31-01-01

System.out.println(date.lengthOfMonth()); // 31
final var date = JapaneseDate.of(2019, 2, 1);
System.out.println(date); // Japanese Heisei 31-02-01

System.out.println(date.isLeapYear()); // false
System.out.println(date.lengthOfMonth()); // 28
final var date = JapaneseDate.of(2020, 2, 1);
System.out.println(date); // Japanese Reiwa 2-02-01

System.out.println(date.isLeapYear()); // true
System.out.println(date.lengthOfMonth()); // 29

JapaneseDate minus (long amountToAdd, TemporalUnit unit)

Returns an object of the same type as this object with the specified period subtracted.

final var date = JapaneseDate.of(1998, 5, 20);
System.out.println(date); // Japanese Heisei 10-05-20

System.out.println(date.minus(1, ChronoUnit.DAYS)); // Japanese Heisei 10-05-19
System.out.println(date.minus(2, ChronoUnit.MONTHS)); // Japanese Heisei 10-03-20
System.out.println(date.minus(3, ChronoUnit.YEARS)); // Japanese Heisei 7-05-20

JapaneseDate minus (TemporalAmount amount)

Returns an object of the same type as this object with an amount subtracted.

final var date1 = JapaneseDate.of(1998, 5, 20);
System.out.println(date1); // Japanese Heisei 10-05-20

final var date2 = JapaneseDate.of(1998, 5, 22);
System.out.println(date2); // Japanese Heisei 10-05-22

final var amount = date1.until(date2);
System.out.println(amount); // Japanese P2D

System.out.println(date1.minus(amount)); // Japanese Heisei 10-05-18
System.out.println(date2.minus(amount)); // Japanese Heisei 10-05-20

static JapaneseDate now ()

Obtains the current JapaneseDate from the system clock in the default time-zone.

final var now = JapaneseDate.now();
System.out.println(now); // Japanese Reiwa 5-11-09

static JapaneseDate now (Clock clock)

Obtains the current JapaneseDate from the specified clock.

final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(-5));

final var now1 = JapaneseDate.now();
System.out.println(now1); // Japanese Reiwa 5-11-09

final var now2 = JapaneseDate.now(clock);
System.out.println(now2); // Japanese Reiwa 5-11-04

static JapaneseDate now (ZoneId zone)

Obtains the current JapaneseDate from the system clock in the specified time-zone.

final var zone = ZoneId.of("JST", ZoneId.SHORT_IDS);
System.out.println(zone); // Asia/Tokyo

final var now = JapaneseDate.now(zone);
System.out.println(now); // Japanese Reiwa 5-11-09
final var zone = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone); // America/Los_Angeles

final var now = JapaneseDate.now(zone);
System.out.println(now); // Japanese Reiwa 5-11-08

static JapaneseDate of (int prolepticYear, int month, int dayOfMonth)

Obtains a JapaneseDate representing a date in the Japanese calendar system from the proleptic-year, month-of-year and day-of-month fields.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

final var isoDate = LocalDate.from(date);
System.out.println(isoDate); // 2000-04-15
final var date = JapaneseDate.of(JapaneseEra.REIWA, 1, 5, 10);
System.out.println(date); // Japanese Reiwa 1-05-10

final var isoDate = LocalDate.from(date);
System.out.println(isoDate); // 2019-05-10

static JapaneseDate of (JapaneseEra era, int yearOfEra, int month, int dayOfMonth)

Obtains a JapaneseDate representing a date in the Japanese calendar system from the era, year-of-era, month-of-year and day-of-month fields.

Please see of(int prolepticYear, int month, int dayOfMonth).

JapaneseDate plus (long amountToAdd, TemporalUnit unit)

Returns an object of the same type as this object with the specified period added.

final var date = JapaneseDate.of(1998, 5, 20);
System.out.println(date); // Japanese Heisei 10-05-20

System.out.println(date.plus(1, ChronoUnit.DAYS)); // Japanese Heisei 10-05-21
System.out.println(date.plus(2, ChronoUnit.MONTHS)); // Japanese Heisei 10-07-20
System.out.println(date.plus(3, ChronoUnit.YEARS)); // Japanese Heisei 13-05-20

JapaneseDate plus (TemporalAmount amount)

Returns an object of the same type as this object with an amount added.

final var date1 = JapaneseDate.of(1998, 5, 20);
System.out.println(date1); // Japanese Heisei 10-05-20

final var date2 = JapaneseDate.of(1998, 5, 22);
System.out.println(date2); // Japanese Heisei 10-05-22

final var amount = date1.until(date2);
System.out.println(amount); // Japanese P2D

System.out.println(date1.plus(amount)); // Japanese Heisei 10-05-22
System.out.println(date2.plus(amount)); // Japanese Heisei 10-05-24

String toString ()

Returns a string representation of the object.

final var date = JapaneseDate.of(2000, 4, 15);
final var str = date.toString();
System.out.println(str); // Japanese Heisei 12-04-15
final var date = JapaneseDate.of(JapaneseEra.REIWA, 1, 5, 10);
final var str = date.toString();
System.out.println(str); // Japanese Reiwa 1-05-10

ChronoPeriod until (ChronoLocalDate endDate)

Calculates the period between this date and another date as a ChronoPeriod.

final var date1 = JapaneseDate.of(2000, 1, 15);
final var date2 = JapaneseDate.of(2001, 3, 18);

System.out.println(date1); // Japanese Heisei 12-01-15
System.out.println(date2); // Japanese Heisei 13-03-18

final var period1 = date1.until(date2);
System.out.println(period1.get(ChronoUnit.YEARS)); // 1
System.out.println(period1.get(ChronoUnit.MONTHS)); // 2
System.out.println(period1.get(ChronoUnit.DAYS)); // 3

final var period2 = date2.until(date1);
System.out.println(period2.get(ChronoUnit.YEARS)); // -1
System.out.println(period2.get(ChronoUnit.MONTHS)); // -2
System.out.println(period2.get(ChronoUnit.DAYS)); // -3

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another date in terms of the specified unit.

final var date1 = JapaneseDate.of(2000, 1, 15);
final var date2 = JapaneseDate.of(2001, 3, 18);

System.out.println(date1); // Japanese Heisei 12-01-15
System.out.println(date2); // Japanese Heisei 13-03-18

System.out.println(date1.until(date2, ChronoUnit.YEARS)); // 1
System.out.println(date1.until(date2, ChronoUnit.MONTHS)); // 14
System.out.println(date1.until(date2, ChronoUnit.DAYS)); // 427

JapaneseDate with (TemporalAdjuster adjuster)

Returns an adjusted object of the same type as this object with the adjustment made.

final var date = JapaneseDate.of(2000, 4, 15);
System.out.println(date); // Japanese Heisei 12-04-15

final var ret1 = date.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ret1); // Japanese Heisei 12-04-01

final var ret2 = date.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(ret2); // Japanese Heisei 12-04-30

JapaneseDate with (TemporalField field, long newValue)

Returns an object of the same type as this object with the specified field altered.

final var date = JapaneseDate.of(1998, 4, 15);
System.out.println(date); // Japanese Heisei 10-04-15

final var ret1 = date.with(ChronoField.YEAR_OF_ERA, 20);
System.out.println(ret1); // Japanese Heisei 20-04-15

final var ret2 = date.with(ChronoField.MONTH_OF_YEAR, 12);
System.out.println(ret2); // Japanese Heisei 10-12-15

final var ret3 = date.with(ChronoField.DAY_OF_MONTH, 30);
System.out.println(ret3); // Japanese Heisei 10-04-30

Methods declared in ChronoLocalDate

adjustInto, compareTo, format, isAfter, isBefore, isEqual, isLeapYear, isSupported, lengthOfYear, query, toEpochDay, toString, until

Please see the link below.

Methods declared in TemporalAccessor

get, range

Please see the link below.


Related posts

To top of page