java.time.LocalDateTime.withDayOfYear()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(3.5k)|赞(0)|评价(0)|浏览(104)

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

LocalDateTime.withDayOfYear介绍

[英]Returns a copy of this LocalDateTime with the day-of-year altered. If the resulting LocalDateTime is invalid, an exception is thrown.

This instance is immutable and unaffected by this method call.
[中]返回更改了日期的LocalDateTime的副本。如果生成的LocalDateTime无效,将引发异常。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: com.vaadin/vaadin-server

private LocalDateTime getDate(LocalDateTime date,
    DateTimeResolution forResolution) {
  if (date == null) {
    return null;
  }
  switch (forResolution) {
  case YEAR:
    return date.withDayOfYear(1).toLocalDate().atStartOfDay();
  case MONTH:
    return date.withDayOfMonth(1).toLocalDate().atStartOfDay();
  case DAY:
    return date.toLocalDate().atStartOfDay();
  case HOUR:
    return date.truncatedTo(ChronoUnit.HOURS);
  case MINUTE:
    return date.truncatedTo(ChronoUnit.MINUTES);
  case SECOND:
    return date.truncatedTo(ChronoUnit.SECONDS);
  default:
    assert false : "Unexpected resolution argument " + forResolution;
    return null;
  }
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this {@code OffsetDateTime} with the day-of-year altered.
 * If the resulting {@code OffsetDateTime} is invalid, an exception is thrown.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
 * @return an {@code OffsetDateTime} based on this date with the requested day, not null
 * @throws DateTimeException if the day-of-year value is invalid
 * @throws DateTimeException if the day-of-year is invalid for the year
 */
public OffsetDateTime withDayOfYear(int dayOfYear) {
  return with(dateTime.withDayOfYear(dayOfYear), offset);
}

代码示例来源:origin: com.github.seratch/java-time-backport

/**
 * Returns a copy of this {@code ZonedDateTime} with the day-of-year altered.
 * <p>
 * This operates on the local time-line,
 * {@link LocalDateTime#withDayOfYear(int) changing the day-of-year} of 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 dayOfYear  the day-of-year to set in the result, from 1 to 365-366
 * @return a {@code ZonedDateTime} based on this date with the requested day, not null
 * @throws DateTimeException if the day-of-year value is invalid
 * @throws DateTimeException if the day-of-year is invalid for the year
 */
public ZonedDateTime withDayOfYear(int dayOfYear) {
  return resolveLocal(dateTime.withDayOfYear(dayOfYear));
}

代码示例来源:origin: kousen/java_8_recipes

@Test
public void with() {
  LocalDateTime start = LocalDateTime.of(2017, Month.FEBRUARY, 2, 11, 30);
  LocalDateTime end = start.withMinute(45);
  assertEquals("2017-02-02T11:45:00", end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  end = start.withHour(16);
  assertEquals("2017-02-02T16:30:00", end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  end = start.withDayOfMonth(28);
  assertEquals("2017-02-28T11:30:00", end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  end = start.withDayOfYear(300);
  assertEquals("2017-10-27T11:30:00", end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
  end = start.withYear(2020);
  assertEquals("2020-02-02T11:30:00", end.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}

相关文章

LocalDateTime类方法