java.time.LocalTime.get()方法的使用及代码示例

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

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

LocalTime.get介绍

[英]Gets the value of the specified field from this time as an int.

This queries this time for the value for the specified field. The returned value will always be within the valid range of values for the field. If it is not possible to return the value, because the field is not supported or for some other reason, an exception is thrown.

If the field is a ChronoField then the query is implemented here. The #isSupported(TemporalField) will return valid values based on this time, except NANO_OF_DAY and MICRO_OF_DAYwhich are too large to fit in an int and throw a DateTimeException. All other ChronoField instances will throw a DateTimeException.

If the field is not a ChronoField, then the result of this method is obtained by invoking TemporalField.getFrom(TemporalAccessor)passing this as the argument. Whether the value can be obtained, and what the value represents, is determined by the field.
[中]获取此时指定字段的int值。
这一次将查询指定字段的值。返回的值将始终在字段的有效值范围内。如果由于字段不受支持或其他原因而无法返回值,则会引发异常。
如果该字段是一个ChronoField,则在此处实现查询。#isSupported(TemporalField)将基于此时间返回有效值,但NANO_OF_DAY和MICRO_OF_DAY太大,无法放入int并引发DateTimeException。所有其他ChronoField实例将引发DateTimeException。
如果字段不是ChronoField,则通过调用TemporalField获得此方法的结果。getFrom(临时Accessor)将此作为参数传递。是否可以获得该值以及该值表示的内容由字段决定。

代码示例

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g,
    SerializerProvider provider) throws IOException
{
  g.writeNumber(value.getHour());
  g.writeNumber(value.getMinute());
  int secs = value.getSecond();
  int nanos = value.getNano();
  if ((secs > 0) || (nanos > 0))
  {
    g.writeNumber(secs);
    if (nanos > 0) {
      if (useNanoseconds(provider)) {
        g.writeNumber(nanos);
      } else {
        g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND));
      }
    }
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Test
public void testGetMinuteOfDay() {
  LocalTime now = LocalTime.now();
  assertEquals(now.get(ChronoField.MINUTE_OF_DAY), getMinuteOfDay(pack(now)), 0.0001);
}

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

LocalTime currTime = LocalTime.now();
this.hour   = currTime.get(ChronoField.HOUR_OF_AMPM);
this.minute = currTime.getMinute();
this.second = currTime.getSecond();

代码示例来源:origin: apache/wicket

@Override
public Integer getObject()
{
  LocalTime t = TimeField.this.getModelObject();
  if (t == null)
  {
    return null;
  }
  return use12HourFormat() ? t.get(ChronoField.CLOCK_HOUR_OF_AMPM) : t.getHour();
}

代码示例来源:origin: prestodb/presto

private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g,
    SerializerProvider provider) throws IOException
{
  g.writeNumber(value.getHour());
  g.writeNumber(value.getMinute());
  int secs = value.getSecond();
  int nanos = value.getNano();
  if ((secs > 0) || (nanos > 0))
  {
    g.writeNumber(secs);
    if (nanos > 0) {
      if (useNanoseconds(provider)) {
        g.writeNumber(nanos);
      } else {
        g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND));
      }
    }
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Test
public void testGetMillisecondOfDay() {
  LocalTime now = LocalTime.now();
  assertEquals(now.get(ChronoField.MILLI_OF_DAY), getMillisecondOfDay(pack(now)));
}

代码示例来源:origin: jtablesaw/tablesaw

@Test
public void testPack() {
  LocalTime time = LocalTime.now();
  int packed = pack(time);
  LocalTime t1 = asLocalTime(PackedLocalDateTime.time(packed));
  assertNotNull(t1);
  assertEquals(time.getHour(), t1.getHour());
  assertEquals(time.getMinute(), t1.getMinute());
  assertEquals(time.getSecond(), t1.getSecond());
  assertEquals(time.get(ChronoField.MILLI_OF_SECOND), t1.get(ChronoField.MILLI_OF_SECOND));
}

代码示例来源:origin: jtablesaw/tablesaw

@Test
public void testGetSecondOfDay() {
  LocalTime now = LocalTime.now();
  assertEquals(now.get(ChronoField.SECOND_OF_DAY), getSecondOfDay(pack(now)), 0.0001);
}

代码示例来源:origin: com.querydsl/querydsl-sql

protected static org.joda.time.LocalTime toJoda(LocalTime value) {
  return new org.joda.time.LocalTime(value.getHour(),
                    value.getMinute(),
                    value.getSecond(),
                    value.get(ChronoField.MILLI_OF_SECOND));
}

代码示例来源:origin: rakam-io/rakam

return jp.getValueAsDouble();
case TIME:
  return (long) LocalTime.parse(jp.getValueAsString()).get(ChronoField.MILLI_OF_DAY);
case DOUBLE:
  return jp.getValueAsDouble();

代码示例来源:origin: rakam-io/rakam

try {
  return (long) LocalTime.parse(jp.getValueAsString())
      .get(ChronoField.MILLI_OF_DAY);
} catch (Exception e) {
  LOGGER.warn(new RuntimeException(jp.getValueAsString(), e), "Error while parsing TIME field");

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

public FieldLocalTimeWrapper(final LocalTime date, final ChronoField field) {
 this.wrapped = (ignored) -> date.get(field);
 this.field = field;
 this.zone = ZoneId.systemDefault();
}

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

LocalTime time = LocalTime.of(8, 59, 32);
System.out.println(
 "Seconds since midnight: " 
 + time.get(ChronoField.SECOND_OF_DAY)); // 32372
System.out.println(
 "Seconds to midnight: " 
 + (86400 - time.get(ChronoField.SECOND_OF_DAY))); // 54028

代码示例来源:origin: com.querydsl/querydsl-sql

@Override
  public void setValue(PreparedStatement st, int startIndex, LocalTime value) throws SQLException {
    st.setTime(startIndex, new Time(value.get(ChronoField.MILLI_OF_DAY)), utc());
  }
}

代码示例来源:origin: apache/wicket

@Override
public AM_PM getObject()
{
  LocalTime t = TimeField.this.getModelObject();
  int i = t == null ? 0 : t.get(ChronoField.AMPM_OF_DAY);
  return i == 0 ? AM_PM.AM : AM_PM.PM;
}

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

@Override
public boolean isBefore(final LocalTime other) {
 return wrapped.applyAsInt(zone) < other.get(field);
}

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

/**
 * Returns the value of the provided field for the ZoneOffset as if the ZoneOffset's
 * hours/minutes/seconds were reckoned as a LocalTime.
 */
private static int offsetFieldValue(ZoneOffset offset, TemporalField field) {
  int offsetSeconds = offset.getTotalSeconds();
  int value = LocalTime.ofSecondOfDay(Math.abs(offsetSeconds)).get(field);
  return offsetSeconds < 0 ? value * -1 : value;
}

代码示例来源:origin: com.healthmarketscience.jackcess/jackcess

@Override
 protected Value eval0(EvalContext ctx) {
  double dd = LocalTime.now(ctx.getZoneId())
   .get(ChronoField.MILLI_OF_DAY) / 1000d;
  return ValueSupport.toValue(dd);
 }
});

代码示例来源:origin: com.querydsl/querydsl-sql

@Test
public void set() throws SQLException {
  LocalTime value = LocalTime.now();
  Time time = new Time(value.get(ChronoField.MILLI_OF_DAY));
  PreparedStatement stmt = EasyMock.createNiceMock(PreparedStatement.class);
  stmt.setTime(1, time, UTC);
  EasyMock.replay(stmt);
  type.setValue(stmt, 1, value);
  EasyMock.verify(stmt);
}

代码示例来源:origin: com.querydsl/querydsl-sql

@Test
public void jodaSet() throws SQLException {
  LocalTime value = LocalTime.now();
  Time time = new Time(value.get(ChronoField.MILLI_OF_DAY));
  PreparedStatement stmt = EasyMock.createNiceMock(PreparedStatement.class);
  stmt.setTime(1, time, UTC);
  EasyMock.replay(stmt);
  new LocalTimeType().setValue(stmt, 1, toJoda(value));
  EasyMock.verify(stmt);
}

相关文章