本文整理了Java中org.joda.time.DateTime.plusYears()
方法的一些代码示例,展示了DateTime.plusYears()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTime.plusYears()
方法的具体详情如下:
包路径:org.joda.time.DateTime
类名称:DateTime
方法名:plusYears
[英]Returns a copy of this datetime plus the specified number of years.
The calculation will do its best to only change the year field retaining the same month of year. However, in certain circumstances, it may be necessary to alter smaller fields. For example, 2008-02-29 plus one year cannot result in 2009-02-29, so the day of month is adjusted to 2009-02-28.
The following three lines are identical in effect:
DateTime added = dt.plusYears(6);
DateTime added = dt.plus(Period.years(6));
DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
This datetime instance is immutable and unaffected by this method call.
[中]返回此datetime加上指定年数的副本。
计算将尽最大努力只更改保留一年中同一月份的“年份”字段。但是,在某些情况下,可能需要更改较小的字段。例如,2008-02-29加上一年不能产生2009-02-29,因此将月日调整为2009-02-28。
以下三行实际上是相同的:
DateTime added = dt.plusYears(6);
DateTime added = dt.plus(Period.years(6));
DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6);
此datetime实例是不可变的,不受此方法调用的影响。
代码示例来源:origin: stackoverflow.com
DateTime aDate = new DateTime(year, month, day, hour, minute, second);
DateTime anotherDate = new DateTime(anotherYear, anotherMonth, anotherDay, ...);
if (aDate.isAfter(anotherDate)) {...}
DateTime yearFromADate = aDate.plusYears(1);
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
/**
* A helper method used to construct a list of Credit Card Expiration Years
* Useful for expiration dropdown menus.
*
* @return List of the next ten years starting with the current year.
*/
public static List<String> getExpirationYearOptions() {
List<String> expirationYears = new ArrayList<>();
DateTime dateTime = new DateTime();
for (int i = 0; i < 10; i++) {
expirationYears.add(dateTime.plusYears(i).getYear() + "");
}
return expirationYears;
}
}
代码示例来源:origin: gocd/gocd
public V3X509CertificateGenerator(Date startDate, X500Name issuerDn, X500Name subjectDn,
PublicKey publicKey, BigInteger serialNumber) {
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
this.v3CertGen = new X509v3CertificateBuilder(issuerDn, serialNumber, startDate, new DateTime().plusYears(YEARS).toDate(), subjectDn, publicKeyInfo);
}
代码示例来源:origin: aws/aws-sdk-java
/**
* Parse the process output to retrieve the expiration date and time. The result includes any configured expiration buffer.
*/
private DateTime credentialExpirationTime(JsonNode credentialsJson) {
String expiration = getText(credentialsJson, "Expiration");
if (expiration != null) {
DateTime credentialExpiration = new DateTime(DateUtils.parseISO8601Date(expiration));
credentialExpiration = credentialExpiration.minus(expirationBufferUnit.toMillis(expirationBufferValue));
return credentialExpiration;
} else {
return DateTime.now().plusYears(9999);
}
}
代码示例来源: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: gocd/gocd
private X509Certificate createTypeOneX509Certificate(Date startDate, String principalDn, KeyPair keyPair) {
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
X500Principal principal = new X500Principal(principalDn);
certGen.setSerialNumber(serialNumber());
certGen.setIssuerDN(principal);
certGen.setNotBefore(startDate);
DateTime now = new DateTime(new Date());
certGen.setNotAfter(now.plusYears(YEARS).toDate());
certGen.setSubjectDN(principal); // note: same as issuer
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm(new SystemEnvironment().get(GO_SSL_CERTS_ALGORITHM));
try {
return certGen.generate(keyPair.getPrivate(), "BC");
} catch (Exception e) {
throw bomb(e);
}
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDate1() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime1);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 19);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDateC() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeC);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 20);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDate2() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime2);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 20);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDate3() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime3);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 20);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDateA() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeA);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 21);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: killbill/killbill
@Test(groups = "fast")
public void testComputeUTCDateTimeFromLocalDateB() {
final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeB);
final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8);
refreshCallContext(effectiveDateTime, timeZone);
final LocalDate endDate = new LocalDate(2013, 01, 21);
final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate);
assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0);
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testCompareAll()
{
Result<Object> r1 = new Result<Object>(time, null);
Result<Object> r2 = new Result<Object>(time.plusYears(5), null);
Assert.assertEquals(ResultGranularTimestampComparator.create(Granularities.ALL, descending).compare(r1, r2), 0);
}
代码示例来源: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: 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: apache/incubator-druid
@Test
public void testUnUsedHighRange() throws IOException
{
coordinator.announceHistoricalSegments(SEGMENTS);
unUseSegment();
Assert.assertEquals(
SEGMENTS,
ImmutableSet.copyOf(
coordinator.getUnusedSegmentsForInterval(
defaultSegment.getDataSource(),
defaultSegment.getInterval().withEnd(defaultSegment.getInterval().getEnd().plus(1))
)
)
);
Assert.assertEquals(
SEGMENTS,
ImmutableSet.copyOf(
coordinator.getUnusedSegmentsForInterval(
defaultSegment.getDataSource(),
defaultSegment.getInterval().withEnd(defaultSegment.getInterval().getEnd().plusYears(1))
)
)
);
}
代码示例来源:origin: killbill/killbill
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusYears(1));
clock.addDeltaFromReality(it.toDurationMillis());
assertListenerStatus();
代码示例来源: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: dlew/joda-time-android
@Test
public void testGetRelativeTimeSpanStringWithPreposition() {
Context ctx = InstrumentationRegistry.getContext();
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
LocalDate nextYear = today.plusYears(1);
assertEquals("12:35", DateUtils.getRelativeTimeSpanString(ctx, today, false));
assertEquals("at 12:35", DateUtils.getRelativeTimeSpanString(ctx, today, true));
assertEquals("Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrow, false));
assertEquals("on Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrow, true));
assertEquals("10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYear, false));
assertEquals("on 10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYear, true));
DateTime todayDt = DateTime.now();
DateTime tomorrowDt = todayDt.plusDays(1);
DateTime nextYearDt = todayDt.plusYears(1);
assertEquals("12:35", DateUtils.getRelativeTimeSpanString(ctx, todayDt, false));
assertEquals("at 12:35", DateUtils.getRelativeTimeSpanString(ctx, todayDt, true));
assertEquals("Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrowDt, false));
assertEquals("on Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrowDt, true));
assertEquals("10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYearDt, false));
assertEquals("on 10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYearDt, true));
}
内容来源于网络,如有侵权,请联系作者删除!