java.time.temporal.TemporalAdjusters Java Examples

The following examples show how to use java.time.temporal.TemporalAdjusters. 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: TCKTemporalAdjusters.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_next() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);

            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.next(dow).adjustInto(date);

                assertSame(test.getDayOfWeek(), dow, date + " " + test);

                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff > 0 && dayDiff < 8);
                } else {
                    assertSame(month, Month.DECEMBER);
                    assertTrue(date.getDayOfMonth() > 24);
                    assertEquals(test.getYear(), 2008);
                    assertSame(test.getMonth(), Month.JANUARY);
                    assertTrue(test.getDayOfMonth() < 8);
                }
            }
        }
    }
}
 
Example #2
Source File: TCKTemporalAdjusters.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_previous() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);

            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);

                assertSame(test.getDayOfWeek(), dow, date + " " + test);

                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
                } else {
                    assertSame(month, Month.JANUARY);
                    assertTrue(date.getDayOfMonth() < 8);
                    assertEquals(test.getYear(), 2006);
                    assertSame(test.getMonth(), Month.DECEMBER);
                    assertTrue(test.getDayOfMonth() > 24);
                }
            }
        }
    }
}
 
Example #3
Source File: AbstractChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
    int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
    if (resolverStyle == ResolverStyle.LENIENT) {
        long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
        long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
        return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
    }
    int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
    ValueRange domRange = range(DAY_OF_MONTH);
    int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
    if (resolverStyle == ResolverStyle.SMART) {  // previous valid
        try {
            return date(y, moy, dom);
        } catch (DateTimeException ex) {
            return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth());
        }
    }
    return date(y, moy, dom);
}
 
Example #4
Source File: AbstractChronology.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
    int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
    if (resolverStyle == ResolverStyle.LENIENT) {
        long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
        long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
        return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
    }
    int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
    ValueRange domRange = range(DAY_OF_MONTH);
    int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
    if (resolverStyle == ResolverStyle.SMART) {  // previous valid
        try {
            return date(y, moy, dom);
        } catch (DateTimeException ex) {
            return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth());
        }
    }
    return date(y, moy, dom);
}
 
Example #5
Source File: TCKTemporalAdjusters.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_previous() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);

            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);

                assertSame(test.getDayOfWeek(), dow, date + " " + test);

                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
                } else {
                    assertSame(month, Month.JANUARY);
                    assertTrue(date.getDayOfMonth() < 8);
                    assertEquals(test.getYear(), 2006);
                    assertSame(test.getMonth(), Month.DECEMBER);
                    assertTrue(test.getDayOfMonth() > 24);
                }
            }
        }
    }
}
 
Example #6
Source File: TCKTemporalAdjusters.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_previous() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);

            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);

                assertSame(test.getDayOfWeek(), dow, date + " " + test);

                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
                } else {
                    assertSame(month, Month.JANUARY);
                    assertTrue(date.getDayOfMonth() < 8);
                    assertEquals(test.getYear(), 2006);
                    assertSame(test.getMonth(), Month.DECEMBER);
                    assertTrue(test.getDayOfMonth() > 24);
                }
            }
        }
    }
}
 
Example #7
Source File: PostgreSQLCastTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testCastEscapeOperator() {
    doInJPA(entityManager -> {
        List<Post> posts = entityManager.createNativeQuery(
            "SELECT * " +
            "FROM post " +
            "WHERE " +
            "   date_part('dow', created_on) = " +
            "   date_part('dow', :datetime\\:\\:date)", Post.class)
        .setParameter("datetime", Timestamp.valueOf(
            LocalDateTime.now().with(
                TemporalAdjusters.next(DayOfWeek.MONDAY)))
            )
        .getResultList();

        assertEquals(1, posts.size());
        assertEquals("High-Performance Java Persistence, Part 1", posts.get(0).getTitle());
    });
}
 
Example #8
Source File: TCKTemporalAdjusters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfMonth_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), month.length(false));
        }
    }
}
 
Example #9
Source File: TCKTemporalAdjusters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfMonth_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), month.length(false));
        }
    }
}
 
Example #10
Source File: TCKTemporalAdjusters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_firstDayOfNextYear_leap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(true); i++) {
            LocalDate date = date(2008, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date);
            assertEquals(test.getYear(), 2009);
            assertEquals(test.getMonth(), JANUARY);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #11
Source File: TCKTemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_firstDayOfMonth_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #12
Source File: TCKTemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_firstDayOfNextYear_leap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(true); i++) {
            LocalDate date = date(2008, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date);
            assertEquals(test.getYear(), 2009);
            assertEquals(test.getMonth(), JANUARY);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #13
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 #14
Source File: TCKTemporalAdjusters.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfYear_leap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(true); i++) {
            LocalDate date = date(2008, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date);
            assertEquals(test.getYear(), 2008);
            assertEquals(test.getMonth(), Month.DECEMBER);
            assertEquals(test.getDayOfMonth(), 31);
        }
    }
}
 
Example #15
Source File: TCKTemporalAdjusters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_positive")
public void test_dayOfWeekInMonth_positive(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int ordinal = 1; ordinal <= 5; ordinal++) {
        for (int day = 1; day <= Month.of(month).length(false); day++) {
            LocalDate date = date(year, month, day);
            LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(ordinal, dow).adjustInto(date);
            assertEquals(test, expected.plusWeeks(ordinal - 1));
        }
    }
}
 
Example #16
Source File: TCKTemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_positive")
public void test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.firstInMonth(dow).adjustInto(date);
        assertEquals(test, expected, "day-of-month=" + day);
    }
}
 
Example #17
Source File: TCKTemporalAdjusters.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_firstDayOfMonth_leap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(true); i++) {
            LocalDate date = date(2008, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2008);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #18
Source File: TCKTemporalAdjusters.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfYear_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), Month.DECEMBER);
            assertEquals(test.getDayOfMonth(), 31);
        }
    }
}
 
Example #19
Source File: TCKTemporalAdjusters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfYear_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), Month.DECEMBER);
            assertEquals(test.getDayOfMonth(), 31);
        }
    }
}
 
Example #20
Source File: InsightsUtils.java    From Insights with Apache License 2.0 5 votes vote down vote up
public static long getDataToTime(String schedule) {

		Long time = null;
		ZonedDateTime now = ZonedDateTime.now(zoneId);

		if (JobSchedule.DAILY.name().equalsIgnoreCase(schedule)) {
			ZonedDateTime today = now.toLocalDate().atStartOfDay(zoneId);
			ZonedDateTime yesterdayEnd = today.minusSeconds(1);
			time = yesterdayEnd.toInstant().toEpochMilli();
		} else if (JobSchedule.WEEKLY.name().equalsIgnoreCase(schedule)) {
			ZonedDateTime todayStart = now.toLocalDate().atStartOfDay(zoneId)
					.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
			ZonedDateTime lastWeek = todayStart.minusSeconds(1);
			time = lastWeek.toInstant().toEpochMilli();
		} else if (JobSchedule.MONTHLY.name().equalsIgnoreCase(schedule)) {
			ZonedDateTime firstDayofMonth = now.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate()
					.atStartOfDay(zoneId);
			ZonedDateTime firstDayofLastMonth = firstDayofMonth.minusMinutes(1)
					.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate().atStartOfDay(zoneId);
			ZonedDateTime lastDayOftheMonth = firstDayofLastMonth.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate()
					.atStartOfDay(zoneId);
			lastDayOftheMonth = lastDayOftheMonth.minusSeconds(1);
			time = lastDayOftheMonth.toInstant().toEpochMilli();
		} else if (JobSchedule.QUARTERLY.name().equalsIgnoreCase(schedule)) {
			// Will add code later for this usecase
			// zdt = zdt.plusMonths(3);
		} else if (JobSchedule.YEARLY.name().equalsIgnoreCase(schedule)) {
			ZonedDateTime firstDayofYear = now.with(TemporalAdjusters.firstDayOfYear()).toLocalDate()
					.atStartOfDay(zoneId);
			ZonedDateTime firstDayofLastYear = firstDayofYear.minusMinutes(1).with(TemporalAdjusters.firstDayOfYear())
					.toLocalDate().atStartOfDay(zoneId);
			ZonedDateTime lastDayofYear = firstDayofLastYear.with(TemporalAdjusters.lastDayOfYear()).toLocalDate()
					.atStartOfDay(zoneId);
			lastDayofYear = lastDayofYear.minusSeconds(1);
			time = lastDayofYear.toInstant().toEpochMilli();
		}
		return time;

	}
 
Example #21
Source File: TCKTemporalAdjusters.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_firstDayOfMonth_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #22
Source File: TCKTemporalAdjusters.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_lastDayOfMonth_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date);
            assertEquals(test.getYear(), 2007);
            assertEquals(test.getMonth(), month);
            assertEquals(test.getDayOfMonth(), month.length(false));
        }
    }
}
 
Example #23
Source File: TCKTemporalAdjusters.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_firstDayOfNextYear_nonLeap() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date);
            assertEquals(test.getYear(), 2008);
            assertEquals(test.getMonth(), JANUARY);
            assertEquals(test.getDayOfMonth(), 1);
        }
    }
}
 
Example #24
Source File: TCKTemporalAdjusters.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_negative")
public void test_dayOfWeekInMonth_negative(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int ordinal = 0; ordinal < 5; ordinal++) {
        for (int day = 1; day <= Month.of(month).length(false); day++) {
            LocalDate date = date(year, month, day);
            LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(-1 - ordinal, dow).adjustInto(date);
            assertEquals(test, expected.minusWeeks(ordinal));
        }
    }
}
 
Example #25
Source File: TCKTemporalAdjusters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_zero")
public void test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(0, dow).adjustInto(date);
        assertEquals(test, expected);
    }
}
 
Example #26
Source File: BsqDashboardView.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void initialize() {

    ADJUSTERS.put(DAY, TemporalAdjusters.ofDateAdjuster(d -> d));

    createKPIs();
    createChart();

    priceChangeListener = (observable, oldValue, newValue) -> {
        updatePrice();
        updateAveragePriceFields(avgPrice90TextField, avgPrice30TextField, false);
        updateAveragePriceFields(avgUSDPrice90TextField, avgUSDPrice30TextField, true);
    };
}
 
Example #27
Source File: TCKTemporalAdjusters.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_zero")
public void test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(0, dow).adjustInto(date);
        assertEquals(test, expected);
    }
}
 
Example #28
Source File: TCKTemporalAdjusters.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_positive")
public void test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.firstInMonth(dow).adjustInto(date);
        assertEquals(test, expected, "day-of-month=" + day);
    }
}
 
Example #29
Source File: TCKTemporalAdjusters.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_firstDayOfYear() {
    assertNotNull(TemporalAdjusters.firstDayOfYear());
}
 
Example #30
Source File: TCKTemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_lastDayOfMonth() {
    assertNotNull(TemporalAdjusters.lastDayOfMonth());
}