Как вы устанавливаете время и только время в календаре на Java?
Вопрос
Имея часы и минуты, есть ли более простой или лучший способ установить его в объект календаря, чем:
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
hour, minute);
Решение
Из здесь :
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Другие советы
Используйте набор (поле int, значение int) . Первый параметр - это номер поля для HOUR / MINUTE / SECOND.
In 2018 no one should use the Calendar
class anymore. It it long outmoded, and java.time
the modern Java date and time API is so much nicer to work with. Instead use, depending on you exact requirements, a ZonedDateTime
or perhaps an OffsetDateTime
or LocalDateTime
. In either case, set the time of day this way:
dateTime = dateTime.with(LocalTime.of(hour, minute));
Не связан с StackOverflow