问题
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?
回答1:
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
回答2:
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
- Oracle tutorial: Date Time, explaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
回答3:
Finally I found the answer.. :) Thanks Ole V.V.
Here it is : Java DateFormat.SHORT in Android not working as expected
来源:https://stackoverflow.com/questions/48398572/date-format-with-locale