Java Code Examples for org.threeten.bp.LocalDate#isBefore()

The following examples show how to use org.threeten.bp.LocalDate#isBefore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalendarPagerAdapter.java    From material-calendarview with MIT License 6 votes vote down vote up
/**
 * Clear the previous selection, select the range of days from first to last, and finally
 * invalidate. First day should be before last day, otherwise the selection won't happen.
 *
 * @param first The first day of the range.
 * @param last The last day in the range.
 * @see CalendarPagerAdapter#setDateSelected(CalendarDay, boolean)
 */
public void selectRange(final CalendarDay first, final CalendarDay last) {
  selectedDates.clear();

  // Copy to start from the first day and increment
  LocalDate temp = LocalDate.of(first.getYear(), first.getMonth(), first.getDay());

  // for comparison
  final LocalDate end = last.getDate();

  while( temp.isBefore(end) || temp.equals(end) ) {
    selectedDates.add(CalendarDay.from(temp));
    temp = temp.plusDays(1);
  }

  invalidateSelectedDates();
}
 
Example 2
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 * system from the era, year-of-era and day-of-year fields.
 * <p>
 * This returns a {@code JapaneseDate} with the specified fields.
 * The day must be valid for the year, otherwise an exception will be thrown.
 * The Japanese day-of-year is reset when the era changes.
 *
 * @param era  the Japanese era, not null
 * @param yearOfEra  the Japanese year-of-era
 * @param dayOfYear  the Japanese day-of-year, from 1 to 31
 * @return the date in Japanese calendar system, not null
 * @throws DateTimeException if the value of any field is out of range,
 *  or if the day-of-year is invalid for the year
 */
static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {
    Jdk8Methods.requireNonNull(era, "era");
    if (yearOfEra < 1) {
        throw new DateTimeException("Invalid YearOfEra: " + yearOfEra);
    }
    LocalDate eraStartDate = era.startDate();
    LocalDate eraEndDate = era.endDate();
    if (yearOfEra == 1) {
        dayOfYear += eraStartDate.getDayOfYear() - 1;
        if (dayOfYear > eraStartDate.lengthOfYear()) {
            throw new DateTimeException("DayOfYear exceeds maximum allowed in the first year of era " + era);
        }
    }
    int yearOffset = eraStartDate.getYear() - 1;
    LocalDate isoDate = LocalDate.ofYearDay(yearOfEra + yearOffset, dayOfYear);
    if (isoDate.isBefore(eraStartDate) || isoDate.isAfter(eraEndDate)) {
        throw new DateTimeException("Requested date is outside bounds of era " + era);
    }
    return new JapaneseDate(era, yearOfEra, isoDate);
}
 
Example 3
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates an instance from an ISO date.
 *
 * @param isoDate  the standard local date, validated not null
 */
JapaneseDate(LocalDate isoDate) {
    if (isoDate.isBefore(MIN_DATE)) {
        throw new DateTimeException("Minimum supported date is January 1st Meiji 6");
    }
    this.era = JapaneseEra.from(isoDate);
    int yearOffset = this.era.startDate().getYear() - 1;
    this.yearOfEra = isoDate.getYear() - yearOffset;
    this.isoDate = isoDate;
}
 
Example 4
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,
 * and {@code era} and {@code year} must agree with {@code isoDate}.
 *
 * @param era  the era, validated not null
 * @param year  the year-of-era, validated
 * @param isoDate  the standard local date, validated not null
 */
JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {
    if (isoDate.isBefore(MIN_DATE)) {
        throw new DateTimeException("Minimum supported date is January 1st Meiji 6");
    }
    this.era = era;
    this.yearOfEra = year;
    this.isoDate = isoDate;
}
 
Example 5
Source File: JapaneseEra.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Obtains an instance of {@code JapaneseEra} from a date.
 *
 * @param date  the date, not null
 * @return the Era singleton, never null
 */
static JapaneseEra from(LocalDate date) {
    if (date.isBefore(MEIJI.since)) {
        throw new DateTimeException("Date too early: " + date);
    }
    JapaneseEra[] known = KNOWN_ERAS.get();
    for (int i = known.length - 1; i >= 0; i--) {
        JapaneseEra era = known[i];
        if (date.compareTo(era.since) >= 0) {
            return era;
        }
    }
    return null;
}
 
Example 6
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 * system from the era, year-of-era, month-of-year and day-of-month fields.
 * <p>
 * This returns a {@code JapaneseDate} with the specified fields.
 * The day must be valid for the year and month, otherwise an exception will be thrown.
 * <p>
 * The Japanese month and day-of-month are the same as those in the
 * ISO calendar system. They are not reset when the era changes.
 * For example:
 * <pre>
 *  6th Jan Showa 64 = ISO 1989-01-06
 *  7th Jan Showa 64 = ISO 1989-01-07
 *  8th Jan Heisei 1 = ISO 1989-01-08
 *  9th Jan Heisei 1 = ISO 1989-01-09
 * </pre>
 *
 * @param era  the Japanese era, not null
 * @param yearOfEra  the Japanese year-of-era
 * @param month  the Japanese month-of-year, from 1 to 12
 * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
 * @return the date in Japanese calendar system, not null
 * @throws DateTimeException if the value of any field is out of range,
 *  or if the day-of-month is invalid for the month-year
 */
public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
    Jdk8Methods.requireNonNull(era, "era");
    if (yearOfEra < 1) {
        throw new DateTimeException("Invalid YearOfEra: " + yearOfEra);
    }
    LocalDate eraStartDate = era.startDate();
    LocalDate eraEndDate = era.endDate();
    int yearOffset = eraStartDate.getYear() - 1;
    LocalDate date = LocalDate.of(yearOfEra + yearOffset, month, dayOfMonth);
    if (date.isBefore(eraStartDate) || date.isAfter(eraEndDate)) {
        throw new DateTimeException("Requested date is outside bounds of era " + era);
    }
    return new JapaneseDate(era, yearOfEra, date);
}