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

The following examples show how to use java.time.LocalDate#with() . 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: TCKWeekFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example 2
Source File: TCKWeekFields.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example 3
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example 4
Source File: TestLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 5
Source File: TCKLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate next(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() + 1;
    if (newDayOfMonth <= date.getMonth().length(isIsoLeap(date.getYear()))) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.withDayOfMonth(1);
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() + 1);
    }
    return date.with(date.getMonth().plus(1));
}
 
Example 6
Source File: TCKWeekFields.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = weekDef.weekOfMonth();
    TemporalField woyField = weekDef.weekOfYear();

    LocalDate day = LocalDate.of(2012, 11, 30);
    LocalDate endDay = LocalDate.of(2013, 1, 2);
    while (day.isBefore(endDay)) {
        LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
        int lastWOM = last.get(womField);
        LocalDate first = day.with(DAY_OF_MONTH, 1);
        int firstWOM = first.get(womField);
        ValueRange rangeWOM = day.range(womField);
        assertEquals(rangeWOM.getMinimum(), firstWOM,
                "Range min should be same as WeekOfMonth for first day of month: "
                + first + ", " + weekDef);
        assertEquals(rangeWOM.getMaximum(), lastWOM,
                "Range max should be same as WeekOfMonth for last day of month: "
                + last + ", " + weekDef);

        last = day.with(DAY_OF_YEAR, day.lengthOfYear());
        int lastWOY = last.get(woyField);
        first = day.with(DAY_OF_YEAR, 1);
        int firstWOY = first.get(woyField);
        ValueRange rangeWOY = day.range(woyField);
        assertEquals(rangeWOY.getMinimum(), firstWOY,
                "Range min should be same as WeekOfYear for first day of Year: "
                + day + ", " + weekDef);
        assertEquals(rangeWOY.getMaximum(), lastWOY,
                "Range max should be same as WeekOfYear for last day of Year: "
                + day + ", " + weekDef);

        day = day.plusDays(1);
    }
}
 
Example 7
Source File: VariableInjector.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Object eval() {
    LocalDate date = LocalDate.now();
    date = date.minusWeeks(-1);
    LocalDate minDate = date.with(WeekFields.of(SiteConfiguration.getDefaultLocale()).dayOfWeek(), 1);
    LocalDate maxDate = date.with(WeekFields.of(SiteConfiguration.getDefaultLocale()).dayOfWeek(), 7);
    return new LocalDate[]{minDate, maxDate};
}
 
Example 8
Source File: DateAPIUtilities.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	
	LocalDate today = LocalDate.now();
	
	//Get the Year, check if it's leap year
	System.out.println("Year "+today.getYear()+" is Leap Year? "+today.isLeapYear());
	
	//Compare two LocalDate for before and after
	System.out.println("Today is before 01/01/2015? "+today.isBefore(LocalDate.of(2015,1,1)));
	
	//Create LocalDateTime from LocalDate
	System.out.println("Current Time="+today.atTime(LocalTime.now()));
	
	//plus and minus operations
	System.out.println("10 days after today will be "+today.plusDays(10));
	System.out.println("3 weeks after today will be "+today.plusWeeks(3));
	System.out.println("20 months after today will be "+today.plusMonths(20));

	System.out.println("10 days before today will be "+today.minusDays(10));
	System.out.println("3 weeks before today will be "+today.minusWeeks(3));
	System.out.println("20 months before today will be "+today.minusMonths(20));
	
	//Temporal adjusters for adjusting the dates
	System.out.println("First date of this month= "+today.with(TemporalAdjusters.firstDayOfMonth()));
	LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
	System.out.println("Last date of this year= "+lastDayOfYear);
	
	Period period = today.until(lastDayOfYear);
	System.out.println("Period Format= "+period);
	System.out.println("Months remaining in the year= "+period.getMonths());
	
}
 
Example 9
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 10
Source File: TestLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 11
Source File: TCKLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private LocalDate previous(LocalDate date) {
    int newDayOfMonth = date.getDayOfMonth() - 1;
    if (newDayOfMonth > 0) {
        return date.withDayOfMonth(newDayOfMonth);
    }
    date = date.with(date.getMonth().minus(1));
    if (date.getMonth() == Month.DECEMBER) {
        date = date.withYear(date.getYear() - 1);
    }
    return date.withDayOfMonth(date.getMonth().length(isIsoLeap(date.getYear())));
}
 
