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

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

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

DateTime.plusHours介绍

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

The calculation will add 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 adding one hour to 01:30 will result in 03:30. This is a duration of one hour later, even though the hour field value changed from 1 to 3.

The following three lines are identical in effect:

DateTime added = dt.plusHours(6); 
DateTime added = dt.plus(Period.hours(6)); 
DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6);

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

DateTime added = dt.plusHours(6); 
DateTime added = dt.plus(Period.hours(6)); 
DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6);

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

代码示例

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

private DataSegment createSegment(int shift)
{
 return new DataSegment(
   "dataSource",
   new Interval(referenceTime.plusHours(shift), referenceTime.plusHours(shift).plusHours(1)),
   "version",
   Collections.emptyMap(),
   Collections.emptyList(),
   Collections.emptyList(),
   null,
   0,
   100
 );
}

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

private void sampleDateTime() {
  List<String> text = new ArrayList<String>();
  DateTime now = DateTime.now();
  text.add("Now: " + now);
  text.add("Now + 30 minutes: " + now.plusMinutes(30));
  text.add("Now + 5 hours: " + now.plusHours(5));
  text.add("Now + 2 days: " + now.plusDays(2));
  addSample("DateTime", text);
}

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

public static Interval shifted1HInterval(DateTime REFERENCE_TIME, int shiftInHours)
{
 return new Interval(
   REFERENCE_TIME.plusHours(shiftInHours),
   REFERENCE_TIME.plusHours(shiftInHours + 1)
 );
}

代码示例来源:origin: stackoverflow.com

DateTime dt = new DateTime();
DateTime twoHoursLater = dt.plusHours(2).plusMinutes(10).plusSeconds(5);
Period period = new Period(dt, twoHoursLater);
PeriodFormatter HHMMSSFormater = new PeriodFormatterBuilder()
    .printZeroAlways()
    .minimumPrintedDigits(2)
    .appendHours().appendSeparator("-")
    .appendMinutes().appendSeparator("-")
    .appendSeconds()
    .toFormatter(); // produce thread-safe formatter
System.out.println(HHMMSSFormater.print(period));

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

private void sampleDateRange() {
  List<String> text = new ArrayList<String>();
  DateTime start = DateTime.now();
  DateTime end = start.plusMinutes(30).plusHours(2).plusDays(56);
  text.add("Range: " + DateUtils.formatDateRange(this, start, end, 0));
  text.add("Range (with year): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_YEAR));
  text.add("Range (abbreviated): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_ABBREV_ALL));
  text.add("Range (with time): " + DateUtils.formatDateRange(this, start, end, DateUtils.FORMAT_SHOW_TIME));
  addSample("DateUtils.formatDateRange()", text);
}

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

DataSegment createSegment(DateTime t)
{
 return new DataSegment(
   "test",
   new Interval(t, t.plusHours(1)),
   "v1",
   null,
   null,
   null,
   null,
   0,
   0
 );
}

代码示例来源: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: apache/incubator-druid

private DataSegment createRandomSegment(Random random, DateTime referenceTime)
 {
  int timeShift = random.nextInt((int) TimeUnit.DAYS.toHours(DAYS_IN_MONTH * 12));
  return new DataSegment(
    String.valueOf(random.nextInt(50)),
    new Interval(referenceTime.plusHours(timeShift), referenceTime.plusHours(timeShift + 1)),
    "version",
    Collections.emptyMap(),
    Collections.emptyList(),
    Collections.emptyList(),
    null,
    0,
    100
  );
 }
}

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

@Before
public void setUp()
{
 coordinator = EasyMock.createMock(DruidCoordinator.class);
 mockPeon = EasyMock.createMock(LoadQueuePeon.class);
 emitter = EasyMock.createMock(ServiceEmitter.class);
 EmittingLogger.registerEmitter(emitter);
 databaseRuleManager = EasyMock.createMock(MetadataRuleManager.class);
 DateTime start = DateTimes.of("2012-01-01");
 availableSegments = new ArrayList<>();
 for (int i = 0; i < 24; i++) {
  availableSegments.add(
    new DataSegment(
      "test",
      new Interval(start, start.plusHours(1)),
      DateTimes.nowUtc().toString(),
      new HashMap<>(),
      new ArrayList<>(),
      new ArrayList<>(),
      NoneShardSpec.instance(),
      IndexIO.CURRENT_VERSION_ID,
      1
    )
  );
  start = start.plusHours(1);
 }
 ruleRunner = new DruidCoordinatorRuleRunner(new ReplicationThrottler(24, 1), coordinator);
}

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

@Test(groups = "fast")
public void testEventsForCancelledSubscriptionAfterTransfer() throws Exception {
  final DateTime subscriptionStartTime = clock.getUTCNow();
  final DateTime subscriptionCancelTime = subscriptionStartTime.plusDays(1);
  final ImmutableList<ExistingEvent> existingEvents = ImmutableList.<ExistingEvent>of(createEvent(subscriptionStartTime, SubscriptionBaseTransitionType.CREATE),
                                            createEvent(subscriptionCancelTime, SubscriptionBaseTransitionType.CANCEL));
  final SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder();
  final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(subscriptionBuilder);
  final DateTime transferDate = subscriptionStartTime.plusHours(1);
  final List<SubscriptionBaseEvent> events = transferApi.toEvents(existingEvents, subscription, transferDate, catalog, internalCallContext);
  Assert.assertEquals(events.size(), 1);
  Assert.assertEquals(events.get(0).getType(), EventType.API_USER);
  Assert.assertEquals(events.get(0).getEffectiveDate(), transferDate);
  Assert.assertEquals(((ApiEventTransfer) events.get(0)).getApiEventType(), ApiEventType.TRANSFER);
}

代码示例来源: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

@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 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 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 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 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 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类方法