Java Code Examples for java.time.ZonedDateTime#plusDays()

The following examples show how to use java.time.ZonedDateTime#plusDays() . 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: TestDateTimeFunctions.java    From tajo with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateTimeNow() throws TajoException {
  QueryContext context = new QueryContext(getConf());
  context.put(SessionVars.TIMEZONE, "America/Los_Angeles");

  ZonedDateTime zonedDateTime = ZonedDateTime.now(TimeZone.getTimeZone(context.get(SessionVars.TIMEZONE)).toZoneId());

  testSimpleEval(context, "select to_char(now(), 'yyyy-MM-dd');",
      new String[]{dateFormat(zonedDateTime, "yyyy-MM-dd")});
  testSimpleEval(context, "select cast(extract(year from now()) as INT4);",
      new String[]{dateFormat(zonedDateTime, "yyyy")});
  testSimpleEval(context, "select current_date();",
      new String[]{dateFormat(zonedDateTime, "yyyy-MM-dd")});
  testSimpleEval(context, "select cast(extract(hour from current_time()) as INT4);",
      new String[]{String.valueOf(Integer.parseInt(dateFormat(zonedDateTime, "HH")))});

  zonedDateTime = zonedDateTime.plusDays(1);
  testSimpleEval(context, "select current_date() + 1;", new String[]{dateFormat(zonedDateTime, "yyyy-MM-dd")});
}
 
Example 2
Source File: CronExpressionTest.java    From cron with Apache License 2.0 6 votes vote down vote up
@Test
public void check_hour_shall_run_25_times_in_DST_change_to_wintertime() throws Exception {
    CronExpression cron = new CronExpression("0 1 * * * *");
    ZonedDateTime start = ZonedDateTime.of(2011, 10, 30, 0, 0, 0, 0, zoneId);
    ZonedDateTime slutt = start.plusDays(1);
    ZonedDateTime tid = start;

    // throws: Unsupported unit: Seconds
    // assertEquals(25, Duration.between(start.toLocalDate(), slutt.toLocalDate()).toHours());

    int count = 0;
    ZonedDateTime lastTime = tid;
    while (tid.isBefore(slutt)) {
        ZonedDateTime nextTime = cron.nextTimeAfter(tid);
        assertTrue(nextTime.isAfter(lastTime));
        lastTime = nextTime;
        tid = tid.plusHours(1);
        count++;
    }
    assertEquals(25, count);
}
 
Example 3
Source File: ElasticSearchIntegrationTest.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Test
void searchDateRange() {
    ZonedDateTime from = ZonedDateTime.now();
    ZonedDateTime to = from.plusDays(5);
    documentType.index("1", document("1", "value1", 1, 0, from, LocalTime.of(12, 0)));
    documentType.index("2", document("2", "value2", 1, 0, from.plusDays(1), LocalTime.of(13, 0)));
    documentType.index("3", document("3", "value3", 1, 0, to, LocalTime.of(14, 0)));
    documentType.index("4", document("4", "value4", 1, 0, to.plusDays(1), LocalTime.of(15, 0)));
    elasticSearch.refreshIndex("document");

    var request = new SearchRequest();
    request.query = rangeQuery("zoned_date_time_field").from(from).to(to);
    SearchResponse<TestDocument> response = documentType.search(request);
    assertThat(response.totalHits).isEqualTo(3);
    assertThat(response.hits.stream().map(document1 -> document1.stringField).collect(Collectors.toList()))
        .containsOnly("value1", "value2", "value3");

    request.query = rangeQuery("local_time_field").gt(LocalTime.of(13, 0));
    response = documentType.search(request);
    assertThat(response.totalHits).isEqualTo(2);
    assertThat(response.hits.stream().map(document -> document.stringField).collect(Collectors.toList()))
        .containsOnly("value3", "value4");
}
 
Example 4
Source File: ExecutionTimeCron4jIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #37: nextExecution not calculating correct time.
 */
@Test
public void testEveryWeekdayAt6() {
    ZonedDateTime lastRun = ZonedDateTime.now();
    final ExecutionTime executionTime = ExecutionTime.forCron(cron4jCronParser.parse(EVERY_WEEKDAY_AT_6));

    // iterate through the next 8 days so we roll over for a week
    // and make sure the next run time is always in the future from the prior run time
    for (int i = 0; i < 8; i++) {

        final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(lastRun);
        if (nextExecution.isPresent()) {
            final ZonedDateTime nextRun = nextExecution.get();
            log.info(LOG_LAST_RUN, lastRun);
            log.info(LOG_NEXT_RUN, nextRun);

            assertNotEquals(6, nextRun.getDayOfWeek());
            assertNotEquals(7, nextRun.getDayOfWeek());
            assertTrue(lastRun.isBefore(nextRun));
            lastRun = lastRun.plusDays(1);
        } else {
            fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
        }
    }
}
 
Example 5
Source File: ExecutionTimeQuartzIntegrationTest.java    From cron-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Issue #142: https://github.com/jmrozanec/cron-utils/pull/142
 * Special Character L for day of week behaves differently in Quartz
 * @throws ParseException in case the CronExpression can not be created
 */
@Test
public void lastDayOfTheWeek() throws ParseException {
    // L (“last”) - If used in the day-of-week field by itself, it simply means “7” or “SAT”.
    final Cron cron = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ)).parse("0 0 0 ? * L *");

    final ZoneId utc = ZoneId.of("UTC");
    final ZonedDateTime date = LocalDate.parse("2016-12-22").atStartOfDay(utc);   // Thursday
    final ZonedDateTime expected = date.plusDays(2);   // Saturday

    final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(cron).nextExecution(date);
    if (nextExecution.isPresent()) {
        final ZonedDateTime cronUtilsNextTime = nextExecution.get();// 2016-12-30T00:00:00Z

        final org.quartz.CronExpression cronExpression = new org.quartz.CronExpression(cron.asString());
        cronExpression.setTimeZone(TimeZone.getTimeZone(utc));
        final Date quartzNextTime = cronExpression.getNextValidTimeAfter(Date.from(date.toInstant()));// 2016-12-24T00:00:00Z

        assertEquals(expected.toInstant(), quartzNextTime.toInstant());    // test the reference implementation
        assertEquals(expected.toInstant(), cronUtilsNextTime.toInstant()); // and compare with cronUtils
    } else {
        fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
    }
}
 
Example 6
Source File: TCKZonedDateTime.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 7
Source File: TCKZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 8
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 9
Source File: TCKZonedDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 10
Source File: TCKZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 11
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 12
Source File: TCKZonedDateTime.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void factory_ofInstant_allDaysInCycle() {
    // sanity check using different algorithm
    ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC);
    for (long i = 0; i < 146097; i++) {
        Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
        ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        assertEquals(test, expected);
        expected = expected.plusDays(1);
    }
}
 
Example 13
Source File: EntityManagerTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateZonedDateTimeField() {
  ZonedDateTimeField entity = new ZonedDateTimeField();
  ZonedDateTime now = ZonedDateTime.now();
  entity.setTimestamp(now);
  entity = em.insert(entity);
  ZonedDateTime nextDay = now.plusDays(2);
  entity.setTimestamp(nextDay);
  entity = em.update(entity);
  entity = em.load(ZonedDateTimeField.class, entity.getId());
  assertEquals(nextDay, entity.getTimestamp());
}
 
Example 14
Source File: TimeIteratorTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * A representative unit test to cover iterating. Actual computations are covered by {@link #testInc()}
 */
@Test
public void testIterator() {
  ZonedDateTime startTime = ZonedDateTime.of(2019,12,20,11,
      20,30, 0, zone);
  ZonedDateTime endTime = startTime.plusDays(12);
  TimeIterator iterator = new TimeIterator(startTime, endTime, TimeIterator.Granularity.DAY);
  int days = 0;
  while (iterator.hasNext()) {
    Assert.assertEquals(iterator.next(), startTime.plusDays(days++));
  }
  Assert.assertEquals(days, 13);
}
 
Example 15
Source File: TaskUtil.java    From GriefDefender with MIT License 5 votes vote down vote up
public static ZonedDateTime getNextTargetZoneDate(int targetHour, int targetMin, int targetSec) {
    LocalDateTime localNow = LocalDateTime.now();
    ZoneId currentZone = ZoneId.systemDefault();
    ZonedDateTime zonedNow = ZonedDateTime.of(localNow, currentZone);
    ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin).withSecond(targetSec);
    if(zonedNow.compareTo(zonedNextTarget) > 0) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
    }
    return zonedNextTarget;
}
 
Example 16
Source File: TaskUtil.java    From GriefDefender with MIT License 5 votes vote down vote up
public static long computeDelay(int targetHour, int targetMin, int targetSec) 
{
    LocalDateTime localNow = LocalDateTime.now();
    ZoneId currentZone = ZoneId.systemDefault();
    ZonedDateTime zonedNow = ZonedDateTime.of(localNow, currentZone);
    ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin).withSecond(targetSec);
    if(zonedNow.compareTo(zonedNextTarget) > 0) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
    }

    Duration duration = Duration.between(zonedNow, zonedNextTarget);
    return duration.getSeconds();
}
 
