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

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

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

DateTime.plusMonths介绍

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

The calculation will do its best to only change the month field retaining the same day of month. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2007-03-31 plus one month cannot result in 2007-04-31, so the day of month is adjusted to 2007-04-30.

The following three lines are identical in effect:

DateTime added = dt.plusMonths(6); 
DateTime added = dt.plus(Period.months(6)); 
DateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);

This datetime instance is immutable and unaffected by this method call.
[中]返回此datetime加上指定月数的副本。
计算将尽最大努力只更改保留当月同一天的月份字段。但是,在某些情况下,可能需要更改较小的字段。例如,2007-03-31加上一个月不能生成2007-04-31,因此将月日调整为2007-04-30。
以下三行实际上是相同的:

DateTime added = dt.plusMonths(6); 
DateTime added = dt.plus(Period.months(6)); 
DateTime added = dt.withFieldAdded(DurationFieldType.months(), 6);

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

代码示例

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

@Override
public DateTime addToDateTime(final DateTime dateTime) {
  if ((number == null) && (unit != TimeUnit.UNLIMITED)) {
    return dateTime;
  }
  switch (unit) {
    case DAYS:
      return dateTime.plusDays(number);
    case WEEKS:
      return dateTime.plusWeeks(number);
    case MONTHS:
      return dateTime.plusMonths(number);
    case YEARS:
      return dateTime.plusYears(number);
    case UNLIMITED:
    default:
      throw new IllegalStateException("Unexpected duration unit " + unit);
  }
}

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

@Override
public DateTime addToDateTime(final DateTime dateTime) throws CatalogApiException {
  if ((number == null) && (unit != TimeUnit.UNLIMITED)) {
    return dateTime;
  }
  switch (unit) {
    case DAYS:
      return dateTime.plusDays(number);
    case WEEKS:
      return dateTime.plusWeeks(number);
    case MONTHS:
      return dateTime.plusMonths(number);
    case YEARS:
      return dateTime.plusYears(number);
    case UNLIMITED:
    default:
      throw new CatalogApiException(ErrorCode.CAT_UNDEFINED_DURATION, unit);
  }
}

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

@Test
public void testPatchedNameKeysSydney() throws Exception {
  // the tz database does not have unique name keys [1716305]
  DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
  
  DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
  String str1 = zone.getName(now.getMillis());
  String str2 = zone.getName(now.plusMonths(6).getMillis());
  assertEquals(false, str1.equals(str2));
}

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

@Test
public void testPatchedNameKeysSydneyHistoric() throws Exception {
  // the tz database does not have unique name keys [1716305]
  DateTimeZone zone = DateTimeZone.forID("Australia/Sydney");
  
  DateTime now = new DateTime(1996, 1, 1, 0, 0, 0, 0);
  String str1 = zone.getName(now.getMillis());
  String str2 = zone.getName(now.plusMonths(6).getMillis());
  assertEquals(false, str1.equals(str2));
}

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

@Test
public void testPatchedNameKeysGazaHistoric() throws Exception {
  // the tz database does not have unique name keys [1716305]
  DateTimeZone zone = DateTimeZone.forID("Africa/Johannesburg");
  
  DateTime now = new DateTime(1943, 1, 1, 0, 0, 0, 0);
  String str1 = zone.getName(now.getMillis());
  String str2 = zone.getName(now.plusMonths(6).getMillis());
  assertEquals(false, str1.equals(str2));
}

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

@Test
public void testPatchedNameKeysLondon() throws Exception {
  // the tz database does not have unique name keys [1716305]
  DateTimeZone zone = DateTimeZone.forID("Europe/London");
  
  DateTime now = new DateTime(2007, 1, 1, 0, 0, 0, 0);
  String str1 = zone.getName(now.getMillis());
  String str2 = zone.getName(now.plusMonths(6).getMillis());
  assertEquals(false, str1.equals(str2));
}

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

