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

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

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

LocalTime.withNano介绍

[英]Returns a copy of this LocalTime with the nano-of-second value altered.

This instance is immutable and unaffected by this method call.
[中]返回此LocalTime的副本,其中第二个值的nano已更改。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

@Test
public void testTruncateTime()
{
  LocalTime result = TIME;
  result = result.withNano(0);
  assertFunction("date_trunc('second', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withSecond(0);
  assertFunction("date_trunc('minute', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withMinute(0);
  assertFunction("date_trunc('hour', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
}

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

@SuppressWarnings("deprecation")
@Test
public void shouldReturnLocalDateTimeInstanceWhenConvertingSqlTimeToLocalDateTime() {
  LocalTime now = LocalTime.now();
  java.sql.Time time = new java.sql.Time(now.getHour(),now.getMinute(),now.getSecond()); // 0 nanos!
  assertThat(Conversions.toLocalDateTime(time)).isEqualTo(LocalDateTime.of(Conversions.EPOCH, now.withNano(0)));
}

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

@SuppressWarnings("deprecation")
@Test
public void shouldReturnLocalTimeInstanceWhenConvertingSqlTimeToLocalTime() {
  LocalTime now = LocalTime.now();
  java.sql.Time time = new java.sql.Time(now.getHour(),now.getMinute(),now.getSecond()); // 0 nanos!
  assertThat(Conversions.toLocalTime(time)).isEqualTo(now.withNano(0));
}

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

@SuppressWarnings("deprecation")
@Test
public void shouldReturnLocalTimeInstanceWhenConvertingUtilTimeToLocalTime() {
  LocalTime now = LocalTime.now();
  java.util.Date date = new java.util.Date(0,0,1,now.getHour(),now.getMinute(),now.getSecond()); // 0 nanos!
  assertThat(Conversions.toLocalTime(date)).isEqualTo(now.withNano(0));
}

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

@Test
public void testInsertReadLocalTime() {
  Event event = new Event();
  UUID id = UUID.randomUUID();
  LocalTime localTimeNow = LocalTime.now();
  event.setId(id);
  event.setLocalTime(localTimeNow);
  data.insert(event);
  event = data.findByKey(Event.class, id);
  Assert.assertEquals(localTimeNow.withNano(0), event.getLocalTime());
}

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

/**
 * Returns a copy of this {@code LocalDateTime} with the nano-of-second value altered.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
 * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
 * @throws DateTimeException if the nano value is invalid
 */
public LocalDateTime withNano(int nanoOfSecond) {
  LocalTime newTime = time.withNano(nanoOfSecond);
  return with(date, newTime);
}

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

/**
 * Returns a copy of this {@code OffsetTime} with the nano-of-second value altered.
 * <p>
 * The offset does not affect the calculation and will be the same in the result.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
 * @return an {@code OffsetTime} based on this time with the requested nanosecond, not null
 * @throws DateTimeException if the nanos value is invalid
 */
public OffsetTime withNano(int nanoOfSecond) {
  return with(time.withNano(nanoOfSecond), offset);
}

代码示例来源:origin: br.com.jarch/jarch-jsf

@Override
public LocalDateTime getAsObject(FacesContext arg0, UIComponent arg1, String value) {
  if (value == null) {
    return null;
  }
  try {
    return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
  } catch (Exception ex) {
    try {
      return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
    } catch (Exception ex2) {
      LocalDate localDate = LocalDate.parse(value, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
      LocalTime localTime = LocalTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
      return LocalDateTime.of(localDate, localTime);
    }
  }
}

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

public static LocalTime roundDownMinutes(LocalTime time, int step) {
  int mins = time.getMinute();
  time = time.withSecond(0).withNano(0);
  if (mins % step == 0) return time;
  if (60 % step != 0) throw new IllegalArgumentException("Invalid step: " + step);
  return time.withMinute(mins - (mins % step));
}

代码示例来源:origin: owlike/genson

private static LocalTime localTimeFromMillisOfDay(long millis){
    long seconds = DateTimeUtil.getSecondsFromMillis(millis);
    long nanos = DateTimeUtil.getNanosFromMillis(millis);
    return LocalTime.ofSecondOfDay(seconds).withNano((int) nanos);
  }
}

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

public static LocalTime roundUpMinutes(LocalTime time, int step) {
  int mins = time.getMinute();
  time = time.withSecond(0).withNano(0);
  if (mins % step == 0) return time;
  if (60 % step != 0) throw new IllegalArgumentException("Invalid step: " + step);
  mins = mins + step - (mins % step);
  return mins < 60 ? time.withMinute(mins) : time.plusHours(1).withMinute(mins - 60);
}

代码示例来源:origin: com.enonic.xp/core-api

@Override
public Value createDefaultValue( final Input input )
{
  final String defaultValue = input.getDefaultValue().getRootValue();
  if ( defaultValue != null )
  {
    try
    {
      return ValueFactory.newLocalTime( ValueTypes.LOCAL_TIME.convert( defaultValue ) );
    }
    catch ( ValueTypeException e )
    {
      final RelativeTime result = RelativeTimeParser.parse( defaultValue );
      if ( result != null )
      {
        final Instant instant = Instant.now().plus( result.getTime() );
        return ValueFactory.newLocalTime( instant.atZone( ZoneId.systemDefault() ).toLocalTime().withNano( 0 ) );
      }
      else
      {
        throw new IllegalArgumentException( "Invalid Date format: " + defaultValue );
      }
    }
  }
  return super.createDefaultValue( input );
}

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

f.checkValidValue(newValue);
switch (f) {
  case NANO_OF_SECOND: return withNano((int) newValue);
  case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
  case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
  case MICRO_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000);
  case MILLI_OF_SECOND: return withNano((int) newValue * 1000000);
  case MILLI_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000000);
  case SECOND_OF_MINUTE: return withSecond((int) newValue);

代码示例来源:origin: io.prestosql/presto-main

@Test
public void testTruncateTime()
{
  LocalTime result = TIME;
  result = result.withNano(0);
  assertFunction("date_trunc('second', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withSecond(0);
  assertFunction("date_trunc('minute', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withMinute(0);
  assertFunction("date_trunc('hour', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
}

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

@Test
public void testTruncateTime()
{
  LocalTime result = TIME;
  result = result.withNano(0);
  assertFunction("date_trunc('second', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withSecond(0);
  assertFunction("date_trunc('minute', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
  result = result.withMinute(0);
  assertFunction("date_trunc('hour', " + TIME_LITERAL + ")", TimeType.TIME, toTime(result));
}

相关文章