简体   繁体   中英

Date Format with Locale

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?

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

    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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM