Java Code Examples for java.time.LocalDate#from()

The following examples show how to use java.time.LocalDate#from() . 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: IsoFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
    if (isSupportedBy(temporal) == false) {
        throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
    }
    int newWby = range().checkValidIntValue(newValue, WEEK_BASED_YEAR);  // strict check
    LocalDate date = LocalDate.from(temporal);
    int dow = date.get(DAY_OF_WEEK);
    int week = getWeek(date);
    if (week == 53 && getWeekRange(newWby) == 52) {
        week = 52;
    }
    LocalDate resolved = LocalDate.of(newWby, 1, 4);  // 4th is guaranteed to be in week one
    int days = (dow - resolved.get(DAY_OF_WEEK)) + ((week - 1) * 7);
    resolved = resolved.plusDays(days);
    return (R) temporal.with(resolved);
}
 
Example 2
Source File: IsoFields.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
    if (isSupportedBy(temporal) == false) {
        throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
    }
    int newWby = range().checkValidIntValue(newValue, WEEK_BASED_YEAR);  // strict check
    LocalDate date = LocalDate.from(temporal);
    int dow = date.get(DAY_OF_WEEK);
    int week = getWeek(date);
    if (week == 53 && getWeekRange(newWby) == 52) {
        week = 52;
    }
    LocalDate resolved = LocalDate.of(newWby, 1, 4);  // 4th is guaranteed to be in week one
    int days = (dow - resolved.get(DAY_OF_WEEK)) + ((week - 1) * 7);
    resolved = resolved.plusDays(days);
    return (R) temporal.with(resolved);
}
 
Example 3
Source File: IsoFields.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
    if (isSupportedBy(temporal) == false) {
        throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
    }
    int newWby = range().checkValidIntValue(newValue, WEEK_BASED_YEAR);  // strict check
    LocalDate date = LocalDate.from(temporal);
    int dow = date.get(DAY_OF_WEEK);
    int week = getWeek(date);
    if (week == 53 && getWeekRange(newWby) == 52) {
        week = 52;
    }
    LocalDate resolved = LocalDate.of(newWby, 1, 4);  // 4th is guaranteed to be in week one
    int days = (dow - resolved.get(DAY_OF_WEEK)) + ((week - 1) * 7);
    resolved = resolved.plusDays(days);
    return (R) temporal.with(resolved);
}
 
Example 4
Source File: IsoFields.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
    if (isSupportedBy(temporal) == false) {
        throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear");
    }
    int newVal = range().checkValidIntValue(newValue, WEEK_BASED_YEAR);  // strict check
    LocalDate date = LocalDate.from(temporal);
    int week = getWeek(date);
    date = date.withDayOfYear(180).withYear(newVal).with(WEEK_OF_WEEK_BASED_YEAR, week);
    return (R) date.with(date);
}
 
Example 5
Source File: ThaiBuddhistChronology.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ThaiBuddhistDate date(TemporalAccessor temporal) {
    if (temporal instanceof ThaiBuddhistDate) {
        return (ThaiBuddhistDate) temporal;
    }
    return new ThaiBuddhistDate(LocalDate.from(temporal));
}
 
Example 6
Source File: HurricaneSeasonQuery.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean queryFrom(TemporalAccessor temporal) {

	LocalDate date = LocalDate.from(temporal);
	
	MonthDay juneFirst = MonthDay.of(Month.JUNE.getValue(), 1);
	MonthDay novemberThirty = MonthDay.of(Month.NOVEMBER.getValue(), 30);
	
	if (date.isAfter(juneFirst.atYear(date.getYear())) 
			&& date.isBefore(novemberThirty.atYear(date.getYear()))) {
		return true;
	} else {
		return false;
	}
}
 
Example 7
Source File: ThaiBuddhistChronology.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ThaiBuddhistDate date(TemporalAccessor temporal) {
    if (temporal instanceof ThaiBuddhistDate) {
        return (ThaiBuddhistDate) temporal;
    }
    return new ThaiBuddhistDate(LocalDate.from(temporal));
}
 
