1

I want to display date according to each country's date format. I have tried many ways finally I found this example

http://www.java2s.com/Code/Java/Data-Type/DateFormatwithLocale.htm

Which gives the perfect output of what I meant. Some countries like Germany won't use 12hr formats instead they uses 24 hr formats with no AM/PM. While some countries like US uses 12hr formats.

But I found that while running this java class it returns the correct output as expected but while running this inside an Android project it returns something like this

I/System.out: Locale: en_US
I/System.out: Jan 23, 2018 5:26:41 AM
I/System.out: Jan 23, 2018 5:26:41 AM
I/System.out: Jan 23, 2018 5:26:41 AM
I/System.out: Jan 23, 2018 5:26:41 AM
I/System.out: Jan 23, 2018 5:26:41 AM
I/System.out: Locale: de_DE
I/System.out: 23.01.2018 5:26:41 vorm.
I/System.out: 23.01.2018 5:26:41 vorm.
I/System.out: 23.01.2018 5:26:41 vorm.
I/System.out: 23.01.2018 5:26:41 vorm.
I/System.out: 23.01.2018 5:26:41 vorm.

In case of Locale: en_US it is as expected but in case of Locale: de_DE it is expected not to have that “vorm.”.

Could anyone explain this behavior?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    5:26 AM is the same as 5:26 in 24h format, 5:26 PM would be 17:26 in 24h format – DGK Jan 23 '18 at 09:48
  • 1
    But I don't want that 'vorm' to get displayed. if in case of 5.30 pm it will display 17.30 nachm, I just need it to be 17.30 – Adhithya G Nair Jan 23 '18 at 10:05
  • "nachm." is German for PM, "vorm." for "AM". The behaviour is as expected! Take a look at this link, http://www.localeplanet.com/icu/de-DE/index.html – Sameer Khan Jan 23 '18 at 10:40
  • Not being a native German speaker I very much agree that *vorm.* for AM and *nach,.* for PM looks odd and very unGerman (how do you capitalize *Ungerman*?) I too would expect just 5:26:41 (or 17:26:41 if the time was in PM). – Ole V.V. Jan 23 '18 at 11:43
  • @OleV.V. Yeah you are correct.. – Adhithya G Nair Jan 23 '18 at 12:13
  • There is [an interesting answer to a duplicate question here](https://stackoverflow.com/a/48662449/5772882). – Ole V.V. Feb 07 '18 at 12:12
  • 1
    Oyye.. Thanks for the update @OleV.V. , Thats one is correct – Adhithya G Nair Feb 07 '18 at 12:53

3 Answers3

1
    LocalDateTime dateTime = LocalDateTime.of(2018, 1, 23, 5, 26, 41);
    DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(Locale.GERMAN);
    System.out.println(dateTime.format(formatter));

Prints

23.01.2018, 05:26:41

Not tested on Android, though, but I believe the result will be the same.

java.time

The Date and DateFormat classes used on the java2s page you are linking to are long outdated, and the latter in particular also notoriously troublesome. For this reason alone I recommend you stop using them and start using java.time, the modern Java date and time API, instead. It’s so much nicer to work with.

Can you do that on Android? You certainly can. I am told that java.time is built in on newer Android devices. For older devices, add ThreeTenABP to your project (see the links at the bottom) and make sure to import org.threeten.bp.LocalDateTime, org.threeten.bp.format.DateTimeFormatter and org.threeten.bp.format.FormatStyle. Then it will all work.

What went wrong?

Java picks up its locale information from different sources. It varies between desktop Java and Android Java, it may even vary between Android devices, and it varies between Java versions. Without any guarantee, I think that java.time is more stable in this respect than the old classes were.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • V.V , Hey it works perfectly even in android. But the problem is that it can only target to API 26 or more, that means only in Android O or higher. But my project's min API is 21. So I can't use this. – Adhithya G Nair Jan 24 '18 at 05:57
  • I think you can use ThreeTenABP on level 21. Am I mistaken? Not my home field, sorry… – Ole V.V. Jan 24 '18 at 06:37
0

This is native behaviour in java JDK.

Depends on the valid Locale you pass, JDK provide the time with formatted date.

Returns a new DateFormat instance which formats date with the given formatting style for the specified locale.

Parameters:
style the given formatting style. Either one of DateFormat.SHORT, 
DateFormat.MEDIUM, DateFormat.LONG, or DateFormat.FULL.
locale the desired locale.
Returns: a date formatter.
Throws: java.lang.IllegalArgumentException if style is invalid, or if locale isn't 
one of the locales returned from getAvailableLocales().
java.lang.NullPointerException if locale is null
See also: java.text.DateFormat.getDateInstance(int,java.util.Locale)

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/text/spi/DateFormatProvider.java#DateFormatProvider.getTimeInstance%28int%2Cjava.util.Locale%29

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Takermania
  • 1,345
  • 2
  • 12
  • 20
0

Finally I found the answer.. :) Thanks Ole V.V.

Here it is : Java DateFormat.SHORT in Android not working as expected