Example 17
Source File: TaskUtil.java    From GriefDefender with MIT License 5 votes vote down vote up
public static ZonedDateTime getNextTargetZoneDate(int targetHour, int targetMin, int targetSec) {
    LocalDateTime localNow = LocalDateTime.now();
    ZoneId currentZone = ZoneId.systemDefault();
    ZonedDateTime zonedNow = ZonedDateTime.of(localNow, currentZone);
    ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin).withSecond(targetSec);
    if(zonedNow.compareTo(zonedNextTarget) > 0) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
    }
    return zonedNextTarget;
}
 
Example 18
Source File: NationsPlugin.java    From Nations with MIT License 4 votes vote down vote up
@Listener
public void onStart(GameStartedServerEvent event)
{
	LanguageHandler.load();
	ConfigHandler.load();
	DataHandler.load();
	
	Sponge.getServiceManager()
			.getRegistration(EconomyService.class)
			.ifPresent(prov -> economyService = prov.getProvider());


	NationCmds.create(this);

	Sponge.getEventManager().registerListeners(this, new PlayerConnectionListener());
	Sponge.getEventManager().registerListeners(this, new PlayerMoveListener());
	Sponge.getEventManager().registerListeners(this, new GoldenAxeListener());
	Sponge.getEventManager().registerListeners(this, new PvpListener());
	Sponge.getEventManager().registerListeners(this, new FireListener());
	Sponge.getEventManager().registerListeners(this, new ExplosionListener());
	Sponge.getEventManager().registerListeners(this, new MobSpawningListener());
	Sponge.getEventManager().registerListeners(this, new BuildPermListener());
	Sponge.getEventManager().registerListeners(this, new InteractPermListener());
	Sponge.getEventManager().registerListeners(this, new ChatListener());

	LocalDateTime localNow = LocalDateTime.now();
	ZonedDateTime zonedNow = ZonedDateTime.of(localNow, ZoneId.systemDefault());
	ZonedDateTime zonedNext = zonedNow.withHour(12).withMinute(0).withSecond(0);
	if (zonedNow.compareTo(zonedNext) > 0)
		zonedNext = zonedNext.plusDays(1);
	long initalDelay = Duration.between(zonedNow, zonedNext).getSeconds();

	Sponge.getScheduler()
			.createTaskBuilder()
			.execute(new TaxesCollectRunnable())
			.delay(initalDelay, TimeUnit.SECONDS)
			.interval(1, TimeUnit.DAYS)
			.async()
			.submit(this);

	logger.info("Plugin ready");
}
 
Example 19
Source File: TopicPartitionRecordGrouperTest.java    From aiven-kafka-connect-gcs with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
void rotateKeysMonthly() {
    final Template filenameTemplate =
        Template.of(
            "{{topic}}-"
                + "{{partition}}-"
                + "{{start_offset}}-"
                + "{{timestamp:unit=YYYY}}"
                + "{{timestamp:unit=MM}}"
        );
    final TimestampSource timestampSourceMock = mock(TimestampSource.class);

    final ZonedDateTime firstMonthTime = ZonedDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
    final ZonedDateTime secondMonth = firstMonthTime.plusDays(1);
    final String firstMonthTs =
        firstMonthTime.format(DateTimeFormatter.ofPattern("YYYY"))
            + firstMonthTime.format(DateTimeFormatter.ofPattern("MM"));
    final String secondMonthTs =
        secondMonth.format(DateTimeFormatter.ofPattern("YYYY"))
            + secondMonth.format(DateTimeFormatter.ofPattern("MM"));

    when(timestampSourceMock.time()).thenReturn(firstMonthTime);
    final TopicPartitionRecordGrouper grouper =
        new TopicPartitionRecordGrouper(
            filenameTemplate,
            null,
            timestampSourceMock
        );

    grouper.put(T0P1R0);
    grouper.put(T0P1R1);
    grouper.put(T0P1R2);

    when(timestampSourceMock.time()).thenReturn(secondMonth);

    grouper.put(T0P1R3);

    final Map<String, List<SinkRecord>> records = grouper.records();

    assertEquals(2, records.size());

    assertThat(
        records.keySet(),
        containsInAnyOrder(
            "topic0-1-10-" + firstMonthTs,
            "topic0-1-10-" + secondMonthTs
        )
    );
    assertThat(
        records.get("topic0-1-10-" + firstMonthTs),
        contains(T0P1R0, T0P1R1, T0P1R2)
    );
    assertThat(
        records.get("topic0-1-10-" + secondMonthTs),
        contains(T0P1R3)
    );
}
 
Example 20
Source File: DailyReport.java    From blynk-server with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ZonedDateTime getNextTriggerTime(ZonedDateTime zonedNow, ZoneId zoneId) {
    ZonedDateTime zonedStartAt = buildZonedStartAt(zonedNow, zoneId);
    return zonedStartAt.isAfter(zonedNow) ? zonedStartAt : zonedStartAt.plusDays(1);
}