Example usage for java.time LocalTime getHour

List of usage examples for java.time LocalTime getHour

Introduction

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

Prototype

public int getHour() 

Source Link

Document

Gets the hour-of-day field.

Usage

From source file:Main.java

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

    System.out.println(l.getHour());
}

From source file:Main.java

public static void main(String[] args) {
    LocalTime time = LocalTime.of(15, 30, 23, 234); // 15:30:00
    System.out.println(time.getHour()); // 15
    System.out.println(time.getMinute()); // 30
    System.out.println(time.getSecond()); // 23
    System.out.println(time.getNano()); // 234
    System.out.println(time.toSecondOfDay()); // 55823
    System.out.println(time.toNanoOfDay()); // 55823000000234
}

From source file:com.haulmont.cuba.web.widgets.CubaTimeField.java

protected LocalTime applyResolutionToValue(LocalTime value) {
    if (value == null) {
        return null;
    }/*from  ww w .  ja v  a2  s  .c  o  m*/

    LocalTime result = LocalTime.MIDNIGHT;
    List<TimeResolution> resolutions = getResolutionsHigherOrEqualTo(getResolution())
            .collect(Collectors.toList());

    for (TimeResolution resolution : resolutions) {
        switch (resolution) {
        case HOUR:
            result = result.withHour(value.getHour());
            break;
        case MINUTE:
            result = result.withMinute(value.getMinute());
            break;
        case SECOND:
            result = result.withSecond(value.getSecond());
            break;
        default:
            throw new IllegalArgumentException("Cannot detect resolution type");
        }
    }

    return result;
}

From source file:fll.scheduler.TournamentSchedule.java

/**
 * Parse a time from a schedule. This method allows only hours and minutes to
 * be//from   w w w . j a va 2 s . c  om
 * 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;
            }
        }
    }
}

From source file:nu.yona.server.analysis.service.ActivityServiceTest.java

@Test
public void getUserDayActivityDetail_activityPresent_resultWithActivity() {
    ZonedDateTime today = getDayStartTime(ZonedDateTime.now(userAnonZone));
    ZonedDateTime yesterday = today.minusDays(1);

    LocalTime activityStartTimeOnDay = LocalTime.parse("20:14:57");
    LocalTime activityEndTimeOnDay = LocalTime.parse("20:21:00");

    int hour = 20;
    int[] expectedSpread = getEmptySpread();
    expectedSpread[hour * 4] = 1;/* w ww  . j a v a  2s. c om*/
    expectedSpread[hour * 4 + 1] = 6;

    // gambling goal was created 2 weeks ago, see above
    // mock some activity on yesterday
    DayActivity yesterdayRecordedActivity = DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, yesterday.toLocalDate());
    ZonedDateTime activityStartTime = yesterday.withHour(activityStartTimeOnDay.getHour())
            .withMinute(activityStartTimeOnDay.getMinute()).withSecond(activityStartTimeOnDay.getSecond());
    ZonedDateTime activityEndTime = yesterday.withHour(activityEndTimeOnDay.getHour())
            .withMinute(activityEndTimeOnDay.getMinute()).withSecond(activityEndTimeOnDay.getSecond());
    Activity recordedActivity = Activity.createInstance(userAnonZone, activityStartTime.toLocalDateTime(),
            activityEndTime.toLocalDateTime(), Optional.empty());
    yesterdayRecordedActivity.addActivity(recordedActivity);
    when(mockDayActivityRepository.findOne(userAnonId, yesterday.toLocalDate(), gamblingGoal.getId()))
            .thenReturn(yesterdayRecordedActivity);

    DayActivityDto activityDay = service.getUserDayActivityDetail(userId, yesterday.toLocalDate(),
            gamblingGoal.getId());

    verify(mockDayActivityRepository, times(1)).findOne(userAnonId, yesterday.toLocalDate(),
            gamblingGoal.getId());
    assertThat(activityDay.getSpread(), equalTo(Arrays.asList(ArrayUtils.toObject((expectedSpread)))));
}

From source file:org.dozer.converters.LocalTimeConverter.java

@Override
public Object convert(Class destClass, Object srcObj) {
    LocalTime convertedValue = null;
    try {/*from   w  w w. j  a v a  2s.  c om*/
        if (srcObj instanceof String) {
            convertedValue = LocalTime.parse((String) srcObj);
        } else {
            LocalTime srcObject = (LocalTime) srcObj;
            convertedValue = LocalTime.of(srcObject.getHour(), srcObject.getMinute(), srcObject.getSecond(),
                    srcObject.getNano());
        }
    } catch (Exception e) {
        MappingUtils.throwMappingException("Cannot convert [" + srcObj + "] to LocalTime.", e);
    }
    return convertedValue;
}

From source file:org.openmrs.module.operationtheater.api.impl.OperationTheaterServiceImpl.java

@Override
public Interval getLocationAvailableTime(Location location, LocalDate date) {
    Date date1 = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
    Date date2 = Date.from(date.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
    List<AppointmentBlock> blocks = appointmentService.getAppointmentBlocks(date1, date2,
            location.getId() + ",", null, null);
    //      List<AppointmentBlock> blocks = new ArrayList<AppointmentBlock>();

    if (blocks.size() == 1) {
        //         return new Interval(new DateTime(blocks.get(0).getStartDate()), new DateTime(blocks.get(0).getEndDate()));
        Instant startInstant = LocalDateTime.from(blocks.get(0).getStartDate().toInstant())
                .toInstant(ZoneOffset.UTC);
        Instant endInstant = LocalDateTime.from(blocks.get(0).getEndDate().toInstant())
                .toInstant(ZoneOffset.UTC);
        return Interval.of(startInstant, endInstant);
    } else if (blocks.size() > 1) {
        throw new APIException("There shouldn't be multiple appointment blocks per location and date");
    }/*w ww.j a  v a2s.  c o  m*/

    DateTimeFormatter timeFormatter = OTMetadata.AVAILABLE_TIME_FORMATTER;
    LocalDateTime availableStart = null;
    LocalDateTime availableEnd = null;
    for (LocationAttribute attribute : location.getAttributes()) {
        if (attribute.getAttributeType().getUuid().equals(OTMetadata.DEFAULT_AVAILABLE_TIME_BEGIN_UUID)) {
            LocalTime beginTime = LocalTime.parse((String) attribute.getValue(), timeFormatter);
            //            availableStart = date.withTime(beginTime.getHourOfDay(), beginTime.getMinuteOfHour(), 0, 0);
            availableStart = date.atTime(beginTime.getHour(), beginTime.getMinute());
        } else if (attribute.getAttributeType().getUuid().equals(OTMetadata.DEFAULT_AVAILABLE_TIME_END_UUID)) {
            LocalTime endTime = LocalTime.parse((String) attribute.getValue(), timeFormatter);
            //            availableEnd = date.withTime(endTime.getHourOfDay(), endTime.getMinuteOfHour(), 0, 0);
            availableEnd = date.atTime(endTime.getHour(), endTime.getMinute());
        }
    }

    if (availableStart != null && availableEnd != null) {
        return Interval.of(availableStart.toInstant(ZoneOffset.UTC), availableEnd.toInstant(ZoneOffset.UTC));
    }

    throw new APIException("Available times not defined. please make sure that the attributes "
            + "'default available time begin' and 'default available time end' for the location "
            + location.getName() + " are defined");

}