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

x33g5p2x  于2022-02-05 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(263)

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

ZonedDateTime.truncatedTo介绍

[英]Returns a copy of this ZonedDateTime 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 operates on the local time-line, LocalDateTime#truncatedTo(TemporalUnit)the underlying local date-time. This is then converted back to a ZonedDateTime, using the zone ID to obtain the offset.

When converting back to 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.

This instance is immutable and unaffected by this method call.
[中]返回此ZoneDateTime的副本,时间被截断。
截断返回原始日期时间的副本,其中字段小于指定的单位,设置为零。例如,使用ChronoUnit#MINUTES单位进行截断会将秒数和秒数字段设置为零。
该单元必须有一个临时单元#getDuration(),它可以划分为一个标准日的长度,没有余数。这包括ChronoUnit和ChronoUnit#天上提供的所有时间单位。其他单位会抛出一个例外。
它在本地时间线LocalDateTime#truncatedTo(TemporalUnit)基础本地日期时间上运行。然后将其转换回ZoneDateTime,使用分区ID获取偏移量。
当转换回ZoneDateTime时,如果本地日期时间重叠,那么如果可能,将保留偏移量,否则将使用较早的偏移量。如果存在间隙,本地日期时间将根据间隙的长度向前调整。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

switch (period) {
  case YEAR:
    zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS)
        .withDayOfYear(1)
        .plus(1, ChronoUnit.YEARS);
    break;
  case MONTH:
    zonedDateTime =  zonedDateTime.truncatedTo(ChronoUnit.DAYS)
        .withDayOfMonth(1)
        .plus(1,ChronoUnit.MONTHS);
    break;
  case WEEK:
    zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS)
        .with(TemporalAdjusters.next(WeekFields.of(Locale.getDefault()).getFirstDayOfWeek()));
    break;
  case DAY:
    zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS)
        .plus(1, ChronoUnit.DAYS);
    break;
  case HALF_DAY:
    ZonedDateTime halfDay = ZonedDateTime.from(zonedDateTime).truncatedTo(ChronoUnit.DAYS)
        .plus(1, ChronoUnit.HALF_DAYS);
    if ( zonedDateTime.isBefore(halfDay) ) {
    zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.HOURS)
        .plus(1, ChronoUnit.HOURS);
    break;
  case MINUTE:
    zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.MINUTES)

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

stopTime.trip_id = trip.trip_id;
final ZonedDateTime arrival_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
stopTime.arrival_time = (int) Duration.between(arrival_time.truncatedTo(ChronoUnit.DAYS), arrival_time).getSeconds();
final ZonedDateTime departure_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
stopTime.departure_time = (int) Duration.between(departure_time.truncatedTo(ChronoUnit.DAYS), departure_time).getSeconds();
stopTimes.add(stopTime);
logger.trace("Number of stop times: {}", stopTimes.size());

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

stopTime.trip_id = trip.trip_id;
  final ZonedDateTime arrival_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
  stopTime.arrival_time = (int) Duration.between(arrival_time.truncatedTo(ChronoUnit.DAYS), arrival_time).getSeconds();
  final ZonedDateTime departure_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
  stopTime.departure_time = (int) Duration.between(departure_time.truncatedTo(ChronoUnit.DAYS), departure_time).getSeconds();
  return stopTime;
})

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

LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = now.atZone(ZoneId.of("America/Denver"));
System.out.println("Pre-Truncate:  " + zoned);
DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
System.out.println("Post-Truncate: " + zoned.truncatedTo(ChronoUnit.SECONDS).format(dtf));

代码示例来源:origin: org.elasticsearch/elasticsearch

@SuppressForbidden(reason = "only exposing the method as a passthrough")
public ZonedDateTime truncatedTo(TemporalUnit unit) {
  return dt.truncatedTo(unit);
}

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

ZonedDateTime zdt = 
 ZonedDateTime.of(2015, 10, 18, 0, 30, 0, 0, 
  ZoneId.of("America/Sao_Paulo")); // switch to summer time
ZonedDateTime zdt1 = zdt.truncatedTo(ChronoUnit.DAYS);
ZonedDateTime zdt2 = zdt.toLocalDate().atStartOfDay(zdt.getZone());

System.out.println(zdt); // 2015-10-18T01:30-02:00[America/Sao_Paulo]
System.out.println(zdt1); // 2015-10-18T01:00-02:00[America/Sao_Paulo]
System.out.println(zdt2); // 2015-10-18T01:00-02:00[America/Sao_Paulo]

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

/**
 * Returns an {@link java.time.ZonedDateTime} with the time portion cleared.
 *
 * @param self a ZonedDateTime
 * @return a ZonedDateTime
 * @since 2.5.0
 */
