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

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

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

LocalDateTime.truncatedTo介绍

[英]Returns a copy of this LocalDateTime with the time truncated.

Truncation returns a copy of the original date-time with fields smaller than the specified unit set to zero. For example, truncating with the ChronoUnit#MINUTES unit will set the second-of-minute and nano-of-second field to zero.

The unit must have a TemporalUnit#getDuration()that divides into the length of a standard day without remainder. This includes all supplied time units on ChronoUnit and ChronoUnit#DAYS. Other units throw an exception.

This instance is immutable and unaffected by this method call.
[中]返回此LocalDateTime的副本,时间被截断。
截断返回原始日期时间的副本,其中字段小于指定的单位并设置为零。例如,使用ChronoUnit#MINUTES单位进行截断会将秒数和秒数字段设置为零。
该单位必须有一个临时单位#getDuration(),它可以划分为一个标准日的长度,没有余数。这包括在ChronoUnit和ChronoUnit#天提供的所有时间单位。其他单位抛出异常。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

LocalDateTime now =  LocalDateTime.now();
LocalDateTime roundFloor =  now.truncatedTo(ChronoUnit.MINUTES);
LocalDateTime roundCeiling =  now.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1);

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

LocalDateTime now = LocalDateTime.now();
System.out.println("Pre-Truncate:  " + now);
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
System.out.println("Post-Truncate: " + now.truncatedTo(ChronoUnit.SECONDS).format(dtf));

代码示例来源:origin: kiegroup/optaplanner

@Test
public void fullLocalDateTimeRange() {
  TemporalUnit unit = ChronoUnit.DAYS;
  LocalDateTime from = LocalDateTime.MIN;
  LocalDateTime to = LocalDateTime.MAX.truncatedTo(unit);
  int increment = 1;
  TemporalValueRange<LocalDateTime> range = new TemporalValueRange<>(from, to, increment, unit);
  assertEquals(from.until(to, unit), range.getSize() * increment);
  assertTrue(range.contains(from));
  assertFalse(range.contains(to));
}

代码示例来源: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: rakam-io/rakam

private long millisToNextHour() {
  LocalDateTime nextHour = LocalDateTime.now().plusHours(1).truncatedTo(ChronoUnit.HOURS);
  return LocalDateTime.now().until(nextHour, ChronoUnit.MINUTES);
}

代码示例来源:origin: domaframework/doma

@Override
public LocalDateTime roundDownTimePart(LocalDateTime localDateTime) {
 if (localDateTime == null) {
  return null;
 }
 return localDateTime.truncatedTo(ChronoUnit.DAYS);
}

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * Returns a {@link java.time.LocalDateTime} with the time portion cleared.
 *
 * @param self a LocalDateTime
 * @return a LocalDateTime
 * @since 2.5.0
 */
public static LocalDateTime clearTime(final LocalDateTime self) {
  return self.truncatedTo(DAYS);
}

代码示例来源:origin: com.sqlapp/sqlapp-core

/**
 * 時刻情報を切り捨てます
 * 
 * @param date
 *            日付
 * @return 時刻情報を切り捨てた日付
 */
public static LocalDateTime truncateTime(final LocalDateTime date) {
  if (date == null) {
    return null;
  }
  return date.truncatedTo(ChronoUnit.DAYS);
}

代码示例来源:origin: inspire-software/yes-cart

/**
 * Determine start of day for this zoned date time (the start is at 00:00:00).
 *
 * @param date date time
 *
 * @return start of the day
 */
public static LocalDateTime ldtAtStartOfDay(final LocalDateTime date) {
  return date != null ? date.truncatedTo(ChronoUnit.DAYS) : null;
}

代码示例来源:origin: org.pageseeder.ox/pso-ox-core

/**
* Format.
*
* @param time the time
* @return the string
*/
private String format(LocalDateTime time) {
 return time.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

代码示例来源:origin: eXparity/hamcrest-date

@Override
public boolean isAfter(final LocalDateTime other) {
  return wrapped.get().truncatedTo(accuracy).isAfter(other.truncatedTo(accuracy));
}

代码示例来源:origin: com.nhl.link.rest/link-rest-java8

@Override
protected boolean encodeNonNullObject(Object object, JsonGenerator out) throws IOException {
  LocalDateTime dateTime = (LocalDateTime) object;
  String formatted = dateTime.truncatedTo(ChronoUnit.SECONDS).toString();
  out.writeObject(formatted);
  return true;
}

代码示例来源:origin: domaframework/doma

@Override
public LocalDateTime roundUpTimePart(LocalDateTime localDateTime) {
 if (localDateTime == null) {
  return null;
 }
 return localDateTime.plusDays(1).truncatedTo(ChronoUnit.DAYS);
}

代码示例来源:origin: peter-lawrey/Performance-Examples

@Benchmark
public LocalDateTime usingLocalDateTime() {
  LocalDateTime now = LocalDateTime.now();
  LocalDateTime inSecs = now.truncatedTo(ChronoUnit.SECONDS);
  LocalDateTime tomorrow = inSecs.plusDays(1);
  return tomorrow;
}

代码示例来源:origin: inspire-software/yes-cart

/**
 * Determine start of the year for this local date time (the start is on the 1st at 00:00:00).
 *
 * @param date date time
 *
 * @return start of the year
 */
public static LocalDateTime ldtAtStartOfYear(final LocalDateTime date) {
  return date != null ? date.with(TemporalAdjusters.firstDayOfYear()).truncatedTo(ChronoUnit.DAYS) : null;
}

代码示例来源:origin: inspire-software/yes-cart

/**
 * Determine start of the month for this local date time (the start is on the 1st at 00:00:00).
 *
 * @param date date time
 *
 * @return start of the month
 */
public static LocalDateTime ldtAtStartOfMonth(final LocalDateTime date) {
  return date != null ? date.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS) : null;
}

代码示例来源:origin: inspire-software/yes-cart

/**
 * Determine start of the week for this local date time (the start is on Monday at 00:00:00).
 *
 * @param date date time
 *
 * @return start of the week
 */
public static LocalDateTime ldtAtStartOfWeek(final LocalDateTime date) {
  return date != null ? date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).truncatedTo(ChronoUnit.DAYS) : null;
}

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

public LocalDateTime evaluate(LocalDateTime ldt, EventBean[] eventsPerStream, boolean isNewData, ExprEvaluatorContext context) {
  if (code == ApacheCommonsDateUtils.MODIFY_TRUNCATE) {
    return ldt.truncatedTo(fieldName.getChronoUnit());
  } else if (code == ApacheCommonsDateUtils.MODIFY_CEILING) {
    return ldt.plus(1, fieldName.getChronoUnit()).truncatedTo(fieldName.getChronoUnit());
  } else {
    throw new EPException("Round-half operation not supported for LocalDateTime");
  }
}

代码示例来源:origin: UniversaBlockchain/universa

@Test
public void checkRegisterContractExpiresAtDistantPastTime() throws Exception{
  Contract oldContract = Contract.fromDslFile(ROOT_PATH + "simple_root_contract.yml");
  oldContract.addSignerKeyFromFile(ROOT_PATH+"_xer0yfe2nn1xthc.private.unikey");
  oldContract.getDefinition().setExpiresAt(ZonedDateTime.of(LocalDateTime.MIN.truncatedTo(ChronoUnit.SECONDS), ZoneOffset.UTC));
  oldContract.seal();
  oldContract.check();
  oldContract.traceErrors();
  System.out.println("Contract is valid: " + oldContract.isOk());
  assertFalse(oldContract.isOk());
}

代码示例来源:origin: UniversaBlockchain/universa

@Test
public void checkContractExpiresAtDistantPastTime() throws Exception{
  Contract oldContract = Contract.fromDslFile(rootPath + "simple_root_contract.yml");
  oldContract.addSignerKeyFromFile(rootPath+"_xer0yfe2nn1xthc.private.unikey");
  oldContract.getDefinition().setExpiresAt(ZonedDateTime.of(LocalDateTime.MIN.truncatedTo(ChronoUnit.SECONDS), ZoneOffset.UTC));
  oldContract.seal();
  oldContract.check();
  oldContract.traceErrors();
  System.out.println("Contract is valid: " + oldContract.isOk());
  assertFalse(oldContract.isOk());
}

相关文章

LocalDateTime类方法