Example 12
Source File: DigitalObjectManager.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Prepares the handler to produce a series of NDK issues.
 * @param from a date to start the series
 * @param to an optional date to end the series, inclusive
 * @param dayIdxs an optional list of ISO day indicies to include in the series
 * @param partNumberFrom a partNumber to start the series
 * @return the handler
 */
public CreateHandler issueSeries(LocalDate from, LocalDate to, List<Integer> dayIdxs, Integer partNumberFrom) {
    this.seriesDateFrom = Objects.requireNonNull(from, "from");
    if (to != null) {
        if (to.isBefore(from)) {
            throw new IllegalArgumentException(String.format("Invalid date interval. from > to: %s > %s", from, to));
        }
        if (from.getYear() != to.getYear()) {
            throw new IllegalArgumentException(String.format("Not the same year: %s, %s", from, to));
        }
    } else {
        to = from.with(TemporalAdjusters.lastDayOfYear());
    }
    this.seriesDateTo = to;
    this.seriesDaysIncluded = dayIdxs == null || dayIdxs.isEmpty()
            ? EnumSet.allOf(DayOfWeek.class)
            : dayIdxs.stream()
                .filter(day -> day != null && day >= 1 && day <= 7)
                .map(i -> DayOfWeek.of(i))
                .collect(Collectors.toSet());
    if (seriesDaysIncluded.contains(seriesDateFrom.getDayOfWeek())) {
        params.put(DigitalObjectHandler.PARAM_ISSUE_DATE,
                seriesDateFrom.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));
    } else {
        // move the start to the first acceptable day
        this.hasNext = nextDate();
    }
    this.seriesPartNumberFrom = partNumberFrom;
    if (partNumberFrom != null) {
        params.put(DigitalObjectHandler.PARAM_PART_NUMBER, seriesPartNumberFrom.toString());
    }
    return this;
}
 
Example 13
Source File: TestIsoChronology.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void test_adjust_toLocalDate() {
    LocalDate isoDate = IsoChronology.INSTANCE.date(1726, 1, 4);
    LocalDate test = isoDate.with(LocalDate.of(2012, 7, 6));
    assertEquals(test, IsoChronology.INSTANCE.date(2012, 7, 6));
}
 
Example 14
Source File: TestIsoChronology.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_adjust_toLocalDate() {
    LocalDate isoDate = IsoChronology.INSTANCE.date(1726, 1, 4);
    LocalDate test = isoDate.with(LocalDate.of(2012, 7, 6));
    assertEquals(test, IsoChronology.INSTANCE.date(2012, 7, 6));
}
 
Example 15
Source File: TestIsoChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_adjust1() {
    LocalDate base = IsoChronology.INSTANCE.date(1728, 10, 28);
    LocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
    assertEquals(test, IsoChronology.INSTANCE.date(1728, 10, 31));
}
 
Example 16
Source File: TestIsoChronology.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void test_adjust1() {
    LocalDate base = IsoChronology.INSTANCE.date(1728, 10, 28);
    LocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
    assertEquals(test, IsoChronology.INSTANCE.date(1728, 10, 31));
}
 
Example 17
Source File: MonthAdjusters.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void last_day_of_month() {

	LocalDate date = LocalDate.of(1955, Month.JUNE, 11);

	LocalDate firstMondayInMonth = date.with(TemporalAdjusters
			.lastDayOfMonth());

	assertEquals(LocalDate.of(1955, Month.JUNE, 30), firstMondayInMonth);
}
 
Example 18
Source File: FirstDayInYear.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void first_day_in_year_current_year_java8 () {
	
	LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 01);

	LocalDate firstDayOfYear = date.with(TemporalAdjusters.firstDayOfYear());
	
	assertEquals(1, firstDayOfYear.getDayOfMonth());
}
 
Example 19
Source File: LastDayOfQuarterAdjuster.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void third_quarter_last_day() {

	LocalDate date = LocalDate.of(2009, Month.AUGUST, 1);

	LocalDate thirdQuarter = date.with(new LastDayOfQuarterAdjuster());

	assertEquals(LocalDate.of(2009, Month.SEPTEMBER, 30), thirdQuarter);
}
 
Example 20
Source File: DateUtils.java    From Raincat with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 得到month的终止时间点..
 *
 * @param date 日期
 * @return 传入的日期当月的结束日期 month end
 */
public static LocalDate getMonthEnd(final LocalDate date) {
    return date.with(TemporalAdjusters.lastDayOfMonth());
}