Java : MonthDay con ejemplos
MonthDay (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de MonthDay.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15
Methods
Temporal adjustInto (Temporal temporal)
final var monthDay = MonthDay.of(Month.DECEMBER, 15);
System.out.println(monthDay); // --12-15
final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03
final var ret = monthDay.adjustInto(date);
System.out.println(ret); // 2100-12-15
LocalDate atYear (int year)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.atYear(2100)); // 2100-04-15
System.out.println(monthDay.atYear(1800)); // 1800-04-15
int compareTo (MonthDay other)
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --01-15
System.out.println(monthDay1.compareTo(monthDay2)); // 0
System.out.println(monthDay2.compareTo(monthDay1)); // 0
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.FEBRUARY, 15);
System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --02-15
System.out.println(monthDay1.compareTo(monthDay2)); // -1
System.out.println(monthDay2.compareTo(monthDay1)); // 1
final var monthDay1 = MonthDay.of(Month.APRIL, 2);
final var monthDay2 = MonthDay.of(Month.APRIL, 1);
System.out.println(monthDay1); // --04-02
System.out.println(monthDay2); // --04-01
System.out.println(monthDay1.compareTo(monthDay2)); // 1
System.out.println(monthDay2.compareTo(monthDay1)); // -1
boolean equals (Object obj)
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --01-15
System.out.println(monthDay1.equals(monthDay2)); // true
System.out.println(monthDay2.equals(monthDay1)); // true
final var monthDay1 = MonthDay.of(Month.JANUARY, 15);
final var monthDay2 = MonthDay.of(Month.FEBRUARY, 15);
System.out.println(monthDay1); // --01-15
System.out.println(monthDay2); // --02-15
System.out.println(monthDay1.equals(monthDay2)); // false
System.out.println(monthDay2.equals(monthDay1)); // false
final var monthDay1 = MonthDay.of(Month.APRIL, 2);
final var monthDay2 = MonthDay.of(Month.APRIL, 1);
System.out.println(monthDay1); // --04-02
System.out.println(monthDay2); // --04-01
System.out.println(monthDay1.equals(monthDay2)); // false
System.out.println(monthDay2.equals(monthDay1)); // false
String format (DateTimeFormatter formatter)
final var monthDay = MonthDay.of(Month.APRIL, 1);
System.out.println(monthDay); // --04-01
final var ret1 = monthDay.format(DateTimeFormatter.ofPattern("MM-dd"));
System.out.println(ret1); // 04-01
final var ret2 = monthDay.format(DateTimeFormatter.ofPattern("M-d"));
System.out.println(ret2); // 4-1
static MonthDay from (TemporalAccessor temporal)
final var date = LocalDate.of(2100, 2, 3);
System.out.println(date); // 2100-02-03
final var ret = MonthDay.from(date);
System.out.println(ret); // --02-03
final var dateTime = LocalDateTime.of(1999, 12, 31, 7, 30);
System.out.println(dateTime); // 1999-12-31T07:30
final var ret = MonthDay.from(dateTime);
System.out.println(ret); // --12-31
int get (TemporalField field)
final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15
System.out.println(monthDay.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(monthDay.get(ChronoField.DAY_OF_MONTH)); // 15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31
System.out.println(monthDay.get(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(monthDay.get(ChronoField.DAY_OF_MONTH)); // 31
int getDayOfMonth ()
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15
long getLong (TemporalField field)
final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15
System.out.println(monthDay.getLong(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(monthDay.getLong(ChronoField.DAY_OF_MONTH)); // 15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31
System.out.println(monthDay.getLong(ChronoField.MONTH_OF_YEAR)); // 12
System.out.println(monthDay.getLong(ChronoField.DAY_OF_MONTH)); // 31
Month getMonth ()
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15
int getMonthValue ()
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.getMonth()); // APRIL
System.out.println(monthDay.getMonthValue()); // 4
System.out.println(monthDay.getDayOfMonth()); // 15
int hashCode ()
final var monthDay = MonthDay.of(Month.JANUARY, 15);
System.out.println(monthDay); // --01-15
System.out.println(monthDay.hashCode()); // 79
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
System.out.println(monthDay); // --12-31
System.out.println(monthDay.hashCode()); // 799
boolean isAfter (MonthDay other)
final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01
final var monthDay2 = MonthDay.of(Month.JANUARY, 31);
final var monthDay3 = MonthDay.of(Month.FEBRUARY, 1);
final var monthDay4 = MonthDay.of(Month.FEBRUARY, 2);
System.out.println(monthDay2); // --01-31
System.out.println(monthDay3); // --02-01
System.out.println(monthDay4); // --02-02
System.out.println(monthDay.isAfter(monthDay2)); // true
System.out.println(monthDay.isAfter(monthDay3)); // false
System.out.println(monthDay.isAfter(monthDay4)); // false
boolean isBefore (MonthDay other)
final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01
final var monthDay2 = MonthDay.of(Month.JANUARY, 31);
final var monthDay3 = MonthDay.of(Month.FEBRUARY, 1);
final var monthDay4 = MonthDay.of(Month.FEBRUARY, 2);
System.out.println(monthDay2); // --01-31
System.out.println(monthDay3); // --02-01
System.out.println(monthDay4); // --02-02
System.out.println(monthDay.isBefore(monthDay2)); // true
System.out.println(monthDay.isBefore(monthDay3)); // false
System.out.println(monthDay.isBefore(monthDay4)); // false
boolean isSupported (TemporalField field)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
System.out.println(monthDay.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(monthDay.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println(monthDay.isSupported(ChronoField.YEAR)); // false
boolean isValidYear (int year)
final var monthDay = MonthDay.of(Month.FEBRUARY, 29);
System.out.println(monthDay); // --02-29
System.out.println(Year.isLeap(2103)); // false
System.out.println(monthDay.isValidYear(2103)); // false
System.out.println(Year.isLeap(2104)); // true
System.out.println(monthDay.isValidYear(2104)); // true
static MonthDay now ()
final var now = MonthDay.now();
System.out.println(now); // --10-19
static MonthDay now (Clock clock)
// A clock advanced 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));
System.out.println(MonthDay.now()); // --10-19
System.out.println(MonthDay.now(clock)); // --10-24
static MonthDay now (ZoneId zone)
final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z
final var now = MonthDay.now(zone);
System.out.println(now); // --10-19
static MonthDay of (int month, int dayOfMonth)
final var monthDay = MonthDay.of(1, 2);
System.out.println(monthDay.getMonth()); // JANUARY
System.out.println(monthDay.getMonthValue()); // 1
System.out.println(monthDay.getDayOfMonth()); // 2
final var monthDay = MonthDay.of(12, 15);
System.out.println(monthDay.getMonth()); // DECEMBER
System.out.println(monthDay.getMonthValue()); // 12
System.out.println(monthDay.getDayOfMonth()); // 15
static MonthDay of (Month month, int dayOfMonth)
final var monthDay = MonthDay.of(Month.JANUARY, 2);
System.out.println(monthDay.getMonth()); // JANUARY
System.out.println(monthDay.getMonthValue()); // 1
System.out.println(monthDay.getDayOfMonth()); // 2
final var monthDay = MonthDay.of(Month.DECEMBER, 15);
System.out.println(monthDay.getMonth()); // DECEMBER
System.out.println(monthDay.getMonthValue()); // 12
System.out.println(monthDay.getDayOfMonth()); // 15
static MonthDay parse (CharSequence text)
final var monthDay1 = MonthDay.parse("--04-05");
System.out.println(monthDay1); // --04-05
final var monthDay2 = MonthDay.parse("--12-31");
System.out.println(monthDay2); // --12-31
static MonthDay parse (CharSequence text, DateTimeFormatter formatter)
final var monthDay1 = MonthDay.parse(
"04-05", DateTimeFormatter.ofPattern("MM-dd"));
System.out.println(monthDay1); // --04-05
final var monthDay2 = MonthDay.parse(
"8-1", DateTimeFormatter.ofPattern("M-d"));
System.out.println(monthDay2); // --08-01
<R> R query (TemporalQuery<R> query)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
final var ret = monthDay.query(TemporalQueries.chronology());
System.out.println(ret); // ISO
ValueRange range (TemporalField field)
final var monthDay = MonthDay.of(Month.JANUARY, 1);
System.out.println(monthDay); // --01-01
System.out.println(monthDay.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(monthDay.range(ChronoField.DAY_OF_MONTH)); // 1 - 31
final var monthDay = MonthDay.of(Month.FEBRUARY, 1);
System.out.println(monthDay); // --02-01
System.out.println(monthDay.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
System.out.println(monthDay.range(ChronoField.DAY_OF_MONTH)); // 1 - 28/29
String toString ()
final var monthDay = MonthDay.of(Month.JANUARY, 15);
final var str = monthDay.toString();
System.out.println(str); // --01-15
final var monthDay = MonthDay.of(Month.DECEMBER, 31);
final var str = monthDay.toString();
System.out.println(str); // --12-31
MonthDay with (Month month)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15
final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30
final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15
MonthDay withDayOfMonth (int dayOfMonth)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15
final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30
final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15
MonthDay withMonth (int month)
final var monthDay = MonthDay.of(Month.APRIL, 15);
System.out.println(monthDay); // --04-15
final var ret1 = monthDay.with(Month.DECEMBER);
System.out.println(ret1); // --12-15
final var ret2 = monthDay.withDayOfMonth(30);
System.out.println(ret2); // --04-30
final var ret3 = monthDay.withMonth(1);
System.out.println(ret3); // --01-15
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset