下面列出了java.time.LocalTime#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static TemporalAccessor getTemporalAccessor(Temporal temporal, Class<?> targetType) {
if (temporal.getClass().equals(targetType)) {
return temporal;
}
if (Year.class.equals(targetType)) {
return Year.from(temporal);
} else if (YearMonth.class.equals(targetType)) {
return YearMonth.from(temporal);
} else if (LocalDate.class.equals(targetType)) {
return LocalDate.from(temporal);
} else if (LocalDateTime.class.equals(targetType)) {
return LocalDateTime.from(temporal);
} else if (ZonedDateTime.class.equals(targetType)){
return ZonedDateTime.from(temporal);
} else if (LocalTime.class.equals(targetType)) {
return LocalTime.from(temporal);
}
throw new IllegalArgumentException();
}
private static Temporal adjust(Temporal in, Set<DayOfWeek> dayOfWeeks, List<HoursRange> hoursRanges) {
DayOfWeek dayOfWeek = DayOfWeek.from(in);
LocalTime localTime = LocalTime.from(in);
boolean dayInRange = dayOfWeeks.contains(dayOfWeek);
boolean hourInRange = hoursRanges.stream().anyMatch(hr -> hr.includes(localTime));
if(dayInRange && hourInRange) {
return in;
}
Temporal result = in;
if(!dayInRange) {
do {
result = result.plus(1, ChronoUnit.DAYS);
} while(!dayOfWeeks.contains(DayOfWeek.from(result)));
}
if(!hourInRange) {
OptionalInt distance = hoursRanges.stream()
.mapToInt(hr -> hr.getDistanceInHours(localTime))
.sorted()
.findFirst();
result = result.plus(distance.orElseThrow(IllegalStateException::new), ChronoUnit.HOURS);
}
return result;
}
@Override
public LocalTime unmarshal( String localTimeString ) throws Exception {
if ( localTimeString == null ) {
return null;
}
try {
return LocalTime.from( formatter.parse( localTimeString ) );
} catch ( DateTimeException e ) {
throw new IllegalStateException( "Failed to convert string (" + localTimeString + ") to type ("
+ LocalTime.class.getName() + ")." );
}
}
static Value stringToDateValue(LocaleContext ctx, String valStr) {
// see if we can coerce to date/time
TemporalConfig.Type valTempType = ExpressionTokenizer.determineDateType(
valStr, ctx);
if(valTempType != null) {
DateTimeFormatter parseDf = ctx.createDateFormatter(
ctx.getTemporalConfig().getDateTimeFormat(valTempType));
try {
TemporalAccessor parsedInfo = parseDf.parse(valStr);
LocalDate ld = ColumnImpl.BASE_LD;
if(valTempType.includesDate()) {
// the year may not be explicitly specified
if(parsedInfo.isSupported(ChronoField.YEAR)) {
ld = LocalDate.from(parsedInfo);
} else {
ld = MonthDay.from(parsedInfo).atYear(
Year.now(ctx.getZoneId()).getValue());
}
}
LocalTime lt = ColumnImpl.BASE_LT;
if(valTempType.includesTime()) {
lt = LocalTime.from(parsedInfo);
}
return ValueSupport.toValue(LocalDateTime.of(ld, lt));
} catch(DateTimeException de) {
// note a valid date/time
}
}
// not a valid date string, not a date/time
return null;
}
/**
* Copy constructor.
*
* @param entity Entity to copy from.
*/
EntityWithDateTimeFields(EntityWithDateTimeFields entity) {
id = entity.id;
locTime = LocalTime.from(entity.locTime);
locDate = LocalDate.from(entity.locDate);
locDateTime = LocalDateTime.from(entity.locDateTime);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expected=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
/**
* @return the time point of the block was created as observed in local time
*/
@Override
public LocalTime getLocalCreateTime() {
return LocalTime.from(Instant.ofEpochSecond(Integer.toUnsignedLong(createTime)).atZone(ZoneId.of("Z")));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}
@Test(expectedExceptions=NullPointerException.class)
public void factory_from_TemporalAccessor_null() {
LocalTime.from((TemporalAccessor) null);
}
@Test(expectedExceptions=DateTimeException.class)
public void factory_from_TemporalAccessor_invalid_noDerive() {
LocalTime.from(LocalDate.of(2007, 7, 15));
}