val yearMonth = new YearMonth(); val formatter = DateTimeFormat.forPattern("yyyyMM"); val dates = (0 to 5).map { i=>formatter.print(yearMonth.plusMonths(i)).toInt }.toList
YearMonth ym = YearMonth.of(2000, 2); System.out.println(ym.lengthOfMonth()); //29 as expected
/** * Combines this year with a month to create a {@code YearMonth}. * <p> * This returns a {@code YearMonth} formed from this year and the specified month. * All possible combinations of year and month are valid. * <p> * This method can be used as part of a chain to produce a date: * <pre> * LocalDate date = year.atMonth(month).atDay(day); * </pre> * * @param month the month-of-year to use, not null * @return the year-month formed from this year and the specified month, not null */ public YearMonth atMonth(Month month) { return YearMonth.of(year, month); }
YearMonth yearMonth = new YearMonth(); yearMonth.toLocalDate(1).dayOfMonth().getMaximumValue();
public List<LocalDate> getDateRange(YearMonth yearMonth){ List<LocalDate> dateRange = new ArrayList<>(); IntStream.of(yearMonth.lengthOfMonth()).foreach(day -> dateRange.add(yearMonth.at(day)); return dateRange }
YearMonth ym = new YearMonth(); dt = ym.toDateTime(null); System.out.println(dt.dayOfMonth().getMaximumValue());
f.checkValidValue(newValue); switch (f) { case MONTH_OF_YEAR: return withMonth((int) newValue); case PROLEPTIC_MONTH: return plusMonths(newValue - getLong(PROLEPTIC_MONTH)); case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
@Override public void serialize(YearMonth yearMonth, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { generator.writeStartArray(); generator.writeNumber(yearMonth.getYear()); generator.writeNumber(yearMonth.getMonthValue()); generator.writeEndArray(); } else { String str = (_formatter == null) ? yearMonth.toString() : yearMonth.format(_formatter); generator.writeString(str); } }
YearMonth end = YearMonth.from(endExclusive); if (unit instanceof ChronoUnit) { long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow switch ((ChronoUnit) unit) { case MONTHS: return monthsUntil; case CENTURIES: return monthsUntil / 1200; case MILLENNIA: return monthsUntil / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA);
import org.joda.time.YearMonth; import java.util.Locale; public class X { public static void main(String[] s){ for (int i = 1;i<13; i++) { YearMonth md = new YearMonth(1992, i); System.out.println(md.monthOfYear().getAsText(new Locale("ru"))); } } }
YearMonth yearMonth = new YearMonth (1970, 1); Interval interval = yearMonth.toInterval(DateTimeZone.UTC); int startSSE = (int) TimeUnit.MILLISECONDS.toSeconds(interval.getStartMillis()); int endSSE = (int) TimeUnit.MILLISECONDS.toSeconds(interval.getEndMillis()); System.out.printf("%d -> %d (%4.6f days)\n", startSSE, endSSE, ((float)(endSSE-startSSE))/(24*60*60));
public static void main(String[] args) { System.out.println(mapToCurrentCentury(16, 11)); System.out.println(mapToCurrentCentury(116, 11)); System.out.println(mapToCurrentCentury(1116, 11)); } private static YearMonth mapToCurrentCentury(int year, int month) { if(year < 1000) { YearMonth now = YearMonth.now(); return now.withYear(((now.get(ChronoField.YEAR) / 1000) * 1000) + (year % 1000)).withMonth(month); } else { return YearMonth.of(year, month); } }
/** * Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}. * <p> * The string must represent a valid year-month. * The format must be {@code yyyy-MM}. * Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol. * * @param text the text to parse such as "2007-12", not null * @return the parsed year-month, not null * @throws DateTimeParseException if the text cannot be parsed */ public static YearMonth parse(CharSequence text) { return parse(text, PARSER); }
/** * Gets the range of valid values for the specified field. * <p> * The range object expresses the minimum and maximum valid values for a field. * This year-month is used to enhance the accuracy of the returned range. * If it is not possible to return the range, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. * All other {@code ChronoField} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param field the field to query the range for, not null * @return the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained */ @Override public ValueRange range(TemporalField field) { if (field == YEAR_OF_ERA) { return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); } return super.range(field); }
/** * Is this year-month after the specified year-month. * * @param other the other year-month to compare to, not null * @return true if this is after the specified year-month */ public boolean isAfter(YearMonth other) { return compareTo(other) > 0; }
@Override public YearMonth queryFrom(TemporalAccessor temporal) { return YearMonth.from(temporal); } };
f.checkValidValue(newValue); switch (f) { case MONTH_OF_YEAR: return withMonth((int) newValue); case PROLEPTIC_MONTH: return plusMonths(newValue - getLong(PROLEPTIC_MONTH)); case YEAR_OF_ERA: return withYear((int) (year < 1 ? 1 - newValue : newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
YearMonth end = YearMonth.from(endExclusive); if (unit instanceof ChronoUnit) { long monthsUntil = end.getProlepticMonth() - getProlepticMonth(); // no overflow switch ((ChronoUnit) unit) { case MONTHS: return monthsUntil; case CENTURIES: return monthsUntil / 1200; case MILLENNIA: return monthsUntil / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA);
/** * Obtains an instance of {@code YearMonth} from a text string such as {@code 2007-12}. * <p> * The string must represent a valid year-month. * The format must be {@code yyyy-MM}. * Years outside the range 0000 to 9999 must be prefixed by the plus or minus symbol. * * @param text the text to parse such as "2007-12", not null * @return the parsed year-month, not null * @throws DateTimeParseException if the text cannot be parsed */ public static YearMonth parse(CharSequence text) { return parse(text, PARSER); }