Example 8
Source File: JapaneseChronology.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JapaneseDate date(TemporalAccessor temporal) {
    if (temporal instanceof JapaneseDate) {
        return (JapaneseDate) temporal;
    }
    return new JapaneseDate(LocalDate.from(temporal));
}
 
Example 9
Source File: JapaneseChronology.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JapaneseDate date(TemporalAccessor temporal) {
    if (temporal instanceof JapaneseDate) {
        return (JapaneseDate) temporal;
    }
    return new JapaneseDate(LocalDate.from(temporal));
}
 
Example 10
Source File: TCKTestServiceLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
 public void test_TestServiceLoader() {
    Chronology chrono = Chronology.of("Coptic");
    ChronoLocalDate copticDate = chrono.date(1729, 4, 27);
    LocalDate ld = LocalDate.from(copticDate);
    assertEquals(ld, LocalDate.of(2013, 1, 5), "CopticDate does not match LocalDate");
}
 
Example 11
Source File: MinguoChronology.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MinguoDate date(TemporalAccessor temporal) {
    if (temporal instanceof MinguoDate) {
        return (MinguoDate) temporal;
    }
    return new MinguoDate(LocalDate.from(temporal));
}
 
Example 12
Source File: TCKTestServiceLoader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
 public void test_TestServiceLoader() {
    Chronology chrono = Chronology.of("Coptic");
    ChronoLocalDate copticDate = chrono.date(1729, 4, 27);
    LocalDate ld = LocalDate.from(copticDate);
    assertEquals(ld, LocalDate.of(2013, 1, 5), "CopticDate does not match LocalDate");
}
 
Example 13
Source File: TCKLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    LocalDate.from((TemporalAccessor) null);
}
 
Example 14
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    LocalDate.from((TemporalAccessor) null);
}
 
Example 15
Source File: TCKLocalDate.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    LocalDate.from((TemporalAccessor) null);
}
 
Example 16
Source File: LocalDateJavaDescriptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public LocalDate fromString(String string) {
	return LocalDate.from( LocalDateType.FORMATTER.parse( string ) );
}
 
Example 17
Source File: TemporalAdjusters.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
 * <p>
 * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
 * This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
 * to be wrapped to match the temporal-based interface.
 * This is provided for convenience to make user-written adjusters simpler.
 * <p>
 * In general, user-written adjusters should be static constants:
 * <pre>{@code
 *  static TemporalAdjuster TWO_DAYS_LATER =
 *       TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
 * }</pre>
 *
 * @param dateBasedAdjuster  the date-based adjuster, not null
 * @return the temporal adjuster wrapping on the date adjuster, not null
 */
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
    Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
    return (temporal) -> {
        LocalDate input = LocalDate.from(temporal);
        LocalDate output = dateBasedAdjuster.apply(input);
        return temporal.with(output);
    };
}
 
Example 18
Source File: TemporalAdjusters.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
 * <p>
 * The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
 * This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
 * to be wrapped to match the temporal-based interface.
 * This is provided for convenience to make user-written adjusters simpler.
 * <p>
 * In general, user-written adjusters should be static constants:
 * <pre>{@code
 *  static TemporalAdjuster TWO_DAYS_LATER =
 *       TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
 * }</pre>
 *
 * @param dateBasedAdjuster  the date-based adjuster, not null
 * @return the temporal adjuster wrapping on the date adjuster, not null
 */
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
    Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
    return (temporal) -> {
        LocalDate input = LocalDate.from(temporal);
        LocalDate output = dateBasedAdjuster.apply(input);
        return temporal.with(output);
    };
}
 
Example 19
Source File: IsoChronology.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date, not null
 * @throws DateTimeException if unable to create the date
 */
@Override  // override with covariant return type
public LocalDate date(TemporalAccessor temporal) {
    return LocalDate.from(temporal);
}
 
Example 20
Source File: IsoChronology.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date, not null
 * @throws DateTimeException if unable to create the date
 */
@Override  // override with covariant return type
public LocalDate date(TemporalAccessor temporal) {
    return LocalDate.from(temporal);
}