org.joda.time.DateTime.minusHours()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(16.5k)|赞(0)|评价(0)|浏览(244)

本文整理了Java中org.joda.time.DateTime.minusHours()方法的一些代码示例,展示了DateTime.minusHours()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTime.minusHours()方法的具体详情如下:
包路径:org.joda.time.DateTime
类名称:DateTime
方法名:minusHours

DateTime.minusHours介绍

[英]Returns a copy of this datetime minus the specified number of hours.

The calculation will subtract a duration equivalent to the number of hours expressed in milliseconds.

For example, if a spring daylight savings cutover is from 01:59 to 03:00 then subtracting one hour from 03:30 will result in 01:30. This is a duration of one hour earlier, even though the hour field value changed from 3 to 1.

The following three lines are identical in effect:

DateTime subtracted = dt.minusHours(6); 
DateTime subtracted = dt.minus(Period.hours(6)); 
DateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);

This datetime instance is immutable and unaffected by this method call.
[中]返回此datetime减去指定小时数的副本。
计算将减去一个与以毫秒表示的小时数相等的持续时间。
例如,如果春季夏令时切换时间为01:59到03:00,则从03:30减去一小时将得到01:30。这是一个提前一小时的持续时间,即使小时字段值从3更改为1。
以下三行实际上是相同的:

DateTime subtracted = dt.minusHours(6); 
DateTime subtracted = dt.minus(Period.hours(6)); 
DateTime subtracted = dt.withFieldAdded(DurationFieldType.hours(), -6);

此datetime实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: apache/incubator-druid

@Setup
public void setupDummyCluster()
{
 segment = createSegment(t0);
 Random r = ThreadLocalRandom.current();
 segments = new ArrayList<>(n);
 for (int i = 0; i < n; ++i) {
  final DateTime t = t0.minusHours(r.nextInt(365 * 24) - 365 * 12);
  segments.add(createSegment(t));
 }
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Helper method for getting a value containing CURRENTDAY-1 or CURRENTHOUR-1 in the form yyyyMMddHHmmss
 * @param value
 * @param timezone
 * @return
 */
public static long getLongWithCurrentDate(String value, String timezone) {
 if (Strings.isNullOrEmpty(value)) {
  return 0;
 }
 DateTime time = getCurrentTime(timezone);
 DateTimeFormatter dtFormatter = DateTimeFormat.forPattern(CURRENT_DATE_FORMAT).withZone(time.getZone());
 if (value.toUpperCase().startsWith(CURRENT_DAY)) {
  return Long
    .parseLong(dtFormatter.print(time.minusDays(Integer.parseInt(value.substring(CURRENT_DAY.length() + 1)))));
 }
 if (value.toUpperCase().startsWith(CURRENT_HOUR)) {
  return Long
    .parseLong(dtFormatter.print(time.minusHours(Integer.parseInt(value.substring(CURRENT_HOUR.length() + 1)))));
 }
 return Long.parseLong(value);
}

代码示例来源:origin: dlew/joda-time-android

private void sampleGetRelativeTimeSpanString() {
  List<String> text = new ArrayList<String>();
  DateTime now = DateTime.now();
  text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25)));
  text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5)));
  text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3)));
  text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25)));
  text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5)));
  text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3)));
  addSample("DateUtils.getRelativeTimeSpanString()", text);
}

代码示例来源:origin: dlew/joda-time-android

private void sampleGetRelativeDateTimeString() {
  List<String> text = new ArrayList<String>();
  DateTime now = DateTime.now();
  text.add("Short future: " + DateUtils.getRelativeDateTimeString(this, now.plusMinutes(25), null, 0));
  text.add("Medium future: " + DateUtils.getRelativeDateTimeString(this, now.plusHours(5), null, 0));
  text.add("Long future: " + DateUtils.getRelativeDateTimeString(this, now.plusDays(3), null, 0));
  text.add("Short past: " + DateUtils.getRelativeDateTimeString(this, now.minusMinutes(25), null, 0));
  text.add("Medium past: " + DateUtils.getRelativeDateTimeString(this, now.minusHours(5), null, 0));
  text.add("Long past: " + DateUtils.getRelativeDateTimeString(this, now.minusDays(3), null, 0));
  addSample("DateUtils.getRelativeDateTimeString()", text);
}

代码示例来源:origin: dlew/joda-time-android

private void sampleGetRelativeTimeSpanStringWithPreposition() {
  List<String> text = new ArrayList<String>();
  DateTime now = DateTime.now();
  text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25), true));
  text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5), true));
  text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3), true));
  text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25), true));
  text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5), true));
  text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3), true));
  addSample("DateUtils.getRelativeTimeSpanString() (with preposition)", text);
}

代码示例来源:origin: Netflix/conductor

