DateFormatter transforms am/pm to a. m./p. m. in spanish

Multi tool use
Multi tool use


DateFormatter transforms am/pm to a. m./p. m. in spanish



I am parsing with DateFormatter "Thu Jun 28 14:25:00 GMT+03:00 2018", and it correctly outputs Jun 28, 2018 2:25:00 pm. However when the user switches to Spanish locale the formatter outputs jun. 28, 2018 2:25:00 p. m. Is this normal?



here is how I'm parsing the date


DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setDateFormatPattern("MMM dd, yyyy hh:mm:ss a");
mCalendar = new GregorianCalendar(mTimeZone);
mCalendar.setTime(date);
mSimpleDateFormat.applyPattern(pattern);
mSimpleDateFormat.setTimeZone(mTimeZone);

mSimpleDateFormat.format(mCalendar.getTime());





Apparently this is normal, I just googled for Spanish representation of AM/PM and found a bunch of articles about this. I think this is expected.. Interesting, indeed :)
– Gennadii Saprykin
Jun 29 at 8:21





@GennadiiSaprykin hello thanks for your comment, Could you post it as an answer mentioning at least an article so that I can mark it as accepted just for anyone that stumble upon this post in the future?
– Libathos
Jun 29 at 9:01





I don’t speak Spanish, but is AM and PM used in Spanish at all? Also what result would you have desired?
– Ole V.V.
Jun 29 at 9:15





2 Answers
2



It is the correct representation. For example, compare the Spanish AM/PM Strings and the Italian AM/PM Strings. You'll see that Spanish is p.m and Italian is PM.



Although you may want to refer to this Quora answer which states that Spanish times are always presented in a 24-hour format.



Για σου. I believe that vguzzi’s answer is correct. I would like to add two messages:


GregorianCalendar


Date


TimeZone


SimpleDateFormat


java.time



Code example:


DateTimeFormatter originalFormatter
= DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
DateTimeFormatter userFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String originalDateTimeString = "Thu Jun 28 14:25:00 GMT+03:00 2018";
ZonedDateTime dateTime = ZonedDateTime.parse(originalDateTimeString, originalFormatter);
String formattedDateTime = dateTime.format(userFormatter);
System.out.println(formattedDateTime);



When run in US locale this prints a result that resembles yours:



Jun 28, 2018, 2:25:00 PM



Already British locale is a bit different:



28 Jun 2018, 14:25:00



There’s English and English. And Java knows that Spanish doesn’t use AM and PM either:



28 jun. 2018 14:25:00



It was a surprise to me to learn that Greek does:



28 Ιουν 2018, 2:25:00 μ.μ.



The output examples are from the code running on my Java 9. The locale data on Android may differ giving slightly different output in some cases, but still well suited to the different local audiences.



Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.


java.time


org.threeten.bp


java.time


java.time


java.time






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

PAOXUcNYJMvXsz4myvYSpMx7j5oyD,trPUYH2T3R3sfhGqg9I2
xQPi5xyHkbt,6K,mEzegvwBN1NapD a77SEKp,dgCHRKM,Iym vlM AA9XPl,OagZcDVSyQlUY,VSl BL

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters