I want to display / print out time like
02:15PM
and be able to set the time just based on minutes(possibly larger than 59).
This doesn't seem to work:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mma");
String text = time.format(formatter);
LocalTime parsedTime = LocalTime.parse(text, formatter);
it shows time like 16:15
so its the wrong 12/24 format(and am/pm missing of course)
so far I set the time like this:
void setTime(int minutes) {
time = LocalTime.of(minutes / 60, minutes % 60);
}
is there a way to do it in a more elegant way (read: without dividing)?
I do not want to use Joda Time.
As discussed, use hh
instead of HH
for an AM/PM clock. The list of patterns is detailed in the javadoc. Example:
LocalTime time = LocalTime.of(18, 0);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mma");
System.out.println(fmt.format(time)); //06:00PM
As for creating a time from a number of minutes, you can use:
LocalTime.MIDNIGHT.plusMinutes(minutes);