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

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

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

LocalDateTime.withDayOfMonth介绍

[英]Returns a copy of this LocalDateTime with the day-of-month altered. If the resulting LocalDateTime is invalid, an exception is thrown. The time does not affect the calculation and will be the same in the result.

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: stackoverflow.com

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
  public static void main(String[] args) {
    DateTimeFormatter fmt = DateTimeFormatter.RFC_1123_DATE_TIME;

    LocalDateTime tmp
      = LocalDateTime.now().withDayOfMonth(4).withMonthOfYear(8);
    System.out.println(fmt.format(tmp));

    tmp = tmp.withDayOfWeek(1);
    System.out.println(fmt.format(tmp));
  }
}

代码示例来源:origin: qala-io/datagen

public static LocalDateTime startOfMonth() {
  return startOfDay().withDayOfMonth(1);
}
public static LocalDateTime startOfDay() {

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

/**
 * Returns a copy of this {@code OffsetDateTime} with the day-of-month altered.
 * If the resulting {@code OffsetDateTime} is invalid, an exception is thrown.
 * The offset does not affect the calculation and will be the same in the result.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
 * @return an {@code OffsetDateTime} based on this date-time with the requested day, not null
 * @throws DateTimeException if the day-of-month value is invalid
 * @throws DateTimeException if the day-of-month is invalid for the month-year
 */
public OffsetDateTime withDayOfMonth(int dayOfMonth) {
  return with(dateTime.withDayOfMonth(dayOfMonth), offset);
}

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

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

代码示例来源:origin: espertechinc/esper

/**
 * NOTE: Code-generation-invoked method, method name and parameter order matters
 *
 * @param ldt   localdatetime
 * @param year  year
 * @param month month
 * @param day   day
 * @return ldt
 */
public static LocalDateTime actionSetYMDLocalDateTime(LocalDateTime ldt, Integer year, Integer month, Integer day) {
  if (year != null) {
    ldt = ldt.withYear(year);
  }
  if (month != null) {
    ldt = ldt.withMonth(month);
  }
  if (day != null) {
    ldt = ldt.withDayOfMonth(day);
  }
  return ldt;
}

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

@Test(expected = DateTimeException.class)
public void withInvalidDate() {
  LocalDateTime start = LocalDateTime.of(2017, Month.FEBRUARY, 2, 11, 30);
  start.withDayOfMonth(29);
}

代码示例来源:origin: com.intrbiz.bergamot/bergamot-timerange

@Override
public LocalDateTime computeNextStartTime(Clock clock)
{
  LocalDateTime next = super.computeNextStartTime(clock);
  if (next == null) return null;
  // set the day of month
  int dom = this.dayOfMonth > 0 ? this.dayOfMonth : (next.getMonth().maxLength() + this.dayOfMonth + 1);
  next = next.withDayOfMonth(dom);
  // is it next month
  if (! next.isAfter(LocalDateTime.now(clock))) next = next.plusMonths(1);
  // check the date is valid
  return (next.isAfter(LocalDateTime.now(clock))) ? next : null;
}

代码示例来源:origin: com.intrbiz.bergamot/bergamot-timerange

@Override
public LocalDateTime computeNextStartTime(Clock clock)
{
  LocalDateTime next = super.computeNextStartTime(clock);
  if (next == null) return null;
  next = next.withYear(this.year).withMonth(this.month.getMonth() + 1).withDayOfMonth(this.day);
  // check the date is valid
  return (next.isAfter(LocalDateTime.now(clock))) ? next : null;
}

代码示例来源:origin: net.anwiba.commons/anwiba-commons-swing-core

private LocalDateTime adapt(final LocalDateTime dateTime, final LocalDate date) {
 return dateTime.withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
}

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

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

public LocalDateTime truncateTo(LocalDateTime dt, Time interval) {
  switch(interval) {
  case MONTH_6:
    if(dt.getMonthValue() > 6)
      dt = dt.withMonth(7);
    else
      dt = dt.withMonth(1);
  case MONTH:
    dt = dt.withDayOfMonth(1);
  case DAY:
    dt = dt.withHour(0);
  case HOUR: 
    dt = dt.withMinute(0);
    dt = dt.withSecond(0);
    dt = dt.withNano(0);
  break;
  }
  return dt;
}

代码示例来源:origin: com.intrbiz.bergamot/bergamot-timerange

next = next.withDayOfMonth(1);
int addDays = this.dayOfWeek.toJavaTime().getValue() - next.getDayOfWeek().getValue();
if (addDays < 0) addDays = addDays + 7;
next = next.withDayOfMonth(next.getMonth().maxLength());
int subDays = next.getDayOfWeek().getValue() - this.dayOfWeek.toJavaTime().getValue();
if (subDays < 0) subDays = subDays + 7;

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

cal.withDayOfMonth(1);
cal.withDayOfMonth(1);

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

observance.getProperties().add(rrule);
observance.getProperties().add(new DtStart(startDate.withMonth(transitionRule.getMonth().getValue())
    .withDayOfMonth(transitionRule.getDayOfMonthIndicator())
    .with(transitionRule.getDayOfWeek()).format(DateTimeFormatter.ofPattern(DATE_TIME_TPL))));

代码示例来源:origin: org.mnode.ical4j/ical4j

observance.getProperties().add(rrule);
observance.getProperties().add(new DtStart(startDate.withMonth(transitionRule.getMonth().getValue())
    .withDayOfMonth(transitionRule.getDayOfMonthIndicator())
    .with(transitionRule.getDayOfWeek()).format(DateTimeFormatter.ofPattern(DATE_TIME_TPL))));

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

minute = prevInterval(_minutes, _minutes.length - 1);
cal.withDayOfMonth(cal.getDayOfMonth() - 1);
 cal.withDayOfMonth(0);

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

minute = nextInterval(_minutes, 0);
cal.withDayOfMonth(cal.getDayOfMonth() + 1);
 cal.withDayOfMonth(1);

相关文章

LocalDateTime类方法