public static ZonedDateTime clearTime(final ZonedDateTime self) {
  return self.truncatedTo(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 ZonedDateTime zdtAtStartOfDay(final ZonedDateTime date) {
  return date != null ? date.truncatedTo(ChronoUnit.DAYS) : null;
}

代码示例来源:origin: com.hp.autonomy.aci.content/content-parameter-api

private static String formatDate(final ZonedDateTime param) {
  // IDOL ISO date time does not have a year 0 whereas Java ISO date time does
  return DateTimeFormatter.ISO_INSTANT
    .format(param.truncatedTo(ChronoUnit.SECONDS));
}

代码示例来源:origin: com.hp.autonomy.aci.content/content-parameter-api

protected String formatElement(final ZonedDateTime range) {
    // IDOL ISO date time does not have a year 0 whereas Java ISO date time does
    return DateTimeFormatter.ISO_INSTANT
      .format(range.truncatedTo(ChronoUnit.SECONDS));
  }
}

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

@Override
public boolean isAfter(final ZonedDateTime other) {
  return wrapped.apply(zone).truncatedTo(accuracy).toInstant().isAfter(other.truncatedTo(accuracy).toInstant());
}

代码示例来源:origin: OpenNMS/opennms

public static String iso8601OffsetString(Date d, ZoneId zone, ChronoUnit truncateTo) {
  ZonedDateTime zdt = ((d).toInstant())
      .atZone(zone);
  if(truncateTo != null) {
    zdt = zdt.truncatedTo(truncateTo);
  }
  return zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

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

@Override
public boolean isBefore(final Date other) {
  return wrapped.apply(zone).truncatedTo(accuracy).toInstant().isBefore(other.toInstant().atZone(zone).truncatedTo(accuracy).toInstant());
}

代码示例来源:origin: com.testdroid/testdroid-api

public static Long atMidnight(LocalDateTime date) {
  return date
      .atZone(ZoneId.of("UTC"))
      .truncatedTo(ChronoUnit.DAYS)
      .toInstant().toEpochMilli();
}

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

@Override
public boolean isSame(final ZonedDateTime other) {
  return wrapped.apply(zone).truncatedTo(accuracy).toInstant().equals(other.truncatedTo(accuracy).toInstant());
}

代码示例来源:origin: sakaiproject/sakai

public ReportParams(String siteId){
  this.siteId = siteId;
  whatToolIds.add(ReportManager.WHAT_EVENTS_ALLTOOLS);
  // events are counted against a particular date using the server time zone, so initialize the date
  // range based on the current date in that time zone
  ZonedDateTime today = ZonedDateTime.now(ZoneId.systemDefault()).truncatedTo(ChronoUnit.DAYS);
  ZonedDateTime yesterday = today.minusDays(1);
  whenFrom = Date.from(yesterday.toInstant());
  whenTo = Date.from(today.toInstant());
}

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

public List<Binder> getPaymentStats(Ledger ledger, int daysNum) {
    List<Binder> result = new ArrayList<>();
    ZonedDateTime now = ZonedDateTime.now();
    Map<Integer, Integer> payments = ledger.getPayments(now.truncatedTo(ChronoUnit.DAYS).minusDays(daysNum));
    payments.keySet().forEach( day -> {
      result.add(Binder.of("date",ZonedDateTime.ofInstant(Instant.ofEpochSecond(day), ZoneId.systemDefault()).format(formatter), "units",payments.get(day)));
    });
    return result;
  }
}

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

private static String format(LocalDateTime datetime) {
 // We format using second resolutions in UTC
 return datetime.atZone(ZoneOffset.systemDefault())
   .withZoneSameInstant(ZoneOffset.UTC)
   .truncatedTo(ChronoUnit.SECONDS)
   .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)+"Z";
}

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

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

代码示例来源:origin: com.cronutils/cron-utils

private ExecutionTimeResult getNextPotentialDayOfMonth(final ZonedDateTime date,
                            final int lowestHour,
                            final int lowestMinute,
                            final int lowestSecond,
                            final TimeNode node) {
  final NearestValue nearestValue = node.getNextValue(date.getDayOfMonth(), 0);
  if (nearestValue.getShifts() > 0) {
    return new ExecutionTimeResult(date.truncatedTo(DAYS).withDayOfMonth(1).plusMonths(nearestValue.getShifts()), false);
  }
  return new ExecutionTimeResult(date.truncatedTo(SECONDS).withDayOfMonth(nearestValue.getValue())
      .with(LocalTime.of(lowestHour, lowestMinute, lowestSecond)), false);
}

相关文章

ZonedDateTime类方法