In case someone is reading this and either uses Java 8 or later or is fine with a (good and futureproof) external library:
DateTimeFormatter noSeconds = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.ITALY);
LocalTime time = LocalTime.now(ZoneId.systemDefault());
System.out.println(time.format(noSeconds));
This just printed:
15.26
Please substitute your desired locale instead of Locale.ITALY
. Use Locale.getDefault()
for your JVM’s locale setting. I believe it prints without seconds in all locales.
In the code I have used a LocalTime
object, but the same code works for many other date and time classes including LocalDateTime
, OffsetDateTime
, OffsetTime
and ZonedDateTime
.
To use DateTimeFormatter
and any of the other classes mentioned on Android you need the ThreeTenABP. More details on how to in this question: How to use ThreeTenABP in Android Project. For any non-Android Java 6 or 7, use ThreeTen Backport.