@Test
public void testAddFieldToDate()
{
  assertFunction("date_add('day', 0, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE));
  assertFunction("date_add('day', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusDays(3)));
  assertFunction("date_add('week', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusWeeks(3)));
  assertFunction("date_add('month', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusMonths(3)));
  assertFunction("date_add('quarter', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusMonths(3 * 3)));
  assertFunction("date_add('year', 3, " + DATE_LITERAL + ")", DateType.DATE, toDate(DATE.plusYears(3)));
}

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

@Test
public void testDublin() {
  DateTimeZone zone = DateTimeZone.forID("Europe/Dublin");
  DateTime winter = new DateTime(2018, 1, 1, 0, 0, 0, 0, zone);
  assertEquals(0, zone.getStandardOffset(winter.getMillis()));
  assertEquals(0, zone.getOffset(winter.getMillis()));
  assertEquals(true, zone.isStandardOffset(winter.getMillis()));
  assertEquals("Greenwich Mean Time", zone.getName(winter.getMillis()));
  assertEquals("GMT", zone.getNameKey(winter.getMillis()));
  DateTime summer = winter.plusMonths(6);
  assertEquals(0, zone.getStandardOffset(summer.getMillis()));
  assertEquals(3600000, zone.getOffset(summer.getMillis()));
  assertEquals(false, zone.isStandardOffset(summer.getMillis()));
  assertEquals(true, zone.getName(summer.getMillis()).startsWith("Irish "));
  assertEquals("IST", zone.getNameKey(summer.getMillis()));
}

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

@Test(groups = "fast")
public void testCreateWithTargetPhaseType2() throws Exception {
  final String productName = "pistol-monthly";
  final DateTime now = clock.getUTCNow();
  final DateTime bundleStartDate = now;
  final DateTime alignStartDate = bundleStartDate;
  final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow());
  final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
  Assert.assertEquals(phases.length, 2);
  Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
  Assert.assertEquals(phases[0].getStartPhase(), now);
  Assert.assertNull(phases[1]);
  final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
  final String newProductName = "shotgun-monthly";
  final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow());
  final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
  final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
  Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
  Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}

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

@Test(groups = "fast")
public void testCreateWithTargetPhaseType1() throws Exception {
  final String productName = "pistol-monthly";
  final DateTime now = clock.getUTCNow();
  final DateTime bundleStartDate = now;
  final DateTime alignStartDate = bundleStartDate;
  final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow());
  final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
  Assert.assertEquals(phases.length, 2);
  Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
  Assert.assertEquals(phases[0].getStartPhase(), now);
  Assert.assertNull(phases[1]);
  final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
  final String newProductName = "pistol-monthly-notrial";
  final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow());
  final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
  final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
  Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
  Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}

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

public static DateTime addOrRemoveDuration(final DateTime input, final List<Duration> durations, final boolean add) {
  DateTime result = input;
  for (final Duration cur : durations) {
    switch (cur.getUnit()) {
      case DAYS:
        result = add ? result.plusDays(cur.getNumber()) : result.minusDays(cur.getNumber());
        break;
      case MONTHS:
        result = add ? result.plusMonths(cur.getNumber()) : result.minusMonths(cur.getNumber());
        break;
      case YEARS:
        result = add ? result.plusYears(cur.getNumber()) : result.minusYears(cur.getNumber());
        break;
      case UNLIMITED:
      default:
        throw new RuntimeException("Trying to move to unlimited time period");
    }
  }
  return result;
}

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

@Test(groups = "fast")
public void testCreateWithTargetPhaseType4() throws Exception {
  final String productName = "pistol-monthly";
  final DateTime now = clock.getUTCNow();
  final DateTime bundleStartDate = now;
  final DateTime alignStartDate = bundleStartDate;
  final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow());
  final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
  Assert.assertEquals(phases.length, 2);
  Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
  Assert.assertEquals(phases[0].getStartPhase(), now);
  Assert.assertNull(phases[1]);
  final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
  final String newProductName = "pistol-annual-gunclub-discount-notrial";
  final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow());
  final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
  // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType
  final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
  Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
  Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}

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

