Java Code Examples for java.time.LocalDateTime#minusDays()

The following examples show how to use java.time.LocalDateTime#minusDays() . 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: TestExportProfiles.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test export-profiles display message when no profiles were found.
 */
@Test
public void testRetrieveExportProfileStats() throws Exception {
  final String tmpPath = folder0.newFolder("testStats").getAbsolutePath();
  final LocalDateTime toDate = LocalDateTime.now();
  final LocalDateTime fromDate = toDate.minusDays(30);
  ExportProfilesStats stats = new ExportProfilesStats(0, 0, 0, tmpPath);

  String expect = String.format("Defaulting to %s to %s. No profiles were found for the duration.", fromDate, toDate);
  assertEquals(expect, stats.retrieveStats(fromDate, toDate, false));

  expect = String.format("No profiles were found from %s to %s.", fromDate, toDate);
  assertEquals(expect, stats.retrieveStats(fromDate, toDate, true));

  expect = String.format("No profiles were found.");
  assertEquals(expect, stats.retrieveStats(null, null, false));
}
 
Example 2
Source File: ImportDateTime.java    From axelor-open-suite with GNU Affero General Public License v3.0 6 votes vote down vote up
public LocalDateTime updateDay(LocalDateTime datetime, String day) {
  if (!Strings.isNullOrEmpty(day)) {
    Matcher matcher = patternMonth.matcher(day);
    if (matcher.find()) {
      Long days = Long.parseLong(matcher.group());
      if (day.startsWith("+")) datetime = datetime.plusDays(days);
      else if (day.startsWith("-")) datetime = datetime.minusDays(days);
      else {
        if (days > datetime.toLocalDate().lengthOfMonth()) {
          days = Long.valueOf(datetime.toLocalDate().lengthOfMonth());
        }
        datetime = datetime.withDayOfMonth(days.intValue());
      }
    }
  }
  return datetime;
}
 
Example 3
Source File: FuzzyValues.java    From icure-backend with GNU General Public License v2.0 6 votes vote down vote up
public static long getFuzzyDateTime(LocalDateTime dateTime, TemporalUnit precision) {
	int seconds = dateTime.getSecond();
	/*if (seconds == 0 && precision==ChronoUnit.SECONDS) {
		seconds = 60;
		dateTime = dateTime.minusMinutes(1);
	}*/

	int minutes = dateTime.getMinute();
	if (minutes == 0 && precision == ChronoUnit.MINUTES) {
		minutes = 60;
		dateTime = dateTime.minusHours(1);
	}

	int hours = dateTime.getHour();
	if (hours == 0 && precision == ChronoUnit.HOURS) {
		hours = 24;
		dateTime = dateTime.minusDays(1);
	}


	return getFuzzyDate(dateTime, precision) * 1000000l + (precision == ChronoUnit.DAYS ? 0 : (
			hours * 10000l + (precision == ChronoUnit.HOURS ? 0 : (
					minutes * 100l + (precision == ChronoUnit.MINUTES ? 0 : seconds)))));
}
 
Example 4
Source File: StringCode.java    From java-tutorial with MIT License 6 votes vote down vote up
private static void stringTest() {
    String str0 = "123";
    String str1 = "123";
    //"=="在Java比较的不是两个对象的值,而是比较两个对象的引用是否相等
    System.out.println(str0 == str1);

    String str2 = "234";
    String str3 = "234";
    //Java虚拟机的解释器每遇到一个new关键字,都会在堆内存中开辟一块内存来存放一个String对象,不同的引用 false
    System.out.println(str2 == str3);
    //对比值相等
    System.out.println(str2.equals(str3));

    //昨天的当前时刻
    LocalDateTime today = LocalDateTime.now();
    var yesterday = today.minusDays(1);
    System.out.println(yesterday);


}
 
Example 5
Source File: TimeUtil.java    From game-server with MIT License 5 votes vote down vote up
/**@
 * 获取与今日相差天数的日期格式,负为日期前,正为日期后。如yyyy-MM-dd HH
 *
 * @param days
 * @param formatter
 * @return
 */
