本文整理了Java中java.time.LocalDateTime.plusYears()
方法的一些代码示例,展示了LocalDateTime.plusYears()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LocalDateTime.plusYears()
方法的具体详情如下:
包路径:java.time.LocalDateTime
类名称:LocalDateTime
方法名:plusYears
[英]Returns a copy of this LocalDateTime with the specified period in years added.
This method adds the specified amount to the years field in three steps:
For example, 2008-02-29 (leap year) plus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.
This instance is immutable and unaffected by this method call.
[中]返回此LocalDateTime的副本,并添加指定的期间(以年为单位)。
此方法分三步将指定金额添加到“年份”字段:
1.将输入年份添加到年份字段
1.检查结果日期是否无效
1.如有必要,将月份日期调整为最后一个有效日期
例如,2008-02-29(闰年)加上一年将导致无效日期2009-02-29(标准年)。将选择当月的最后一个有效日期2009-02-28,而不是返回无效结果。
此实例是不可变的,不受此方法调用的影响。
代码示例来源:origin: yu199195/Raincat
/**
* 计算 year 年后的时间.
*
* @param date 长日期
* @param year 需要增加的年数
* @return 增加后的日期
*/
public static LocalDateTime addYear(final LocalDateTime date, final int year) {
return date.plusYears(year);
}
代码示例来源:origin: yu199195/Raincat
/**
* 获得当天近一年.
*
* @return 日期
*/
public static LocalDateTime getAYearFromNow() {
LocalDateTime date = LocalDateTime.now();
//一年前
return date.plusYears(-1);
}
代码示例来源:origin: stackoverflow.com
tempDateTime = tempDateTime.plusYears( years );
代码示例来源:origin: SeanDragon/protools
public DatePlus addYear(long year) {
this.localDateTime = this.localDateTime.plusYears(year);
return this;
}
代码示例来源:origin: lets-blade/blade
@Test
public void testPrettyTime() {
Assert.assertEquals("去年", DateKit.prettyTime(LocalDateTime.now().plusYears(-1), Locale.CHINESE));
Assert.assertEquals("上个月", DateKit.prettyTime(LocalDateTime.now().plusMonths(-1), Locale.CHINESE));
Assert.assertEquals("上周", DateKit.prettyTime(LocalDateTime.now().plusWeeks(-1), Locale.CHINESE));
Assert.assertEquals("昨天", DateKit.prettyTime(LocalDateTime.now().plusDays(-1), Locale.CHINESE));
Assert.assertEquals("1小时前", DateKit.prettyTime(LocalDateTime.now().plusHours(-1), Locale.CHINESE));
Assert.assertEquals("1分钟前", DateKit.prettyTime(LocalDateTime.now().plusMinutes(-1), Locale.CHINESE));
Assert.assertEquals("刚刚", DateKit.prettyTime(LocalDateTime.now().plusSeconds(-1), Locale.CHINESE));
Assert.assertEquals("10秒前", DateKit.prettyTime(LocalDateTime.now().plusSeconds(-10), Locale.CHINESE));
}
代码示例来源:origin: biezhi/learn-java8
public static void main(String[] args) {
// 创建一个LocalDateTime实例
LocalDateTime localDateTime = LocalDateTime.now();
// 使用指定的年月日、时分秒、纳秒来新建对象
LocalDateTime localDateTime2 = LocalDateTime.of(2018, 11, 26, 13, 55, 36, 123);
// 3年后的现在
LocalDateTime dt1 = localDateTime.plusYears(3);
// 3年前的现在
LocalDateTime dt2 = localDateTime.minusYears(3);
System.out.println("localDateTime : " + localDateTime);
System.out.println("localDateTime2 : " + localDateTime2);
System.out.println("dt1 : " + dt1);
System.out.println("dt2 : " + dt2);
}
}
代码示例来源:origin: keets2012/Lottor
/**
* 计算 year 年后的时间
*
* @param date 长日期
* @param year 需要增加的年数
* @return 增加后的日期
*/
public static LocalDateTime addYear(LocalDateTime date, int year) {
return date.plusYears(year);
}
代码示例来源:origin: com.liumapp.qtools.date/qtools-date
public DatePlus addYear(long year) {
this.localDateTime = this.localDateTime.plusYears(year);
return this;
}
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
* <p>
* This method subtracts the specified amount from the years field in three steps:
* <ol>
* <li>Subtract the input years from the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) minus one year would result in the
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2009-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to subtract, may be negative
* @return a {@code LocalDateTime} based on this date-time with the years subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDateTime minusYears(long years) {
return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
}
代码示例来源:origin: keets2012/Lottor
/**
* 获得当天近一年
* @return LocalDateTime
*/
public static LocalDateTime getAYearFromNow() {
LocalDateTime date = LocalDateTime.now();
//一年前
return date.plusYears(-1);
}
代码示例来源:origin: stackoverflow.com
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class SO25801191 {
public static void main(String[] args) {
System.out.println(getDateAsStringIncrementedByYear("04/22/1980 11:30:20", "MM/dd/yyyy HH:mm:ss", 1));
}
private static String getDateAsStringIncrementedByYear(String inputDateStr, String pattern, long yearsToIncrement) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime dateTime = LocalDateTime.parse(inputDateStr, formatter);
return dateTime.plusYears(yearsToIncrement).format(formatter);
}
}
代码示例来源:origin: qala-io/datagen
public static LocalDateTime inYears(int n) {
return now().plusYears(n);
}
public static LocalDateTime dayAgo() {
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this {@code OffsetDateTime} with the specified period in years added.
* <p>
* This method adds the specified amount to the years field in three steps:
* <ol>
* <li>Add the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) plus one year would result in the
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2009-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to add, may be negative
* @return an {@code OffsetDateTime} based on this date-time with the years added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public OffsetDateTime plusYears(long years) {
return with(dateTime.plusYears(years), offset);
}
代码示例来源:origin: stackoverflow.com
Instant now = Instant.now();
LocalDateTime start = now.atOffset(ZoneOffset.UTC).toLocalDateTime();
LocalDateTime end =
start.plusYears(duration.getYears())
.plusMonths(duration.getMonths())
.plusDays(duration.getDays())
.plusHours(duration.getHours())
.plusMinutes(duration.getMinutes())
.plusSeconds(duration.getSeconds());
long deltaInMillis = end.toInstant(ZoneOffset.UTC).toEpochMilli() - now.toEpochMilli();
代码示例来源:origin: com.github.seratch/java-time-backport
/**
* Returns a copy of this {@code ZonedDateTime} with the specified period in years added.
* <p>
* This operates on the local time-line,
* {@link LocalDateTime#plusYears(long) adding years} to the local date-time.
* This is then converted back to a {@code ZonedDateTime}, using the zone ID
* to obtain the offset.
* <p>
* When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap,
* then the offset will be retained if possible, otherwise the earlier offset will be used.
* If in a gap, the local date-time will be adjusted forward by the length of the gap.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param years the years to add, may be negative
* @return a {@code ZonedDateTime} based on this date-time with the years added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public ZonedDateTime plusYears(long years) {
return resolveLocal(dateTime.plusYears(years));
}
代码示例来源:origin: ralscha/springsecuritytotp
@Override
@Transactional
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
Object principal = event.getAuthentication().getPrincipal();
if (principal instanceof JpaUserDetails) {
User user = this.entityManager.find(User.class,
((JpaUserDetails) principal).getUserDbId());
user.setLockedOut(null);
user.setFailedLogins(null);
user.setExpirationDate(LocalDateTime.now().plusYears(1));
}
}
}
代码示例来源:origin: com.intrbiz.bergamot/bergamot-timerange
@Override
public LocalDateTime computeNextStartTime(Clock clock)
{
LocalDateTime next = super.computeNextStartTime(clock);
if (next == null) return null;
next = next.withMonth(this.month.getMonth() + 1);
// is it next year?
if (! next.isAfter(LocalDateTime.now(clock))) next = next.plusYears(1);
// check the date is valid
return (next.isAfter(LocalDateTime.now(clock))) ? next : null;
}
代码示例来源:origin: qala-io/datagen
public static RandomDate thisYear() {
return between(startOfYear(), startOfYear().plusYears(1).minusSeconds(1));
}
代码示例来源:origin: entando/entando-core
private void createSwaggerConsumer() throws ApsSystemException {
logger.info("Creating Swagger consumer");
ConsumerRecordVO swaggerConsumer;
String authUrl = baseConfigManager.getParam(SystemConstants.PAR_APPL_BASE_URL);
swaggerConsumer = new ConsumerRecordVO();
swaggerConsumer.setKey("swagger");
swaggerConsumer.setSecret("swaggerswagger");
swaggerConsumer.setName("Swagger");
swaggerConsumer.setDescription("Swagger");
swaggerConsumer.setCallbackUrl(authUrl + "api/webjars/springfox-swagger-ui/oauth2-redirect.html");
swaggerConsumer.setScope("global");
swaggerConsumer.setAuthorizedGrantTypes("password");
swaggerConsumer.setIssuedDate(Date.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)));
swaggerConsumer.setExpirationDate(Date.from(LocalDateTime.now().plusYears(10).toInstant(ZoneOffset.UTC)));
consumerManager.addConsumer(swaggerConsumer);
}
}
代码示例来源:origin: com.sqlapp/sqlapp-core
/**
* 年の加算を実行します
*
* @param date
* 日付型
* @param years
* 加算する年
* @return 年を加算した結果のカレンダー
*/
@SuppressWarnings("unchecked")
public static <T extends Temporal> T addYears(final T date, final int years) {
if (date == null) {
return null;
}
if (date instanceof LocalDate){
return (T)((LocalDate)date).plusYears(years);
}else if (date instanceof LocalDateTime){
return (T)((LocalDateTime)date).plusYears(years);
}else if (date instanceof OffsetDateTime){
return (T)((OffsetDateTime)date).plusYears(years);
}else if (date instanceof ZonedDateTime){
return (T)((ZonedDateTime)date).plusYears(years);
}else if (date instanceof YearMonth){
return (T)((YearMonth)date).plusYears(years);
}
return (T)date.plus(Duration.of(years, ChronoUnit.YEARS));
}
内容来源于网络,如有侵权,请联系作者删除!