Assert.assertEquals(phases[1].getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1));
Assert.assertEquals(nextTimePhase.getStartPhase(), defaultSubscriptionBase.getStartDate().plusMonths(1));
final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(1);
changeSubscription(effectiveChangeDate, defaultSubscriptionBase, productName, newProductName, initialPhase);

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

@Test(groups = "fast")
public void testChangeWithTargetPhaseType1() throws Exception {
  final String productName = "pistol-monthly";
  final DateTime now = clock.getUTCNow();
  final DateTime bundleStartDate = now;
  final DateTime alignStartDate = bundleStartDate;
  final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow());
  final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, null, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
  Assert.assertEquals(phases.length, 2);
  Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.TRIAL);
  Assert.assertEquals(phases[0].getStartPhase(), now);
  Assert.assertEquals(phases[1].getPhase().getPhaseType(), PhaseType.EVERGREEN);
  Assert.assertEquals(phases[1].getStartPhase(), now.plusDays(30));
  final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.TRIAL);
  final String newProductName = "pistol-annual-gunclub-discount";
  final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow());
  final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusDays(5);
  final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext);
  // We end up straight on DISCOUNT but because we are using START_OF_SUBSCRIPTION alignment, such Phase starts with beginning of subscription
  Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
  Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.DISCOUNT);
  final TimedPhase nextPhase = planAligner.getNextTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, PhaseType.DISCOUNT, catalog, internalCallContext);
  Assert.assertEquals(nextPhase.getStartPhase(), alignStartDate.plusMonths(6));
  Assert.assertEquals(nextPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}

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

@Test(groups = "fast")
public void testCreateWithTargetPhaseType3() throws Exception {
  final String productName = "pistol-monthly";
  final DateTime now = clock.getUTCNow();
  final DateTime bundleStartDate = now;
  final DateTime alignStartDate = bundleStartDate;
  final Plan plan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(productName, clock.getUTCNow());
  final TimedPhase[] phases = planAligner.getCurrentAndNextTimedPhaseOnCreate(alignStartDate, bundleStartDate, plan, PhaseType.EVERGREEN, PriceListSet.DEFAULT_PRICELIST_NAME, now, catalog, internalCallContext);
  Assert.assertEquals(phases.length, 2);
  Assert.assertEquals(phases[0].getPhase().getPhaseType(), PhaseType.EVERGREEN);
  Assert.assertEquals(phases[0].getStartPhase(), now);
  Assert.assertNull(phases[1]);
  final DefaultSubscriptionBase defaultSubscriptionBase = createSubscription(bundleStartDate, alignStartDate, productName, PhaseType.EVERGREEN);
  final String newProductName = "assault-rifle-annual-gunclub-discount";
  final Plan newPlan = catalogService.getFullCatalog(true, true, internalCallContext).findPlan(newProductName, clock.getUTCNow());
  final DateTime effectiveChangeDate = defaultSubscriptionBase.getStartDate().plusMonths(15);
  // Because new Plan has an EVERGREEN PhaseType we end up directly on that PhaseType
  final TimedPhase currentPhase = planAligner.getCurrentTimedPhaseOnChange(defaultSubscriptionBase, newPlan, effectiveChangeDate, null, catalog, internalCallContext);
  Assert.assertEquals(currentPhase.getStartPhase(), alignStartDate);
  Assert.assertEquals(currentPhase.getPhase().getPhaseType(), PhaseType.EVERGREEN);
}

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

final DateTime expectedNextPhaseDate = subscription.getStartDate().plusDays(30).plusMonths(6);
final SubscriptionBaseTransition nextPhase = subscription.getPendingTransition();

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

final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);

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

final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);

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

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

Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(2));
clock.addDeltaFromReality(it.toDurationMillis());
assertListenerStatus();
testListener.pushExpectedEvent(NextEvent.CANCEL);
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
clock.addDeltaFromReality(it.toDurationMillis());
assertListenerStatus();

相关文章

DateTime类方法