public static String getOffToDay(int days, DateTimeFormatter formatter) {
    LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault());
    if (days < 0) {
        ldt = ldt.minusDays(-days);
    } else {
        ldt = ldt.plusDays(days);
    }
    return ldt.format(formatter);
}
 
Example 6
Source File: StorageTTLE2E.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@RetryableTest
public void dayMetricsDataShouldBeRemovedAfterTTL() throws Exception {
    final ServiceMeshMetric.Builder builder =
        ServiceMeshMetric.newBuilder()
                         .setSourceServiceName("e2e-test-source-service")
                         .setSourceServiceInstance("e2e-test-source-service-instance")
                         .setDestServiceName("e2e-test-dest-service")
                         .setDestServiceInstance("e2e-test-dest-service-instance")
                         .setEndpoint("e2e/test")
                         .setLatency(2000)
                         .setResponseCode(200)
                         .setStatus(SUCCESS)
                         .setProtocol(Protocol.HTTP)
                         .setDetectPoint(DetectPoint.server);

    final LocalDateTime now = now();
    final LocalDateTime startTime = now.minusDays(SW_CORE_RECORD_DATA_TTL + 1);
    final LocalDateTime endTime = startTime.plusMinutes(1);

    final LocalDateTime queryEnd = now.minusDays(SW_CORE_RECORD_DATA_TTL);

    ensureSendingMetricsWorks(
        builder, startTime.toEpochSecond(ZoneOffset.UTC) * 1000, endTime.toEpochSecond(ZoneOffset.UTC) * 1000,
        startTime, queryEnd, "DAY"
    );

    shouldBeEmptyBetweenTimeRange(startTime, queryEnd, "DAY");
}
 
Example 7
Source File: TimeOfDayOfWeek.java    From SmartApplianceEnabler with GNU General Public License v2.0 5 votes vote down vote up
public LocalDateTime toLastOccurrence(LocalDateTime now) {
    LocalDateTime dateTime = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(),
            getHour(), getMinute(), getSecond());
    while(dateTime.getDayOfWeek().getValue() != dayOfWeek) {
        dateTime = dateTime.minusDays(1);
    }
    return dateTime;
}
 
Example 8
Source File: LitemallOrderServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
public List<LitemallOrder> queryUnconfirm(int days) {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime expired = now.minusDays(days);
    LitemallOrderExample example = new LitemallOrderExample();
    example.or().andOrderStatusEqualTo(OrderUtil.STATUS_SHIP).andShipTimeLessThan(expired).andDeletedEqualTo(false);
    return litemallOrderMapper.selectByExample(example);
}
 
Example 9
Source File: LitemallOrderService.java    From litemall with MIT License 5 votes vote down vote up
public List<LitemallOrder> queryComment(int days) {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime expired = now.minusDays(days);
    LitemallOrderExample example = new LitemallOrderExample();
    example.or().andCommentsGreaterThan((short) 0).andConfirmTimeLessThan(expired).andDeletedEqualTo(false);
    return litemallOrderMapper.selectByExample(example);
}
 
Example 10
Source File: LitemallOrderService.java    From litemall with MIT License 5 votes vote down vote up
public List<LitemallOrder> queryUnconfirm(int days) {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime expired = now.minusDays(days);
    LitemallOrderExample example = new LitemallOrderExample();
    example.or().andOrderStatusEqualTo(OrderUtil.STATUS_SHIP).andShipTimeLessThan(expired).andDeletedEqualTo(false);
    return litemallOrderMapper.selectByExample(example);
}
 
Example 11
Source File: Lesson1_1.java    From java-interview with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // 获取明天此刻时间
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    System.out.println(calendar.getTime());
    LocalDateTime today = LocalDateTime.now();
    LocalDateTime tomorrow = today.minusDays(-1);
    //LocalDateTime tomorrow = today.plusDays(1);
    System.out.println(tomorrow);
}
 
