Example usage for java.time LocalTime from

List of usage examples for java.time LocalTime from

Introduction

In this page you can find the example usage for java.time LocalTime from.

Prototype

public static LocalTime from(TemporalAccessor temporal) 

Source Link

Document

Obtains an instance of LocalTime from a temporal object.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalTime l = LocalTime.from(LocalDateTime.now());

    System.out.println(l);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate february20th = LocalDate.of(2014, Month.FEBRUARY, 20);
    System.out.println(february20th);
    System.out.println(LocalDate.from(february20th.plus(15, ChronoUnit.YEARS))); // 2029-02-20
    System.out.println(LocalDate.MAX);
    System.out.println(LocalDate.MIN);

    System.out.println(LocalTime.MIDNIGHT); // 00:00
    System.out.println(LocalTime.NOON); // 12:00
    System.out.println(LocalTime.of(23, 12, 30, 500)); // 23:12:30.000000500
    System.out.println(LocalTime.now()); // 00:40:34.110
    System.out.println(LocalTime.ofSecondOfDay(11 * 60 * 60)); // 11:00
    System.out.println(LocalTime.from(LocalTime.MIDNIGHT.plusHours(4))); // 04:00
    System.out.println(LocalTime.MIN);
    System.out.println(LocalTime.MAX);

    System.out.println(LocalDateTime.of(2014, 2, 15, 12, 30, 50, 200)); // 2014-02-15T12:30:50.000000200
    System.out.println(LocalDateTime.now()); // 2014-02-28T17:28:21.002
    System.out.println(LocalDateTime.from(LocalDateTime.of(2014, 2, 15, 12, 30, 40, 500).plusHours(19))); // 2014-02-16T07:30:40.000000500
    System.out.println(LocalDateTime.MAX);
}

From source file:defaultmethods.SimpleTimeClient.java

public void setDate(int day, int month, int year) {
    LocalDate dateToSet = LocalDate.of(day, month, year);
    LocalTime currentTime = LocalTime.from(dateAndTime);
    dateAndTime = LocalDateTime.of(dateToSet, currentTime);
}

From source file:br.com.pontocontrol.controleponto.util.SwingUtils.java

public static LocalTime getLocalTimeValueFromField(JTextField field) {
    String value = field.getText();
    if (StringUtils.isNotBlank(value)) {
        try {// www . ja  v  a 2 s  .  c  o m
            return LocalTime.from(LOCAL_TIME_FORMATTER.parse(value));
        } catch (DateTimeParseException e) {
        }
    }
    return null;
}

From source file:org.thevortex.lighting.jinks.robot.Recurrence.java

/**
 * Determine if the target is within the boundaries of this event.
 *
 * @param target the target time/date/* w w w  . j a va 2s. c  o  m*/
 * @return {@code true} if the target is after the start time and within the duration; {@code false} if outside
 * the duration and/or there is no duration
 */
public boolean within(TemporalAccessor target) {
    if (duration == null)
        return false;

    LocalTime lt = LocalTime.from(target);
    return duration != null && lt.isAfter(startTime)
            && (Duration.between(startTime, lt).compareTo(duration) < 0);
}

From source file:msi.gama.util.GamaDate.java

public GamaDate(final IScope scope, final Temporal d) {
    final ZoneId zone;
    if (d instanceof ChronoZonedDateTime) {
        zone = ZonedDateTime.from(d).getZone();
    } else if (d.isSupported(ChronoField.OFFSET_SECONDS)) {
        zone = ZoneId.ofOffset("", ZoneOffset.ofTotalSeconds(d.get(ChronoField.OFFSET_SECONDS)));
    } else {//  w w w  . ja v  a 2  s.  c  o  m
        zone = GamaDateType.DEFAULT_ZONE;
    }
    if (!d.isSupported(MINUTE_OF_HOUR)) {
        internal = ZonedDateTime.of(LocalDate.from(d), LocalTime.of(0, 0), zone);
    } else if (!d.isSupported(DAY_OF_MONTH)) {
        internal = ZonedDateTime.of(LocalDate.from(
                scope == null ? Dates.DATES_STARTING_DATE.getValue() : scope.getSimulation().getStartingDate()),
                LocalTime.from(d), zone);
    } else {
        internal = d;
    }
}

From source file:msi.gama.util.GamaDate.java

private static Temporal parse(final IScope scope, final String original, final DateTimeFormatter df) {
    if (original == null || original.isEmpty() || original.equals("now")) {
        return LocalDateTime.now(GamaDateType.DEFAULT_ZONE);
    }//  w ww  . jav  a2s  . c o  m
    Temporal result = null;

    if (df != null) {
        try {
            final TemporalAccessor ta = df.parse(original);
            if (ta instanceof Temporal) {
                return (Temporal) ta;
            }
            if (!ta.isSupported(ChronoField.YEAR) && !ta.isSupported(ChronoField.MONTH_OF_YEAR)
                    && !ta.isSupported(ChronoField.DAY_OF_MONTH)) {
                if (ta.isSupported(ChronoField.HOUR_OF_DAY)) {
                    return LocalTime.from(ta);
                }
            }
            if (!ta.isSupported(ChronoField.HOUR_OF_DAY) && !ta.isSupported(ChronoField.MINUTE_OF_HOUR)
                    && !ta.isSupported(ChronoField.SECOND_OF_MINUTE)) {
                return LocalDate.from(ta);
            }
            return LocalDateTime.from(ta);
        } catch (final DateTimeParseException e) {
            e.printStackTrace();
        }
        GAMA.reportAndThrowIfNeeded(scope,
                GamaRuntimeException.warning(
                        "The date " + original + " can not correctly be parsed by the pattern provided", scope),
                false);
        return parse(scope, original, null);
    }

    String dateStr;
    try {
        // We first make sure all date fields have the correct length and
        // the string is correctly formatted
        String string = original;
        if (!original.contains("T") && original.contains(" ")) {
            string = StringUtils.replaceOnce(original, " ", "T");
        }
        final String[] base = string.split("T");
        final String[] date = base[0].split("-");
        String other;
        if (base.length == 1) {
            other = "00:00:00";
        } else {
            other = base[1];
        }
        String year, month, day;
        if (date.length == 1) {
            // ISO basic date format
            year = date[0].substring(0, 4);
            month = date[0].substring(4, 6);
            day = date[0].substring(6, 8);
        } else {
            year = date[0];
            month = date[1];
            day = date[2];
        }
        if (year.length() == 2) {
            year = "20" + year;
        }
        if (month.length() == 1) {
            month = '0' + month;
        }
        if (day.length() == 1) {
            day = '0' + day;
        }
        dateStr = year + "-" + month + "-" + day + "T" + other;
    } catch (final Exception e1) {
        throw GamaRuntimeException.error("The date " + original
                + " is not correctly formatted. Please refer to the ISO date/time format", scope);
    }

    try {
        result = LocalDateTime.parse(dateStr);
    } catch (final DateTimeParseException e) {
        try {
            result = OffsetDateTime.parse(dateStr);
        } catch (final DateTimeParseException e2) {
            try {
                result = ZonedDateTime.parse(dateStr);
            } catch (final DateTimeParseException e3) {
                throw GamaRuntimeException.error(
                        "The date " + original
                                + " is not correctly formatted. Please refer to the ISO date/time format",
                        scope);
            }
        }
    }

    return result;
}