public List<String> searchRecentRunningWorkflows(int lastModifiedHoursAgoFrom, int lastModifiedHoursAgoTo) {
  DateTime dateTime = new DateTime();
  QueryBuilder q = QueryBuilders.boolQuery()
      .must(QueryBuilders.rangeQuery("updateTime")
          .gt(dateTime.minusHours(lastModifiedHoursAgoFrom)))
      .must(QueryBuilders.rangeQuery("updateTime")
          .lt(dateTime.minusHours(lastModifiedHoursAgoTo)))
      .must(QueryBuilders.termQuery("status", "RUNNING"));
  SearchResult<String> workflowIds;
  try {
    workflowIds = searchObjectIds(indexName, q, 0, 5000, Collections.singletonList("updateTime:ASC"), WORKFLOW_DOC_TYPE);
  } catch (IOException e) {
    logger.error("Unable to communicate with ES to find recent running workflows", e);
    return Collections.emptyList();
  }
  return workflowIds.getResults();
}

代码示例来源:origin: gocd/gocd

private static void completeBuildInstancesOfStage(Stage stage, JobResult result, Date completionDate) {
  for (JobInstance job : stage.getJobInstances()) {
    job.assign("uuid", new DateTime(completionDate.getTime()).minusHours(1).toDate());
    job.completing(result, completionDate);
    job.completed(completionDate);
  }
  stage.setLastTransitionedTime(new Timestamp(completionDate.getTime()));
}

代码示例来源:origin: Netflix/conductor

@Override
public List<String> searchRecentRunningWorkflows(int lastModifiedHoursAgoFrom,
  int lastModifiedHoursAgoTo) {
  DateTime dateTime = new DateTime();
  QueryBuilder q = QueryBuilders.boolQuery()
    .must(QueryBuilders.rangeQuery("updateTime")
      .gt(dateTime.minusHours(lastModifiedHoursAgoFrom)))
    .must(QueryBuilders.rangeQuery("updateTime")
      .lt(dateTime.minusHours(lastModifiedHoursAgoTo)))
    .must(QueryBuilders.termQuery("status", "RUNNING"));
  SearchRequestBuilder s = elasticSearchClient.prepareSearch(indexName)
    .setTypes("workflow")
    .setQuery(q)
    .setSize(5000)
    .addSort("updateTime", SortOrder.ASC);
  SearchResponse response = s.execute().actionGet();
  return StreamSupport.stream(response.getHits().spliterator(), false)
    .map(hit -> hit.getId())
    .collect(Collectors.toCollection(LinkedList::new));
}

代码示例来源:origin: gocd/gocd

@Before
public void setUp() {
  nowMod = new Modification("user3", "fixed the build.", null, new DateTime().toDate(), "100");
  nowMod.createModifiedFile("foo.java", ".", ModifiedAction.modified);
  oneHourAgoMod = new Modification("user2", "fixed the build.", null, new DateTime().minusHours(1).toDate(), "89");
  oneHourAgoMod.createModifiedFile("foo.java", ".", ModifiedAction.modified);
  yesterdayMod = new Modification("user1", "fixed the build.", null, new DateTime().minusDays(1).toDate(), "9");
  yesterdayMod.createModifiedFile("foo.java", ".", ModifiedAction.modified);
  material = MaterialsMother.svnMaterial("foo");
  material.setName(new CaseInsensitiveString("Foo"));
}

代码示例来源:origin: apache/incubator-druid

@Test
public void randomSegmentsCostTest()
{
 List<DataSegment> dataSegments = new ArrayList<>(1000);
 Random random = new Random(1);
 for (int i = 0; i < 1000; ++i) {
  dataSegments.add(createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, random.nextInt(20)), 100));
 }
 DataSegment referenceSegment = createSegment("ANOTHER_DATA_SOURCE", shifted1HInterval(REFERENCE_TIME, 5), 100);
 SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval(
   REFERENCE_TIME.minusHours(1),
   REFERENCE_TIME.plusHours(25)
 ));
 dataSegments.forEach(prototype::addSegment);
 SegmentsCostCache.Bucket bucket = prototype.build();
 double cost = bucket.cost(referenceSegment);
 assertEquals(0.7065117101966677, cost, EPSILON);
}

代码示例来源:origin: apache/incubator-druid

"DUMMY",
new Interval(
  referenceTime.minusHours(2),
  referenceTime.minusHours(2).plusHours(1)

代码示例来源:origin: killbill/killbill

final DateTime bundleStartDate = now.minusHours(10);
final DateTime alignStartDate = bundleStartDate.plusHours(5);
final DateTime effectiveDateInThePast = defaultSubscriptionBase.getBundleStartDate().minusHours(10);
final TimedPhase[] phasesInThePast = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDateInThePast);
Assert.assertNull(phasesInThePast[0]);

代码示例来源:origin: killbill/killbill

final DateTime bundleStartDate = now.minusHours(10);
final DateTime alignStartDate = bundleStartDate.plusHours(5);
final DateTime effectiveDateInThePast = defaultSubscriptionBase.getStartDate().minusHours(10);
final TimedPhase[] phasesInThePast = getTimedPhasesOnCreate(productName, initialPhase, defaultSubscriptionBase, effectiveDateInThePast);
Assert.assertNull(phasesInThePast[0]);

代码示例来源:origin: apache/incubator-druid