Example 12
Source File: ColorDisplayService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the first date time corresponding to the local time that occurs before the
 * provided date time.
 *
 * @param compareTime the LocalDateTime to compare against
 * @return the prior LocalDateTime corresponding to this local time
 */
public static LocalDateTime getDateTimeBefore(LocalTime localTime, LocalDateTime compareTime) {
    final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(),
            compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute());

    // Check if the local time has passed, if so return the same time yesterday.
    return ldt.isAfter(compareTime) ? ldt.minusDays(1) : ldt;
}
 
Example 13
Source File: HdfsOffsetComputerTest.java    From garmadon with Apache License 2.0 4 votes vote down vote up
@Test
public void actualFileSystem() throws IOException {
    final java.nio.file.Path tmpDir = Files.createTempDirectory("hdfs-reader-test-");

    try {
        final Path rootPath = new Path(tmpDir.toString());
        // Make sure we can read from subdirectories
        final Path basePath = new Path(rootPath, "embedded");
        final FileSystem localFs = FileSystem.getLocal(new Configuration());

        final HdfsOffsetComputer hdfsOffsetComputer = new HdfsOffsetComputer(localFs, basePath, 2);

        final LocalDateTime today = LocalDateTime.now();
        final LocalDateTime yesterday = today.minusDays(1);
        final LocalDateTime twoDaysAgo = today.minusDays(2);

        /*
            /tmp/hdfs-reader-test-1234
            └── embedded
                ├── <today>
                │   ├── 1.index=0
                │   └── 2.index=0
                └── <yesterday>
                │   ├── 1.index=0
                │   └── 1.index=1
                └── <2 days ago> # Should be ignored
                    └── 1.index=0
         */
        localFs.mkdirs(rootPath);
        localFs.mkdirs(basePath);

        writeParquetFile(new Path(basePath, hdfsOffsetComputer.computePath(today, 0L, 1)), 1);
        writeParquetFile(new Path(basePath, hdfsOffsetComputer.computePath(today, 0L, 2)), 12);
        writeParquetFile(new Path(basePath, hdfsOffsetComputer.computePath(yesterday, 0L, 1)), 2);
        writeParquetFile(new Path(basePath, hdfsOffsetComputer.computePath(yesterday, 1L, 1)), 3);
        writeParquetFile(new Path(basePath, hdfsOffsetComputer.computePath(twoDaysAgo, 0L, 1)), 42);

        Map<Integer, Long> offsets = hdfsOffsetComputer.computeOffsets(Arrays.asList(1, 2, 3));

        Assert.assertEquals(3, offsets.get(1).longValue());
        Assert.assertEquals(12, offsets.get(2).longValue());
        Assert.assertEquals(-1, offsets.get(3).longValue());
    } finally {
        FileUtils.deleteDirectory(tmpDir.toFile());
    }
}
 
Example 14
Source File: GraphDataProvider.java    From testgrid with Apache License 2.0 4 votes vote down vote up
/**
 * Provide history of the test execution summary for a given build job.
 *
 * @param productId product id.
 */
public Map<String, BuildExecutionSummary> getTestExecutionHistory(String productId) {

    Map<String, BuildExecutionSummary> buildExecutionSummariesHistory = new TreeMap<>();

    LocalTime midnight = LocalTime.MIDNIGHT;
    LocalDate timeZone = LocalDate.now(ZoneId.of("UTC"));
    LocalDateTime todayMidnight = LocalDateTime.of(timeZone, midnight);

    for (int i = 0; i < MAXIMUM_TIME_RANGE; i++) {
        if (TEST_EXECUTION_HISTORY_RANGE == buildExecutionSummariesHistory.size()) {
            break;
        }
        String from = todayMidnight.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String to = todayMidnight.plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        BuildExecutionSummary buildExecutionSummary = new BuildExecutionSummary();
        List<TestPlan> filteredTestPlanHistory;

        int passedTestPlans = 0;
        int failedTestPlans = 0;
        int skippedTestPlans = 0;

        List<TestPlan> testPlanHistory = testPlanUOW.getTestExecutionHistory(productId, from, to);
        if (testPlanHistory.isEmpty()) {
            todayMidnight = todayMidnight.minusDays(1);
            continue;
        }
        filteredTestPlanHistory = filterTestPlanHistory(testPlanHistory);

        for (TestPlan testplan : filteredTestPlanHistory) {
            if (TestPlanStatus.SUCCESS.equals(testplan.getStatus())) {
                passedTestPlans++;
            } else if (TestPlanStatus.FAIL.equals(testplan.getStatus())) {
                failedTestPlans++;
            } else {
                skippedTestPlans++;
            }
        }
        buildExecutionSummary.setPassedTestPlans(passedTestPlans);
        buildExecutionSummary.setFailedTestPlans(failedTestPlans);
        buildExecutionSummary.setSkippedTestPlans(skippedTestPlans);
        buildExecutionSummariesHistory.put(from.replace(" 00:00:00", ""), buildExecutionSummary);
        todayMidnight = todayMidnight.minusDays(1);
    }
    return buildExecutionSummariesHistory;
}
 
Example 15
Source File: HdfsOffsetComputer.java    From garmadon with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Integer, Long> computeOffsets(Collection<Integer> partitionIds) throws IOException {
    Set<Integer> partitionIdsSet = new HashSet<>(partitionIds);

    final Map<Integer, Long> result = partitionIds.stream().collect(Collectors.toMap(Function.identity(), i -> NO_OFFSET));
    final Map<Integer, Map<String, FinalEventPartitionFile>> resultFile = new HashMap<>();

    LocalDateTime today = LocalDateTime.now();
    for (int i = 0; i < backlogDays; ++i) {
        LocalDateTime day = today.minusDays(i);
        String listedDay = day.format(DateTimeFormatter.ISO_DATE);

        String dirsPattern = String.format("%s/*", computeDirName(day));
        FileStatus[] fileStatuses = fs.globStatus(new Path(basePath, dirsPattern));

        for (FileStatus status : fileStatuses) {
            String fileName = status.getPath().getName();
            Matcher matcher = offsetFilePatternGenerator.matcher(fileName);
            if (matcher.matches()) {
                try {
                    int partitionId = Integer.parseInt(matcher.group("partitionId"));
                    long index = Long.parseLong(matcher.group("index"));
                    if (isPartitionComputedByThisReader(partitionId, partitionIdsSet) &&
                        checkCurrentFileIndexBiggerTheOneInHashPartitionDay(partitionId, listedDay, index, resultFile)) {
                        HashMap<String, FinalEventPartitionFile> dateFinalEventPartitionFile =
                            (HashMap<String, FinalEventPartitionFile>) resultFile.computeIfAbsent(partitionId, HashMap::new);
                        dateFinalEventPartitionFile.put(listedDay,
                            new FinalEventPartitionFile(index, status.getPath()));
                        resultFile.put(partitionId, dateFinalEventPartitionFile);
                    }
                } catch (NumberFormatException e) {
                    LOGGER.info("Couldn't deviate a valid offset from '{}'", fileName);
                }
            }
        }
    }

    // Get last offset
    for (Map.Entry<Integer, Map<String, FinalEventPartitionFile>> partitionIdDateFile : resultFile.entrySet()) {
        result.put(partitionIdDateFile.getKey(), getMaxOffset(partitionIdDateFile.getValue()));
    }

    return result;
}
 
Example 16
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void testComparison() {
  LocalDate date = LocalDate.of(2015, 1, 25);
  LocalDateTime dateTime = LocalDate.of(2015, 1, 25).atStartOfDay();
  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");

  LocalDateTime beforeDate = dateTime.minusDays(1);
  LocalDateTime afterDate = dateTime.plusDays(1);

  dateTimeColumn.append(beforeDate);
  dateTimeColumn.append(dateTime);
  dateTimeColumn.append(afterDate);

  IntColumn index = IntColumn.indexColumn("index", dateTimeColumn.size(), 0);
  Table t = Table.create("test", dateTimeColumn, index);

  assertTrue(dateTimeColumn.isOnOrBefore(date).contains(0));
  assertTrue(dateTimeColumn.isOnOrBefore(date).contains(1));

  assertTrue(dateTimeColumn.isOnOrAfter(date).contains(1));
  assertTrue(dateTimeColumn.isOnOrAfter(date).contains(2));

  assertTrue(dateTimeColumn.isBefore(dateTime).contains(0));
  assertTrue(dateTimeColumn.isBefore(date).contains(0));
  assertTrue(t.where(t.dateTimeColumn("test").isBefore(dateTime)).intColumn("index").contains(0));
  assertTrue(t.where(t.dateTimeColumn("test").isBefore(date)).intColumn("index").contains(0));

  assertTrue(dateTimeColumn.isEqualTo(dateTime).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isEqualTo(dateTime)).intColumn("index").contains(1));

  assertTrue(dateTimeColumn.isAfter(dateTime).contains(2));
  assertTrue(dateTimeColumn.isAfter(date).contains(2));
  assertTrue(t.where(t.dateTimeColumn("test").isAfter(dateTime)).intColumn("index").contains(2));
  assertTrue(t.where(t.dateTimeColumn("test").isAfter(date)).intColumn("index").contains(2));

  assertTrue(dateTimeColumn.isNotEqualTo(dateTime).contains(2));
  assertTrue(
      t.where(t.dateTimeColumn("test").isNotEqualTo(dateTime)).intColumn("index").contains(2));

  assertTrue(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(1));
  assertTrue(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(1));

  assertTrue(dateTimeColumn.isBetweenIncluding(beforeDate, afterDate).contains(2));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenIncluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(2));

  assertTrue(dateTimeColumn.isBetweenIncluding(beforeDate, afterDate).contains(0));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenIncluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(0));

  assertFalse(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(2));
  assertFalse(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(2));

  assertFalse(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(0));
  assertFalse(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(0));
}
 
Example 17
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void testComparison2() {
  LocalDateTime dateTime = LocalDate.of(2015, 1, 25).atStartOfDay();
  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");

  LocalDateTime beforeDate = dateTime.minusDays(1);
  LocalDateTime afterDate = dateTime.plusDays(1);

  dateTimeColumn.append(beforeDate);
  dateTimeColumn.append(dateTime);
  dateTimeColumn.append(afterDate);

  DateTimeColumn same = DateTimeColumn.create("same");
  same.append(beforeDate);
  same.append(dateTime);
  same.append(afterDate);

  DateTimeColumn before = DateTimeColumn.create("before");
  before.append(beforeDate.minusDays(1));
  before.append(dateTime.minusDays(1));
  before.append(afterDate.minusDays(1));

  DateTimeColumn after = DateTimeColumn.create("after");
  after.append(beforeDate.plusDays(1));
  after.append(dateTime.plusDays(1));
  after.append(afterDate.plusDays(1));

  Table t = Table.create("test", dateTimeColumn, same, before, after);

  assertTrue(dateTimeColumn.isEqualTo(same).contains(0));
  assertTrue(t.dateTimeColumn("test").isEqualTo(same).contains(0));
  assertTrue(t.dateTimeColumn("test").isEqualTo(t.dateTimeColumn("same")).contains(0));

  assertTrue(dateTimeColumn.isBefore(after).contains(0));
  assertTrue(t.dateTimeColumn("test").isBefore(after).contains(0));
  assertTrue(t.dateTimeColumn("test").isBefore(t.dateTimeColumn("after")).contains(0));

  assertTrue(dateTimeColumn.isAfter(before).contains(0));
  assertTrue(t.dateTimeColumn("test").isAfter(before).contains(0));
  assertTrue(t.dateTimeColumn("test").isAfter(t.dateTimeColumn("before")).contains(0));

  assertFalse(dateTimeColumn.isNotEqualTo(same).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(same).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("same")).contains(0));

  assertTrue(dateTimeColumn.isOnOrBefore(same).contains(0));
  assertTrue(dateTimeColumn.isOnOrBefore(after).contains(0));
  assertFalse(dateTimeColumn.isOnOrBefore(before).contains(0));
  assertTrue(dateTimeColumn.isNotEqualTo(before).contains(0));

  assertTrue(dateTimeColumn.isOnOrAfter(same).contains(1));
  assertTrue(dateTimeColumn.isOnOrAfter(before).contains(2));
  assertFalse(dateTimeColumn.isOnOrAfter(after).contains(2));
  assertTrue(dateTimeColumn.isNotEqualTo(after).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrAfter(t.dateTimeColumn("same")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrAfter(same).contains(0));

  assertFalse(t.dateTimeColumn("test").isOnOrAfter(t.dateTimeColumn("after")).contains(0));
  assertFalse(t.dateTimeColumn("test").isOnOrAfter(after).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("same")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrBefore(same).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("after")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrBefore(after).contains(0));

  assertFalse(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("before")).contains(0));
  assertFalse(t.dateTimeColumn("test").isOnOrBefore(before).contains(0));

  assertTrue(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("before")).contains(0));
  assertTrue(t.dateTimeColumn("test").isNotEqualTo(before).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("same")).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(same).contains(0));
}
 
Example 18
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void testComparison2() {
  LocalDateTime dateTime = LocalDate.of(2015, 1, 25).atStartOfDay();
  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");

  LocalDateTime beforeDate = dateTime.minusDays(1);
  LocalDateTime afterDate = dateTime.plusDays(1);

  dateTimeColumn.append(beforeDate);
  dateTimeColumn.append(dateTime);
  dateTimeColumn.append(afterDate);

  DateTimeColumn same = DateTimeColumn.create("same");
  same.append(beforeDate);
  same.append(dateTime);
  same.append(afterDate);

  DateTimeColumn before = DateTimeColumn.create("before");
  before.append(beforeDate.minusDays(1));
  before.append(dateTime.minusDays(1));
  before.append(afterDate.minusDays(1));

  DateTimeColumn after = DateTimeColumn.create("after");
  after.append(beforeDate.plusDays(1));
  after.append(dateTime.plusDays(1));
  after.append(afterDate.plusDays(1));

  Table t = Table.create("test", dateTimeColumn, same, before, after);

  assertTrue(dateTimeColumn.isEqualTo(same).contains(0));
  assertTrue(t.dateTimeColumn("test").isEqualTo(same).contains(0));
  assertTrue(t.dateTimeColumn("test").isEqualTo(t.dateTimeColumn("same")).contains(0));

  assertTrue(dateTimeColumn.isBefore(after).contains(0));
  assertTrue(t.dateTimeColumn("test").isBefore(after).contains(0));
  assertTrue(t.dateTimeColumn("test").isBefore(t.dateTimeColumn("after")).contains(0));

  assertTrue(dateTimeColumn.isAfter(before).contains(0));
  assertTrue(t.dateTimeColumn("test").isAfter(before).contains(0));
  assertTrue(t.dateTimeColumn("test").isAfter(t.dateTimeColumn("before")).contains(0));

  assertFalse(dateTimeColumn.isNotEqualTo(same).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(same).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("same")).contains(0));

  assertTrue(dateTimeColumn.isOnOrBefore(same).contains(0));
  assertTrue(dateTimeColumn.isOnOrBefore(after).contains(0));
  assertFalse(dateTimeColumn.isOnOrBefore(before).contains(0));
  assertTrue(dateTimeColumn.isNotEqualTo(before).contains(0));

  assertTrue(dateTimeColumn.isOnOrAfter(same).contains(1));
  assertTrue(dateTimeColumn.isOnOrAfter(before).contains(2));
  assertFalse(dateTimeColumn.isOnOrAfter(after).contains(2));
  assertTrue(dateTimeColumn.isNotEqualTo(after).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrAfter(t.dateTimeColumn("same")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrAfter(same).contains(0));

  assertFalse(t.dateTimeColumn("test").isOnOrAfter(t.dateTimeColumn("after")).contains(0));
  assertFalse(t.dateTimeColumn("test").isOnOrAfter(after).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("same")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrBefore(same).contains(0));

  assertTrue(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("after")).contains(0));
  assertTrue(t.dateTimeColumn("test").isOnOrBefore(after).contains(0));

  assertFalse(t.dateTimeColumn("test").isOnOrBefore(t.dateTimeColumn("before")).contains(0));
  assertFalse(t.dateTimeColumn("test").isOnOrBefore(before).contains(0));

  assertTrue(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("before")).contains(0));
  assertTrue(t.dateTimeColumn("test").isNotEqualTo(before).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(t.dateTimeColumn("same")).contains(0));
  assertFalse(t.dateTimeColumn("test").isNotEqualTo(same).contains(0));
}
 
Example 19
Source File: DateTimeFiltersTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void testComparison() {
  LocalDate date = LocalDate.of(2015, 1, 25);
  LocalDateTime dateTime = LocalDate.of(2015, 1, 25).atStartOfDay();
  DateTimeColumn dateTimeColumn = DateTimeColumn.create("test");

  LocalDateTime beforeDate = dateTime.minusDays(1);
  LocalDateTime afterDate = dateTime.plusDays(1);

  dateTimeColumn.append(beforeDate);
  dateTimeColumn.append(dateTime);
  dateTimeColumn.append(afterDate);

  IntColumn index = IntColumn.indexColumn("index", dateTimeColumn.size(), 0);
  Table t = Table.create("test", dateTimeColumn, index);

  assertTrue(dateTimeColumn.isOnOrBefore(date).contains(0));
  assertTrue(dateTimeColumn.isOnOrBefore(date).contains(1));

  assertTrue(dateTimeColumn.isOnOrAfter(date).contains(1));
  assertTrue(dateTimeColumn.isOnOrAfter(date).contains(2));

  assertTrue(dateTimeColumn.isBefore(dateTime).contains(0));
  assertTrue(dateTimeColumn.isBefore(date).contains(0));
  assertTrue(t.where(t.dateTimeColumn("test").isBefore(dateTime)).intColumn("index").contains(0));
  assertTrue(t.where(t.dateTimeColumn("test").isBefore(date)).intColumn("index").contains(0));

  assertTrue(dateTimeColumn.isEqualTo(dateTime).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isEqualTo(dateTime)).intColumn("index").contains(1));

  assertTrue(dateTimeColumn.isAfter(dateTime).contains(2));
  assertTrue(dateTimeColumn.isAfter(date).contains(2));
  assertTrue(t.where(t.dateTimeColumn("test").isAfter(dateTime)).intColumn("index").contains(2));
  assertTrue(t.where(t.dateTimeColumn("test").isAfter(date)).intColumn("index").contains(2));

  assertTrue(dateTimeColumn.isNotEqualTo(dateTime).contains(2));
  assertTrue(
      t.where(t.dateTimeColumn("test").isNotEqualTo(dateTime)).intColumn("index").contains(2));

  assertTrue(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(1));
  assertTrue(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(1));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(1));

  assertTrue(dateTimeColumn.isBetweenIncluding(beforeDate, afterDate).contains(2));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenIncluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(2));

  assertTrue(dateTimeColumn.isBetweenIncluding(beforeDate, afterDate).contains(0));
  assertTrue(
      t.where(t.dateTimeColumn("test").isBetweenIncluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(0));

  assertFalse(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(2));
  assertFalse(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(2));

  assertFalse(dateTimeColumn.isBetweenExcluding(beforeDate, afterDate).contains(0));
  assertFalse(
      t.where(t.dateTimeColumn("test").isBetweenExcluding(beforeDate, afterDate))
          .intColumn("index")
          .contains(0));
}
 
Example 20
Source File: SearchServiceAggsIT.java    From elastic-rest-spring-wrapper with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
private Date createDate(int numDaysToSubtract) {
    LocalDateTime date = LocalDateTime.now();

    LocalDateTime newDate = date.minusDays(numDaysToSubtract);

    return Date.from(newDate.atZone(ZoneId.systemDefault()).toInstant());
}