7

I want to print a time without seconds in the default format for the locale. So I get the formatter with getTimeInstance() or getTimeInstance(int style). But even when I use a SHORT style it will contain the seconds part of the time as well.

Is there any way (apart from creating my own format, which then would not be the locale default and manually maintained) I can grab the default and split off the seconds ?

Thanks

Roman

roetzi
  • 974
  • 7
  • 10

3 Answers3

21

DateFormat.getTimeInstance(DateFormat.SHORT) works perfectly fine here: from 20:00:00 to 20:00 and from 8:00:00 PM to 8:00 PM.

Guillaume
  • 226
  • 2
  • 3
  • reading this, I wonder if I haven't tried this (or what I did wrong); of course this works, in plain Java (7 at least) and Android. Thanks for making me revisit this – roetzi Dec 21 '14 at 12:17
1

EDIT: This is insufficient (as stated by the first comment below). I'm keeping this here for the sake of history and to keep others from responding in a similar fashion :)


Have you considered saving the current format as a string and manually removing the seconds using String's substring method?

loeschg
  • 29,961
  • 26
  • 97
  • 150
  • 3
    This is not sufficient. One locale could use `HH:mm:ss`, and you would then like to make it `HH:mm` (and thus remove the : separator), whereas other locales could use `HH 'h' m 'min.' s 'sec.'` and you would like to make is `HH 'h' m 'min.'`. What would you remove? – JB Nizet Jul 26 '13 at 16:47
  • Ah, totally misunderstood the question. Deleting :) – loeschg Jul 26 '13 at 16:58
  • have a look at Joda Date Time API , it is much more simple and efficient that JDK Date and Time classes - http://joda-time.sourceforge.net/ – saurav Jul 26 '13 at 17:01
  • 1
    @tech-idiot +1 for JodaTime. Be aware of this when using with Android (http://stackoverflow.com/questions/5059663/android-java-joda-date-is-slow) – loeschg Jul 26 '13 at 17:03
  • thanks for the pointer to Joda Date Time, but just for formatting this seems to be an overkill; also I need this for android (only mentioned in the tags) so with the Android issues mentioned Joda is probably not an option. good pointer, though – roetzi Jul 27 '13 at 10:11
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161