@Test
public void calculationIntervalTest()
{
 DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100);
 DataSegment segmentB = createSegment(
   DATA_SOURCE,
   shifted1HInterval(REFERENCE_TIME, (int) TimeUnit.DAYS.toHours(50)),
   100
 );
 SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(
   new Interval(REFERENCE_TIME.minusHours(5), REFERENCE_TIME.plusHours(5))
 );
 prototype.addSegment(segmentA);
 SegmentsCostCache.Bucket bucket = prototype.build();
 assertTrue(bucket.inCalculationInterval(segmentA));
 assertFalse(bucket.inCalculationInterval(segmentB));
}

代码示例来源:origin: apache/incubator-druid

@Test
public void twoSegmentsCostTest()
{
 DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100);
 DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, -2), 100);
 SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval(
   REFERENCE_TIME.minusHours(5),
   REFERENCE_TIME.plusHours(5)
 ));
 prototype.addSegment(segmentA);
 SegmentsCostCache.Bucket bucket = prototype.build();
 double segmentCost = bucket.cost(segmentB);
 assertEquals(7.8735899489011E-4, segmentCost, EPSILON);
}

代码示例来源:origin: apache/incubator-druid

@Test
public void sameSegmentCostTest()
{
 DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100);
 DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100);
 SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval(
   REFERENCE_TIME.minusHours(5),
   REFERENCE_TIME.plusHours(5)
 ));
 prototype.addSegment(segmentA);
 SegmentsCostCache.Bucket bucket = prototype.build();
 double segmentCost = bucket.cost(segmentB);
 assertEquals(8.26147353873985E-4, segmentCost, EPSILON);
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testCompareHour()
 {
  Result<Object> res = new Result<Object>(time, null);
  Result<Object> same = new Result<Object>(time.plusMinutes(55), null);
  Result<Object> greater = new Result<Object>(time.plusHours(1), null);
  Result<Object> less = new Result<Object>(time.minusHours(1), null);

  Granularity hour = Granularities.HOUR;
  Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, same), 0);
  Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, greater), descending ? 1 : -1);
  Assert.assertEquals(ResultGranularTimestampComparator.create(hour, descending).compare(res, less), descending ? -1 : 1);
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testCompareDay()
{
 Result<Object> res = new Result<Object>(time, null);
 Result<Object> same = new Result<Object>(time.plusHours(12), null);
 Result<Object> greater = new Result<Object>(time.plusHours(25), null);
 Result<Object> less = new Result<Object>(time.minusHours(1), null);
 Granularity day = Granularities.DAY;
 Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, same), 0);
 Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, greater), descending ? 1 : -1);
 Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, less), descending ? -1 : 1);
}

代码示例来源:origin: apache/incubator-druid

@Test
public void multipleSegmentsCostTest()
{
 DataSegment segmentA = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, -2), 100);
 DataSegment segmentB = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 0), 100);
 DataSegment segmentC = createSegment(DATA_SOURCE, shifted1HInterval(REFERENCE_TIME, 2), 100);
 SegmentsCostCache.Bucket.Builder prototype = SegmentsCostCache.Bucket.builder(new Interval(
   REFERENCE_TIME.minusHours(5),
   REFERENCE_TIME.plusHours(5)
 ));
 prototype.addSegment(segmentA);
 prototype.addSegment(segmentC);
 SegmentsCostCache.Bucket bucket = prototype.build();
 double segmentCost = bucket.cost(segmentB);
 assertEquals(0.001574717989780039, segmentCost, EPSILON);
}

代码示例来源:origin: prestodb/presto

@Test
public void testAddFieldToTimestamp()
{
  assertFunction("date_add('millisecond', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMillis(3), session));
  assertFunction("date_add('second', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusSeconds(3), session));
  assertFunction("date_add('minute', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMinutes(3), session));
  assertFunction("date_add('hour', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(3), session));
  assertFunction("date_add('hour', 23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusHours(23), session));
  assertFunction("date_add('hour', -4, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(4), session));
  assertFunction("date_add('hour', -23, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.minusHours(23), session));
  assertFunction("date_add('day', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusDays(3), session));
  assertFunction("date_add('week', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusWeeks(3), session));
  assertFunction("date_add('month', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3), session));
  assertFunction("date_add('quarter', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusMonths(3 * 3), session));
  assertFunction("date_add('year', 3, " + TIMESTAMP_LITERAL + ")", TimestampType.TIMESTAMP, sqlTimestampOf(TIMESTAMP.plusYears(3), session));
  assertFunction("date_add('millisecond', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMillis(3)));
  assertFunction("date_add('second', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusSeconds(3)));
  assertFunction("date_add('minute', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMinutes(3)));
  assertFunction("date_add('hour', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusHours(3)));
  assertFunction("date_add('day', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusDays(3)));
  assertFunction("date_add('week', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusWeeks(3)));
  assertFunction("date_add('month', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3)));
  assertFunction("date_add('quarter', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusMonths(3 * 3)));
  assertFunction("date_add('year', 3, " + WEIRD_TIMESTAMP_LITERAL + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(WEIRD_TIMESTAMP.plusYears(3)));
}

相关文章

DateTime类方法