Java : Month con ejemplos
Month (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de Month.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Un mes del año, como por ejemplo “julio”. (Traducción automática)
final var month = Month.JANUARY;
System.out.println(month); // JANUARY
System.out.println(month.getDisplayName(TextStyle.FULL, Locale.US)); // January
System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.US)); // Jan
final var date = LocalDate.of(2100, 7, 1);
System.out.println(date); // 2100-07-01
final var month = date.getMonth();
System.out.println(month); // JULY
Enum Constants
APRIL
La instancia singleton para el mes de abril con 30 días. (Traducción automática)
System.out.println(Month.APRIL); // APRIL
System.out.println(Month.APRIL.getValue()); // 4
AUGUST
La instancia singleton para el mes de agosto con 31 días. (Traducción automática)
System.out.println(Month.AUGUST); // AUGUST
System.out.println(Month.AUGUST.getValue()); // 8
DECEMBER
La instancia singleton para el mes de diciembre con 31 días. (Traducción automática)
System.out.println(Month.DECEMBER); // DECEMBER
System.out.println(Month.DECEMBER.getValue()); // 12
FEBRUARY
La instancia singleton para el mes de febrero con 28 días, o 29 en un año bisiesto. (Traducción automática)
System.out.println(Month.FEBRUARY); // FEBRUARY
System.out.println(Month.FEBRUARY.getValue()); // 2
JANUARY
La instancia singleton para el mes de enero con 31 días. (Traducción automática)
System.out.println(Month.JANUARY); // JANUARY
System.out.println(Month.JANUARY.getValue()); // 1
JULY
La instancia singleton para el mes de julio con 31 días. (Traducción automática)
System.out.println(Month.JULY); // JULY
System.out.println(Month.JULY.getValue()); // 7
JUNE
La instancia singleton para el mes de junio con 30 días. (Traducción automática)
System.out.println(Month.JUNE); // JUNE
System.out.println(Month.JUNE.getValue()); // 6
MARCH
La instancia singleton para el mes de marzo con 31 días. (Traducción automática)
System.out.println(Month.MARCH); // MARCH
System.out.println(Month.MARCH.getValue()); // 3
MAY
La instancia singleton para el mes de mayo con 31 días. (Traducción automática)
System.out.println(Month.MAY); // MAY
System.out.println(Month.MAY.getValue()); // 5
NOVEMBER
La instancia singleton para el mes de noviembre con 30 días. (Traducción automática)
System.out.println(Month.NOVEMBER); // NOVEMBER
System.out.println(Month.NOVEMBER.getValue()); // 11
OCTOBER
La instancia singleton para el mes de octubre con 31 días. (Traducción automática)
System.out.println(Month.OCTOBER); // OCTOBER
System.out.println(Month.OCTOBER.getValue()); // 10
SEPTEMBER
La instancia singleton para el mes de septiembre con 30 días. (Traducción automática)
System.out.println(Month.SEPTEMBER); // SEPTEMBER
System.out.println(Month.SEPTEMBER.getValue()); // 9
Methods
Temporal adjustInto (Temporal temporal)
Ajusta el objeto temporal especificado para tener este mes del año. (Traducción automática)
final var temporal = LocalDate.of(2100, 3, 15);
System.out.println(temporal); // 2100-03-15
final var ret1 = Month.FEBRUARY.adjustInto(temporal);
System.out.println(ret1); // 2100-02-15
final var ret2 = Month.MARCH.adjustInto(temporal);
System.out.println(ret2); // 2100-03-15
final var ret3 = Month.APRIL.adjustInto(temporal);
System.out.println(ret3); // 2100-04-15
int firstDayOfYear (boolean leapYear)
Obtiene el día del año correspondiente al primer día de este mes. (Traducción automática)
System.out.println(Month.JANUARY.firstDayOfYear(false)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(false)); // 32
System.out.println(Month.MARCH.firstDayOfYear(false)); // 60
System.out.println(Month.APRIL.firstDayOfYear(false)); // 91
System.out.println(Month.JANUARY.firstDayOfYear(true)); // 1
System.out.println(Month.FEBRUARY.firstDayOfYear(true)); // 32
System.out.println(Month.MARCH.firstDayOfYear(true)); // 61
System.out.println(Month.APRIL.firstDayOfYear(true)); // 92
Month firstMonthOfQuarter ()
Obtiene el mes correspondiente al primer mes de este trimestre. (Traducción automática)
System.out.println(Month.JANUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.FEBRUARY.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.MARCH.firstMonthOfQuarter()); // JANUARY
System.out.println(Month.APRIL.firstMonthOfQuarter()); // APRIL
System.out.println(Month.MAY.firstMonthOfQuarter()); // APRIL
System.out.println(Month.JUNE.firstMonthOfQuarter()); // APRIL
System.out.println(Month.JULY.firstMonthOfQuarter()); // JULY
System.out.println(Month.AUGUST.firstMonthOfQuarter()); // JULY
System.out.println(Month.SEPTEMBER.firstMonthOfQuarter()); // JULY
System.out.println(Month.OCTOBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.NOVEMBER.firstMonthOfQuarter()); // OCTOBER
System.out.println(Month.DECEMBER.firstMonthOfQuarter()); // OCTOBER
static Month from (TemporalAccessor temporal)
Obtiene una instancia de Mes de un objeto temporal. (Traducción automática)
final var temporal = LocalDate.of(2100, 1, 15);
System.out.println(temporal); // 2100-01-15
System.out.println(Month.from(temporal)); // JANUARY
final var temporal = LocalDate.of(1999, 2, 28);
System.out.println(temporal); // 1999-02-28
System.out.println(Month.from(temporal)); // FEBRUARY
int get (TemporalField field)
Obtiene el valor del campo especificado de este mes del año como un int. (Traducción automática)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
String getDisplayName (TextStyle style, Locale locale)
Obtiene la representación textual, como 'enero' o 'diciembre'. (Traducción automática)
final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US
System.out.println(Month.JANUARY.getDisplayName(TextStyle.FULL, locale)); // January
System.out.println(Month.JANUARY.getDisplayName(TextStyle.SHORT, locale)); // Jan
System.out.println(Month.JANUARY.getDisplayName(TextStyle.NARROW, locale)); // J
long getLong (TemporalField field)
Obtiene el valor del campo especificado de este mes del año como un valor largo. (Traducción automática)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.getLong(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.getLong(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
int getValue ()
Obtiene el valor int del mes del año. (Traducción automática)
System.out.println(Month.JANUARY.getValue()); // 1
System.out.println(Month.FEBRUARY.getValue()); // 2
System.out.println(Month.MARCH.getValue()); // 3
System.out.println(Month.APRIL.getValue()); // 4
System.out.println(Month.MAY.getValue()); // 5
System.out.println(Month.JUNE.getValue()); // 6
System.out.println(Month.JULY.getValue()); // 7
System.out.println(Month.AUGUST.getValue()); // 8
System.out.println(Month.SEPTEMBER.getValue()); // 9
System.out.println(Month.OCTOBER.getValue()); // 10
System.out.println(Month.NOVEMBER.getValue()); // 11
System.out.println(Month.DECEMBER.getValue()); // 12
boolean isSupported (TemporalField field)
Comprueba si el campo especificado es compatible. (Traducción automática)
System.out.println(Month.JANUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.JANUARY.get(ChronoField.MONTH_OF_YEAR)); // 1
System.out.println(Month.FEBRUARY.isSupported(ChronoField.MONTH_OF_YEAR)); // true
System.out.println(Month.FEBRUARY.get(ChronoField.MONTH_OF_YEAR)); // 2
System.out.println(Month.FEBRUARY.isSupported(ChronoField.YEAR)); // false
try {
final var ret = Month.FEBRUARY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
int length (boolean leapYear)
Obtiene la duración de este mes en días. (Traducción automática)
System.out.println(Month.JANUARY.length(false)); // 31
System.out.println(Month.FEBRUARY.length(false)); // 28
System.out.println(Month.MARCH.length(false)); // 31
System.out.println(Month.APRIL.length(false)); // 30
System.out.println(Month.JANUARY.length(true)); // 31
System.out.println(Month.FEBRUARY.length(true)); // 29
System.out.println(Month.MARCH.length(true)); // 31
System.out.println(Month.APRIL.length(true)); // 30
int maxLength ()
Obtiene la longitud máxima de este mes en días. (Traducción automática)
System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31
System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29
System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31
System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30
int minLength ()
Obtiene la duración mínima de este mes en días. (Traducción automática)
System.out.println(Month.JANUARY.minLength()); // 31
System.out.println(Month.JANUARY.maxLength()); // 31
System.out.println(Month.FEBRUARY.minLength()); // 28
System.out.println(Month.FEBRUARY.maxLength()); // 29
System.out.println(Month.MARCH.minLength()); // 31
System.out.println(Month.MARCH.maxLength()); // 31
System.out.println(Month.APRIL.minLength()); // 30
System.out.println(Month.APRIL.maxLength()); // 30
Month minus (long months)
Devuelve el mes del año que es el número especificado de meses antes de este. (Traducción automática)
final var month = Month.FEBRUARY;
System.out.println(month.getValue()); // 2
final var ret1 = month.minus(1);
System.out.println(ret1.getValue()); // 1
final var ret2 = month.minus(2);
System.out.println(ret2.getValue()); // 12
final var ret3 = month.minus(11);
System.out.println(ret3.getValue()); // 3
final var ret4 = month.minus(12);
System.out.println(ret4.getValue()); // 2
static Month of (int month)
Obtiene una instancia de Mes a partir de un valor int. (Traducción automática)
System.out.println(Month.of(1)); // JANUARY
System.out.println(Month.of(2)); // FEBRUARY
System.out.println(Month.of(3)); // MARCH
System.out.println(Month.of(4)); // APRIL
System.out.println(Month.of(5)); // MAY
System.out.println(Month.of(6)); // JUNE
System.out.println(Month.of(7)); // JULY
System.out.println(Month.of(8)); // AUGUST
System.out.println(Month.of(9)); // SEPTEMBER
System.out.println(Month.of(10)); // OCTOBER
System.out.println(Month.of(11)); // NOVEMBER
System.out.println(Month.of(12)); // DECEMBER
Month plus (long months)
Devuelve el mes del año que es el número especificado de meses después de éste. (Traducción automática)
final var month = Month.NOVEMBER;
System.out.println(month.getValue()); // 11
final var ret1 = month.plus(1);
System.out.println(ret1.getValue()); // 12
final var ret2 = month.plus(2);
System.out.println(ret2.getValue()); // 1
final var ret3 = month.plus(11);
System.out.println(ret3.getValue()); // 10
final var ret4 = month.plus(12);
System.out.println(ret4.getValue()); // 11
<R> R query (TemporalQuery<R> query)
Consulta este mes del año utilizando la consulta especificada. (Traducción automática)
System.out.println(Month.JANUARY.query(TemporalQueries.precision())); // Months
ValueRange range (TemporalField field)
Obtiene el rango de valores válidos para el campo especificado. (Traducción automática)
System.out.println(Month.JANUARY.range(ChronoField.MONTH_OF_YEAR)); // 1 - 12
static Month valueOf (String name)
Devuelve la constante de enumeración de esta clase con el nombre especificado. (Traducción automática)
System.out.println(Month.valueOf("JANUARY")); // JANUARY
System.out.println(Month.valueOf("FEBRUARY")); // FEBRUARY
System.out.println(Month.valueOf("MARCH")); // MARCH
static Month[] values ()
Devuelve una matriz que contiene las constantes de esta clase de enumeración, en el orden en que se declaran. (Traducción automática)
for (final var month : Month.values()) {
System.out.println(month);
}
// Result
// ↓
//JANUARY
//FEBRUARY
//MARCH
//APRIL
//MAY
//JUNE
//JULY
//AUGUST
//SEPTEMBER
//OCTOBER
//NOVEMBER
//DECEMBER
Methods declared in Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Consulte el siguiente enlace.
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset