Java Code Examples for org.joda.time.DateTime#plusMillis()

The following examples show how to use org.joda.time.DateTime#plusMillis() . 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: TimeRangeUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public static DateTime increment(DateTime input, TimeGranularity granularity) {
  DateTime output;
  switch (granularity.getUnit()) {
  case DAYS:
    output = input.plusDays(granularity.getSize());
    break;
  case HOURS:
    output = input.plusHours(granularity.getSize());
    break;
  case MILLISECONDS:
    output = input.plusMillis(granularity.getSize());
    break;
  case MINUTES:
    output = input.plusMinutes(granularity.getSize());
    break;
  case SECONDS:
    output = input.plusSeconds(granularity.getSize());
    break;
  default:
    throw new IllegalArgumentException("Timegranularity:" + granularity + " not supported");
  }
  return output;
}
 
Example 2
Source File: DateConvertor.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@SafeVarargs
private static DateTime parserMills(DateTime dateTime, String timeStr, int mapIdx, Map<Pattern, SimpleDateFormat>... maps) throws ParseException {
    DateTime result = dateTime;
    if (timeStr != null) {
        timeStr = timeStr.trim();
        if (StringUtil.isNotEmpty(timeStr)) {
            for (Entry<Pattern, SimpleDateFormat> entry : maps[mapIdx].entrySet()) {
                Pattern pattern = entry.getKey();
                Matcher matcher = pattern.matcher(timeStr);
                if (matcher.find()) {
                    String matcherGroup = matcher.group();
                    SimpleDateFormat format = entry.getValue();
                    Date date = format.parse(matcherGroup);
                    DateTime timeDateTime = new DateTime(date);
                    result = result.plusMillis(timeDateTime.getMillisOfDay());
                    timeStr = timeStr.substring(matcher.end());
                    mapIdx++;
                    if (mapIdx < maps.length) {
                        result = parserMills(result, timeStr, mapIdx, maps);
                    }
                    break;
                }
            }
        }
    }
    return result;
}
 
Example 3
Source File: LRUCacheTest.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void testLruCacheWithDates() {
  // GIVEN
  DateTime firstDate = DateTime.now();
  DateTime secondDate = firstDate.plusMillis(500);
  // WHEN
  underTest.put("mymessage1", firstDate.toDate().getTime());
  underTest.put("mymessage2", firstDate.toDate().getTime());
  underTest.put("mymessage1", secondDate.toDate().getTime());
  // THEN
  assertEquals((Long) firstDate.toDate().getTime(), underTest.get("mymessage1"));
  assertEquals((Long) firstDate.toDate().getTime(), underTest.get("mymessage2"));
}
 
Example 4
Source File: LRUCacheTest.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void testLruCacheWithDatesReachDedupInterval() {
  // GIVEN
  DateTime firstDate = DateTime.now();
  DateTime secondDate = firstDate.plusMillis(1500);
  // WHEN
  underTest.put("mymessage1", firstDate.toDate().getTime());
  underTest.put("mymessage2", firstDate.toDate().getTime());
  underTest.put("mymessage1", secondDate.toDate().getTime());
  // THEN
  assertEquals((Long) secondDate.toDate().getTime(), underTest.get("mymessage1"));
  assertEquals((Long) firstDate.toDate().getTime(), underTest.get("mymessage2"));
}
 
Example 5
Source File: ExportCommitLogDiffAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the diff keys for one bucket.
 *
 * @param lowerCheckpoint exclusive lower bound on keys in this diff, or null if no lower bound
 * @param upperCheckpoint inclusive upper bound on keys in this diff
 * @param bucketNum the bucket to load diff keys from
 */
private Iterable<Key<CommitLogManifest>> loadDiffKeysFromBucket(
    @Nullable CommitLogCheckpoint lowerCheckpoint,
    CommitLogCheckpoint upperCheckpoint,
    int bucketNum) {
  // If no lower checkpoint exists, or if it exists but had no timestamp for this bucket number
  // (because the bucket count was increased between these checkpoints), then use START_OF_TIME
  // as the effective exclusive lower bound.
  DateTime lowerCheckpointBucketTime =
      firstNonNull(
          (lowerCheckpoint == null) ? null : lowerCheckpoint.getBucketTimestamps().get(bucketNum),
          START_OF_TIME);
  // Since START_OF_TIME=0 is not a valid id in a key, add 1 to both bounds. Then instead of
  // loading lowerBound < x <= upperBound, we can load lowerBound <= x < upperBound.
  DateTime lowerBound = lowerCheckpointBucketTime.plusMillis(1);
  DateTime upperBound = upperCheckpoint.getBucketTimestamps().get(bucketNum).plusMillis(1);
  // If the lower and upper bounds are equal, there can't be any results, so skip the query.
  if (lowerBound.equals(upperBound)) {
    return ImmutableSet.of();
  }
  Key<CommitLogBucket> bucketKey = getBucketKey(bucketNum);
  return ofy().load()
      .type(CommitLogManifest.class)
      .ancestor(bucketKey)
      .filterKey(">=", CommitLogManifest.createKey(bucketKey, lowerBound))
      .filterKey("<", CommitLogManifest.createKey(bucketKey, upperBound))
      .keys();
}
 
Example 6
Source File: CommitLogCheckpointStrategyTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void test_computeCheckpoint_someNewCommits_bucketCheckpointTimesAreClampedToThreshold() {
  DateTime now = clock.nowUtc();
  writeCommitLogToBucket(1);  // 1A
  writeCommitLogToBucket(2);  // 2A
  writeCommitLogToBucket(3);  // 3A
  clock.advanceBy(Duration.millis(5));
  writeCommitLogToBucket(1);  // 1B
  writeCommitLogToBucket(2);  // 2B
  writeCommitLogToBucket(3);  // 3B
  clock.advanceBy(Duration.millis(5));
  writeCommitLogToBucket(1);  // 1C
  writeCommitLogToBucket(2);  // 2C
  writeCommitLogToBucket(3);  // 3C

  // Set first pass times: 1 at T0, 2 at T+5, 3 at T+10.
  saveBucketWithLastWrittenTime(1, now);
  saveBucketWithLastWrittenTime(2, now.plusMillis(5));
  saveBucketWithLastWrittenTime(3, now.plusMillis(10));

  // Commits 1A, 2B, 3C are the commits seen in the first pass.
  // Commits 2A, 3A, 3B are all old prior commits that should be ignored.
  // Commit 1B is the first new commit for bucket 1, at T+5.
  // Commit 1C is the second new commit for bucket 1, at T+10, and should be ignored.
  // Commit 2C is the first new commit for bucket 2, at T+10.
  // Since 1B as a new commit is older than 1C, T+5 is the oldest new commit time.
  // Therefore, expect T+4 as the threshold time.
  DateTime threshold = now.plusMillis(4);

  // Advance clock before taking checkpoint.
  clock.advanceBy(Duration.millis(10));
  DateTime checkpointTime = clock.nowUtc();

  // Bucket checkpoint times should be clamped as expected.
  assertThat(strategy.computeCheckpoint())
      .isEqualTo(CommitLogCheckpoint.create(
          checkpointTime,
          ImmutableMap.of(1, now, 2, threshold, 3, threshold)));
}
 
Example 7
Source File: CommitLogExports.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the diff keys for one bucket.
 *
 * @param lowerCheckpoint exclusive lower bound on keys in this diff, or null if no lower bound
 * @param upperCheckpoint inclusive upper bound on keys in this diff
 * @param bucketNum the bucket to load diff keys from
 */
private static Iterable<Key<CommitLogManifest>> loadDiffKeysFromBucket(
    @Nullable CommitLogCheckpoint lowerCheckpoint,
    CommitLogCheckpoint upperCheckpoint,
    int bucketNum) {
  // If no lower checkpoint exists, or if it exists but had no timestamp for this bucket number
  // (because the bucket count was increased between these checkpoints), then use START_OF_TIME
  // as the effective exclusive lower bound.
  DateTime lowerCheckpointBucketTime =
      firstNonNull(
          (lowerCheckpoint == null) ? null : lowerCheckpoint.getBucketTimestamps().get(bucketNum),
          START_OF_TIME);
  // Since START_OF_TIME=0 is not a valid id in a key, add 1 to both bounds. Then instead of
  // loading lowerBound < x <= upperBound, we can load lowerBound <= x < upperBound.
  DateTime lowerBound = lowerCheckpointBucketTime.plusMillis(1);
  DateTime upperBound = upperCheckpoint.getBucketTimestamps().get(bucketNum).plusMillis(1);
  // If the lower and upper bounds are equal, there can't be any results, so skip the query.
  if (lowerBound.equals(upperBound)) {
    return ImmutableSet.of();
  }
  Key<CommitLogBucket> bucketKey = getBucketKey(bucketNum);
  return ofy()
      .load()
      .type(CommitLogManifest.class)
      .ancestor(bucketKey)
      .filterKey(">=", CommitLogManifest.createKey(bucketKey, lowerBound))
      .filterKey("<", CommitLogManifest.createKey(bucketKey, upperBound))
      .keys();
}
 
Example 8
Source File: AugmentBaseDataVisitor.java    From spork with Apache License 2.0 4 votes vote down vote up
Object GetLargerValue(Object v) {
    byte type = DataType.findType(v);

    if (type == DataType.BAG || type == DataType.TUPLE
            || type == DataType.MAP)
        return null;

    switch (type) {
    case DataType.CHARARRAY:
        return (String) v + "0";
    case DataType.BYTEARRAY:
        String str = ((DataByteArray) v).toString();
        str = str + "0";
        return new DataByteArray(str);
    case DataType.INTEGER:
        return Integer.valueOf((Integer) v + 1);
    case DataType.LONG:
        return Long.valueOf((Long) v + 1);
    case DataType.FLOAT:
        return Float.valueOf((Float) v + 1);
    case DataType.DOUBLE:
        return Double.valueOf((Double) v + 1);
    case DataType.BIGINTEGER:
        return ((BigInteger)v).add(BigInteger.ONE);
    case DataType.BIGDECIMAL:
        return ((BigDecimal)v).add(BigDecimal.ONE);
    case DataType.DATETIME:
        DateTime dt = (DateTime) v;
        if (dt.getMillisOfSecond() != 0) {
            return dt.plusMillis(1);
        } else if (dt.getSecondOfMinute() != 0) {
            return dt.plusSeconds(1);
        } else if (dt.getMinuteOfHour() != 0) {
            return dt.plusMinutes(1);
        } else if (dt.getHourOfDay() != 0) {
            return dt.plusHours(1);
        } else {
            return dt.plusDays(1);
        }
    default:
        return null;
    }
}
 
Example 9
Source File: DatePlusMilliseconds.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void add_milliseconds_to_date_in_java_with_joda () {

	DateTime newYearsEve = new DateTime(2012, 12, 31, 23, 59, 59, 0);
	DateTime newYearsDay = newYearsEve.plusMillis(1000);

	DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss M z");
	
	logger.info(newYearsEve.toString(fmt));
	logger.info(newYearsDay.toString(fmt));

	assertTrue(newYearsDay.isAfter(newYearsEve));
}