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

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

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

ZonedDateTime.withYear介绍

[英]Returns a copy of this ZonedDateTime with the year value altered.

This operates on the local time-line, LocalDateTime#withYear(int) of the 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的副本,其中年份值已更改。
这在本地时间线上运行,即本地日期时间#与本地日期时间的年份(int)。然后将其转换回ZoneDateTime,使用分区ID获取偏移量。
当转换回ZoneDateTime时,如果本地日期时间重叠,那么如果可能,将保留偏移量,否则将使用较早的偏移量。如果存在间隙,本地日期时间将根据间隙的长度向前调整。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: blynkkk/blynk-server

private ZonedDateTime adjustToStartDate(ZonedDateTime zonedStartAt, ZonedDateTime zonedNow, ZoneId zoneId) {
  if (durationType == ReportDurationType.CUSTOM) {
    ZonedDateTime zonedStartDate = getZonedFromTs(startTs, zoneId).with(LocalTime.MIN);
    if (zonedStartDate.isAfter(zonedNow)) {
      zonedStartAt = zonedStartAt
          .withDayOfMonth(zonedStartDate.getDayOfMonth())
          .withMonth(zonedStartDate.getMonthValue())
          .withYear(zonedStartDate.getYear());
    }
  }
  return zonedStartAt;
}

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

public ZonedDateTime withYear(int value) {
  return dt.withYear(value);
}

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

public ZonedDateTime withYear(int value) {
  return dt.withYear(value);
}

代码示例来源:origin: apache/servicemix-bundles

public ZonedDateTime withYear(int value) {
  return dt.withYear(value);
}

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

/**
 * ZonedDateTime型からTime型に変換します
 * 
 * @param dateTime
 * @return カレンダー型
 */
private java.sql.Time toTime(final ZonedDateTime dateTime) {
  if (dateTime == null) {
    return null;
  }
  java.sql.Time time=new java.sql.Time(dateTime.withZoneSameInstant(AbstractJava8DateConverter.INSTANT_ZONE_ID)
      .withYear(1970).withMonth(1).withDayOfMonth(1).toInstant().toEpochMilli());
  return time;
}

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

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

代码示例来源:origin: com.guestful.module/guestful.module.jsr310-extensions

@Override
public Instant instant() {
  ZonedDateTime dt = ZonedDateTime.now(zone);
  if (year != null) dt = dt.withYear(year);
  if (month != null) dt = dt.withMonth(month);
  if (day != null) dt = dt.withDayOfMonth(day);
  if (hour != null) dt = dt.withHour(hour);
  if (min != null) dt = dt.withMinute(min);
  if (sec != null) dt = dt.withSecond(sec);
  return dt.toInstant();
}

代码示例来源:origin: com.impossibl.pgjdbc-ng/pgjdbc-ng

private static Object convertOutput(Context context, Type type, ZonedDateTime dateTime, Class<?> targetClass, Calendar targetCalendar) throws ConversionException {
 if (targetClass == OffsetDateTime.class) {
  return dateTime.toOffsetDateTime();
 }
 if (targetClass == String.class) {
  return context.getTimestampFormat().getPrinter().format(dateTime);
 }
 ZoneId targetZoneId = targetCalendar.getTimeZone().toZoneId();
 ZonedDateTime zonedDateTime = dateTime.withZoneSameInstant(targetZoneId);
 if (targetClass == Time.class) {
  return new Time(zonedDateTime.withYear(1970).withDayOfYear(1).toInstant().toEpochMilli());
 }
 if (targetClass == Date.class) {
  return new Date(zonedDateTime.truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli());
 }
 if (targetClass == Timestamp.class) {
  return Timestamp.from(zonedDateTime.toInstant());
 }
 throw new ConversionException(type, targetClass);
}

代码示例来源:origin: com.impossibl.pgjdbc-ng/pgjdbc-ng

private static Object convertOutput(Context context, Type type, LocalDateTime dateTime, Class<?> targetClass, Calendar targetCalendar) throws ConversionException {
 if (targetClass == LocalDateTime.class) {
  return dateTime;
 }
 if (targetClass == LocalDate.class) {
  return  dateTime.toLocalDate();
 }
 if (targetClass == String.class) {
  return context.getTimestampFormat().getPrinter().format(dateTime);
 }
 ZoneId targetZoneId = targetCalendar.getTimeZone().toZoneId();
 ZonedDateTime zonedDateTime = dateTime.atOffset(ZoneOffset.UTC).atZoneSimilarLocal(targetZoneId);
 if (targetClass == Time.class) {
  return new Time(zonedDateTime.withYear(1970).withDayOfYear(1).toInstant().toEpochMilli());
 }
 if (targetClass == Date.class) {
  return new Date(zonedDateTime.truncatedTo(ChronoUnit.DAYS).toInstant().toEpochMilli());
 }
 if (targetClass == Timestamp.class) {
  return Timestamp.from(zonedDateTime.toInstant());
 }
 throw new ConversionException(type, targetClass);
}

代码示例来源:origin: crawler-commons/crawler-commons

zdt = zdt.withYear(1997);
parseCompareDate(zdt, "1997", "yyyyMMdd");

相关文章

ZonedDateTime类方法