Example usage for java.time LocalTime plusHours

List of usage examples for java.time LocalTime plusHours

Introduction

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

Prototype

public LocalTime plusHours(long hoursToAdd) 

Source Link

Document

Returns a copy of this LocalTime with the specified number of hours added.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalTime l = LocalTime.now();
    LocalTime s = l.plusHours(14);
    System.out.println(s);/*from  w  ww . j  a v a  2s  . co m*/
}

From source file:fll.scheduler.TournamentSchedule.java

/**
 * Parse a time from a schedule. This method allows only hours and minutes to
 * be//from w ww  .  j av a 2 s.c  o m
 * specified and still have times in the afternoon. If there is any time that
 * has an hour before {@link #EARLIEST_HOUR} the time is assumed to be in the
 * afternoon.
 * 
 * @param str a string representing a schedule time, if null the return value
 *          is null, if empty the return value is null
 * @return the {@link LocalTime} object for the string
 * @throws DateTimeParseException if the string could not be parsed as a time
 *           for a schedule
 */
public static LocalTime parseTime(final String str) throws DateTimeParseException {
    if (null == str || str.trim().isEmpty()) {
        return null;
    }

    try {
        // first try with the generic parser
        final LocalTime time = LocalTime.parse(str);
        return time;
    } catch (final DateTimeParseException e) {
        // try with seconds and AM/PM
        try {
            final LocalTime time = LocalTime.parse(str, TIME_FORMAT_AM_PM_SS);
            return time;
        } catch (final DateTimeParseException ampme) {
            // then try with 24-hour clock
            final LocalTime time = LocalTime.parse(str, INPUT_TIME_FORMAT);
            if (time.getHour() < EARLIEST_HOUR) {
                // no time should be this early, it must be the afternoon.
                return time.plusHours(12);
            } else {
                return time;
            }
        }
    }
}