How to remove the SECONDS field from a DateFormat
Asked Answered
U

3

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

Underpay answered 26/7, 2013 at 16:32 Comment(0)
H
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.

Haggle answered 13/12, 2014 at 16:25 Comment(1)
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 thisUnderpay
W
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?

Windy answered 26/7, 2013 at 16:44 Comment(5)
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?Sketch
Ah, totally misunderstood the question. Deleting :)Windy
have a look at Joda Date Time API , it is much more simple and efficient that JDK Date and Time classes - joda-time.sourceforge.netGarald
@tech-idiot +1 for JodaTime. Be aware of this when using with Android (#5060163)Windy
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, thoughUnderpay
S
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.

Sanctus answered 27/6, 2017 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.