Java : JapaneseDate with Examples
JapaneseDate (Java SE 21 & JDK 21) with Examples.
You will find code examples on most JapaneseDate methods.
Summary
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)
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)
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)
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 ()
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 ()
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
long getLong (TemporalField field)
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 ()
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)
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 ()
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 amountToSubtract, TemporalUnit unit)
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)
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 ()
final var now = JapaneseDate.now();
System.out.println(now); // Japanese Reiwa 6-07-18
static JapaneseDate now (Clock clock)
// A clock advanced 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));
final var now1 = JapaneseDate.now();
System.out.println(now1); // Japanese Reiwa 6-07-18
final var now2 = JapaneseDate.now(clock);
System.out.println(now2); // Japanese Reiwa 6-07-23
static JapaneseDate now (ZoneId 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 6-07-18
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 6-07-17
static JapaneseDate of (int prolepticYear, int month, int dayOfMonth)
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)
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
JapaneseDate plus (long amountToAdd, TemporalUnit unit)
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)
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 ()
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)
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)
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)
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)
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
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset