java.time.Year.from()方法的使用及代码示例

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

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

Year.from介绍

[英]Obtains an instance of Year from a temporal object.

A TemporalAccessor represents some form of date and time information. This factory converts the arbitrary temporal object to an instance of Year.

The conversion extracts the ChronoField#YEAR field. The extraction is only permitted if the temporal object has an ISO chronology, or can be converted to a LocalDate.

This method matches the signature of the functional interface TemporalQueryallowing it to be used in queries via method reference, Year::from.
[中]从时间对象获取年份的实例。
临时助理代表某种形式的日期和时间信息。该工厂将任意时态对象转换为年份实例。
转换将提取ChronoField#YEAR字段。仅当时态对象具有ISO年表或可以转换为LocalDate时,才允许提取。
此方法匹配函数接口TemporalQueryLow的签名,通过方法引用Year::from将其用于查询。

代码示例

代码示例来源:origin: com.thoughtworks.xstream/xstream

final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
return GregorianCalendar.from(y.atMonthDay(md).atStartOfDay(ZoneId.systemDefault()));

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

@Override
  public Year queryFrom(TemporalAccessor temporal) {
    return Year.from(temporal);
  }
};

代码示例来源:origin: EvoSuite/evosuite

public static Year from(TemporalAccessor temporal) {
  return Year.from(temporal);
}

代码示例来源:origin: org.simpleflatmapper/sfm-converter

@Override
  public Year convert(Object o, Context context) throws Exception {
    if (o == null) {
      return null;
    }

    if (o instanceof Date) {
      final ZonedDateTime dateTime = Instant.ofEpochMilli(((Date) o).getTime()).atZone(zone);
      return Year.of(dateTime.getYear());
    }

    if (o instanceof Integer || o instanceof Long) {
      return Year.of(((Number)o).intValue());
    }

    if (o instanceof TemporalAccessor) {
      return Year.from((TemporalAccessor) o);
    }

    throw new IllegalArgumentException("Cannot convert " + o + " to Year");
  }
}

代码示例来源:origin: arnaudroger/SimpleFlatMapper

@Override
  public Year convert(Object o, Context context) throws Exception {
    if (o == null) {
      return null;
    }

    if (o instanceof Date) {
      final ZonedDateTime dateTime = Instant.ofEpochMilli(((Date) o).getTime()).atZone(zone);
      return Year.of(dateTime.getYear());
    }

    if (o instanceof Integer || o instanceof Long) {
      return Year.of(((Number)o).intValue());
    }

    if (o instanceof TemporalAccessor) {
      return Year.from((TemporalAccessor) o);
    }

    throw new IllegalArgumentException("Cannot convert " + o + " to Year");
  }
}

代码示例来源:origin: org.mycore.mir/mir-module

private static String getFormatedDateString(String date, DateTimeFormatter formatter) {
    TemporalAccessor ta = formatter.parseBest(date,
      LocalDateTime::from, LocalDate::from, YearMonth::from, Year::from);
    if (ta instanceof LocalDateTime) {
      LocalDateTime ld = LocalDateTime.from(ta);
      return ld.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
    }
    if (ta instanceof LocalDate) {
      LocalDate ld = LocalDate.from(ta);
      return ld.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
    if (ta instanceof YearMonth) {
      YearMonth ld = YearMonth.from(ta);
      return ld.format(DateTimeFormatter.ofPattern("yyyy-MM"));
    }
    if (ta instanceof Year) {
      Year ld = Year.from(ta);
      return ld.format(DateTimeFormatter.ofPattern("yyyy"));
    }
    return date;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public PastDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();
    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();
    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public FutureDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();
    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();
    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: zsoltherpai/fluent-jdbc

private static void javaTimeTypes(Map<Class, ObjectMapperRsExtractor<?>> exs) {
  reg(exs, LocalDate.class, (rs, i) -> {
    Date date = rs.getDate(i);
    return date != null ? date.toLocalDate() : null;
  });
  reg(exs, LocalDateTime.class, (rs, i) -> {
    Timestamp stamp = rs.getTimestamp(i);
    return stamp != null ? stamp.toLocalDateTime() : null;
  });
  reg(exs, LocalTime.class, (rs, i) -> {
    Time time = rs.getTime(i);
    return time != null ? time.toLocalTime() : null;
  });
  reg(exs, Year.class, (rs, i) -> {
    Date date = rs.getDate(i);
    return date != null ? Year.from(date.toLocalDate()) : null;
  });
  reg(exs, YearMonth.class, (rs, i) -> {
    Date date = rs.getDate(i);
    return date != null ? YearMonth.from(date.toLocalDate()) : null;
  });
  reg(exs, Instant.class, (rs, i) -> {
    Timestamp stamp = rs.getTimestamp(i);
    return stamp != null ? stamp.toInstant() : null;
  });
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public PastDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();

    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();

    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public FutureOrPresentDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();

    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();

    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public PastOrPresentDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();

    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();

    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public PastOrPresentDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();
    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();
    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

private FutureOrPresentDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();
    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();
    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests

public FutureDummyEntity(ZonedDateTime dateTime) {
    calendar = GregorianCalendar.from( dateTime );
    date = calendar.getTime();

    instant = dateTime.toInstant();
    localDateTime = dateTime.toLocalDateTime();

    hijrahDate = HijrahDate.from( dateTime );
    japaneseDate = JapaneseDate.from( dateTime );
    localDate = LocalDate.from( dateTime );
    minguoDate = MinguoDate.from( dateTime );
    offsetDateTime = dateTime.toOffsetDateTime();
    thaiBuddhistDate = ThaiBuddhistDate.from( dateTime );
    year = Year.from( dateTime );
    yearMonth = YearMonth.from( dateTime );
    zonedDateTime = dateTime;
  }
}

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

ldt =  YearMonth.from(ta).atDay(1);
} else if (ta.isSupported(ChronoField.YEAR)) {
  ldt = Year.from(ta).atDay(1);

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

Year end = Year.from(endExclusive);
if (unit instanceof ChronoUnit) {
  long yearsUntil = ((long) end.year) - year;  // no overflow

代码示例来源:origin: x-stream/xstream

final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
return GregorianCalendar.from(y.atMonthDay(md).atStartOfDay(ZoneId.systemDefault()));

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

final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
final OffsetTime ot = OffsetTime.from(ta);
final Year y = Year.from(ta);
final MonthDay md = MonthDay.from(ta);
return GregorianCalendar.from(y.atMonthDay(md).atStartOfDay(ZoneId.systemDefault()));

代码示例来源:origin: arnaudroger/SimpleFlatMapper

@Test
public void testObjectToYear() throws Exception {
  ZoneId zoneId = ZoneId.systemDefault();
  Date now = new Date();
  Year year = Year.from(now.toInstant().atZone(zoneId));
  testObjectToYear(null, null);
  testObjectToYear(year, year);
  testObjectToYear(year.atMonth(6).atEndOfMonth().atTime(1, 0).atZone(zoneId), year);
  testObjectToYear(now, year);
  testObjectToYear(year.getValue(), year);
  try {
    testObjectToYear("a string", year);
    fail();
  } catch (IllegalArgumentException e) {
    // expected
  }
}

相关文章