Java Code Examples for java.time.LocalDateTime#from()

The following examples show how to use java.time.LocalDateTime#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FHIRPathUtil.java    From FHIR with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: TimestampType.java    From crate with Apache License 2.0 6 votes vote down vote up
static long parseTimestampIgnoreTimeZone(String timestamp) {
    try {
        return Long.parseLong(timestamp);
    } catch (NumberFormatException e) {
        TemporalAccessor dt;
        try {
            dt = TIMESTAMP_PARSER.parseBest(
                timestamp, LocalDateTime::from, LocalDate::from);
        } catch (DateTimeParseException e1) {
            throw new IllegalArgumentException(e1.getMessage());
        }

        if (dt instanceof LocalDate) {
            LocalDate localDate = LocalDate.from(dt);
            return localDate.atStartOfDay(UTC).toInstant().toEpochMilli();
        }

        LocalDateTime localDateTime = LocalDateTime.from(dt);
        return localDateTime.toInstant(UTC).toEpochMilli();
    }
}
 
Example 3
Source File: TimestampType.java    From crate with Apache License 2.0 6 votes vote down vote up
static long parseTimestamp(String timestamp) {
    try {
        return Long.parseLong(timestamp);
    } catch (NumberFormatException e) {
        TemporalAccessor dt;
        try {
            dt = TIMESTAMP_PARSER.parseBest(
                timestamp, OffsetDateTime::from, LocalDateTime::from, LocalDate::from);
        } catch (DateTimeParseException e1) {
            throw new IllegalArgumentException(e1.getMessage());
        }

        if (dt instanceof LocalDateTime) {
            LocalDateTime localDateTime = LocalDateTime.from(dt);
            return localDateTime.toInstant(UTC).toEpochMilli();
        } else if (dt instanceof LocalDate) {
            LocalDate localDate = LocalDate.from(dt);
            return localDate.atStartOfDay(UTC).toInstant().toEpochMilli();
        }

        OffsetDateTime offsetDateTime = OffsetDateTime.from(dt);
        return offsetDateTime.toInstant().toEpochMilli();
    }
}
 
Example 4
Source File: ChronoZonedDateTimeImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 5
Source File: ChronoZonedDateTimeImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 6
Source File: LocalDateTimeOf.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Parses the date using the formatter to create
 *  {@link LocalDateTime} instances.
 * @param date The date to parse.
 * @param formatter The formatter to use.
 */
public LocalDateTimeOf(final CharSequence date,
    final DateTimeFormatter formatter) {
    this.parsed = new Unchecked<>(
        () -> LocalDateTime.from(formatter.parse(date))
    );
}
 
Example 7
Source File: SimulationResult.java    From cucumber-performance with MIT License 5 votes vote down vote up
public SimulationResult(SimulationResult result) {
	super(result.getName(), new Result(result.getResult().getStatus(),result.getResultDuration(),result.getResult().getError()) ,LocalDateTime.from(result.getStart()), LocalDateTime.from(result.getStop()));

	for (GroupResult gr: result.getChildResults())
	{
		totalRan += gr.getChildResults().size();
		GroupResult ngr = new GroupResult(gr);
		childResults.add(ngr);
	}
	this.updateStatus(childResults);
}
 
Example 8
Source File: GroupResult.java    From cucumber-performance with MIT License 5 votes vote down vote up
public GroupResult(GroupResult result) {
	super(result.getName(), new Result(result.getResult().getStatus(),result.getResultDuration(),result.getResult().getError()) ,LocalDateTime.from(result.getStart()), LocalDateTime.from(result.getStop()));

	for (ScenarioResult sec: result.getChildResults())
	{
		ScenarioResult nsec = new ScenarioResult(sec);
		childResults.add(nsec);
	}
	this.updateStatus(childResults);
}
 
Example 9
Source File: TCKLocalDateTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    LocalDateTime.from(LocalTime.of(12, 30));
}
 
Example 10
Source File: TCKLocalDateTime.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    LocalDateTime.from((TemporalAccessor) null);
}
 
Example 11
Source File: TCKLocalDateTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_from_TemporalAccessor_invalid_noDerive() {
    LocalDateTime.from(LocalTime.of(12, 30));
}
 
Example 12
Source File: TCKLocalDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_from_TemporalAccessor_null() {
    LocalDateTime.from((TemporalAccessor) null);
}
 
Example 13
Source File: GamaDate.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public LocalDateTime getLocalDateTime() {
	return LocalDateTime.from(internal);
}
 
Example 14
Source File: DateUtils.java    From spring-cloud-shop with MIT License 3 votes vote down vote up
/**
 * 转换时间
 *
 * @param parseTime 时间内容
 */
public static Date parseDateTime(String parseTime) {

    LocalDateTime dateTime = LocalDateTime.from(DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN).parse(parseTime));

    return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
}
 
Example 15
Source File: IsoChronology.java    From JDKSourceCode1.8 with MIT License 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}
 
Example 16
Source File: IsoChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}
 
Example 17
Source File: IsoChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}
 
Example 18
Source File: IsoChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}
 
Example 19
Source File: IsoChronology.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}
 
Example 20
Source File: IsoChronology.java    From Java8CN with Apache License 2.0 2 votes vote down vote up
/**
 * Obtains an ISO local date-time from another date-time object.
 * <p>
 * This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
 *
 * @param temporal  the date-time object to convert, not null
 * @return the ISO local date-time, not null
 * @throws DateTimeException if unable to create the date-time
 */
@Override  // override with covariant return type
public LocalDateTime localDateTime(TemporalAccessor temporal) {
    return LocalDateTime.from(temporal);
}