I have an Asus Fonepad first generation(android 4.1.2) and an Asus Fonepad second generation(android 4.3).
I use the following code to parse the date to the desired format in my app:
SimpleDateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", /*new Locale("nl", "NL")*/Locale.GERMANY);
df.setTimeZone(TimeZone.getTimeZone(/*"Europe/Amsterdam"*/"Europe/Berlin"));
String time = df.format(new Date());
The above code results in "Dez 23 17:09:25 2013" on the first generation fonepad and "Dez. 23 17:09:25 2013" on the second generation fonepad.
As you can see, the second generation adds a dot after the month.
this causes a parsexception on the server side.
Why does SimpleDateFormat behave differently on different devices(android versions)? This is worrying.
Is there a way to always get the same format? What is the solution to this?
Thanks.
SimpleDateFormat (and some other framework classes) use icu4c library to format content. Month format for DE was changed between 49.2 and 50.1 versions of this library.
No, you can't expect same behavior for all android versions.
Link to sources: https://android.googlesource.com/platform/external/icu4c/+/android-4.4.2_r1/data/locales/de.txt
Add: If you sending data to a server than solution is to use only numbers: 12 will be always 12 for December.
Related
How one could get Chinese New Year Date on Android?
Since API level 24 Android has Chinese Calendar class.
https://developer.android.com/reference/android/icu/util/ChineseCalendar
However, doing it like this returns wrong date (Feb 12 for 2023).
val chinese = ChineseCalendar.getInstance()
chinese.set(ChineseCalendar.MONTH, 0)
chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)
I was able to get Gregorian date for Chinese new year in the following way. Getting Chinese calendar is done using simple instantiation ChineseCalendar(). No need to call getInstance().
val chinese = ChineseCalendar()
chinese.set(ChineseCalendar.MONTH, 0)
chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)
println("chinese " + chinese.time.toString())
In the logs I got
chinese Sun Jan 22 13:24:41 GMT+02:00 2023
You can also add year to get next new year date, like this
chinese.add(ChineseCalendar.YEAR, 1)
and get
chinese Sat Feb 10 13:27:41 GMT+02:00 2024
I was working on Time conversion from One Locale to another (US to Swedish) in android using Java. It is working well on all the devices except Samsung devices.
DateTimeFormatter parser = DateTimeFormatter.ofPattern("hh:mm a", Locale.US);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a", new Locale("sv","SE"));
LocalTime time = LocalTime.parse("06:45 AM", parser);
String formatted = time.format(formatter);
In other devices I am getting output as 06:45 FM
In samsung devices I am getting it as
06:45 AM itself.
I want the the output to show as 06:45 FM in every device type.
Note : This issue only occurs for Time conversion,Date conversion (month names) works fine.
Thanks in advance.
I am working on notifications and I need to display the time-> An action was performed in a way similar to ("5 seconds ago","12 mins ago","1 week ago" etc.)
This is the code I am using
long now = System.currentTimeMillis();
String datetime1 = "06/12/2015 03:58 PM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm aa");
Date convertedDate = dateFormat.parse(datetime1);
CharSequence relavetime1 = DateUtils.getRelativeTimeSpanString(
convertedDate.getTime(),
now,
DateUtils.SECOND_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE);
And the result I get is
relativetime1=June 12, 2015
The above obtained result doesn't seem like what its supposed to look like.
By searching online I've found that if that duration is greater than a week, in which case it returns an ABSOLUTE
-I don't quite understand what I found.
How do I achieve my requirement without using an external library?
Kindly help.
Is there a way how to specify dateFormat elements and respect current locale rules? I know that I can use SimpleDateFormat and specify the format I like - but it may be wrong in different country.
I tried to mask the elements in DateFormat but it accepts only SHORT, MEDIUM and LONG:
DateFormat dayMonth = DateFormat.getDateInstance(DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD | DateFormat.MONTH_FIELD);
Caused by: java.lang.IllegalArgumentException: Illegal date style: 11
at java.text.DateFormat.checkDateStyle(DateFormat.java:843)
at java.text.DateFormat.getDateInstance(DateFormat.java:378)
I want to get "Oct 2016", or "Říj 2016". This can be implemented with:
DateFormat dayMonth = new SimpleDateFormat("MMM yyyy");
But I do not want to hard code this format in my app. I see one way only: put it into strings.xml and a translator will have to set it up. Or is there better way?
Android has DateFormat.getBestDateTimePattern() to achieve this
pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "MMMyyyy");
dayMonth = new SimpleDateFormat(pattern);
The only downside is the API level of 18. I personally used the result of DateFormat.getMediumDateFormat() as fallback. Alternatively you could just ignore the locale for older devices and use new SimpleDateFormat("MMM yyyy") there as fallback or try to backport this one method.
OP Edit
I tried on Nexus 5x with Android 6 a following code
for (Locale locale : locales) {
format = android.text.format.DateFormat.getBestDateTimePattern(locale, "MMMyyyy");
DateFormat dateFormat = new SimpleDateFormat(format, locale);
log.debug("{}: {}", locale.getCountry(), dateFormat.format(new Date()));
}
And here are some result:
NA: Jul 2016
AE: يوليو ٢٠١٦
BG: 07.2016 г.
CZ: červenec 2016
FR: Goue 2016
DE: Juli 2016
GB: Jul 2016
FI: heinä 2016
IT: Lui 2016
HR: srp 2016.
RU: июль 2016
I tested my app on 5 various phones and this exception occurs only on Samsung Galaxy Nexus:
java.text.ParseException: Unparseable date: "Sun, 19 Feb 2012 14:02:43 +0100" (at offset 0)
My input string:
<pubDate>Sun, 19 Feb 2012 14:02:43 +0100</pubDate>
My code:
private String getString(Element item, String tag) {
Element e = (Element) item.getElementsByTagName(tag).item(0);
return e.getFirstChild().getNodeValue();
}
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZ");
String tmpDate = getString(item, "pubDate");
Date pubDate = new Date(System.currentTimeMillis());
pubDate = sdf.parse(tmpDate);
Is this a problem of Android 4.0 or I made some mistake?
In addition I have a problem with my national signs. I have xml in UTF-8, and I want to display it in WebView. I have UTF-8 encoded html file and it works perfect on all devices except Galaxy Nexus - it display some strange characters instead of my national signs.
Do you have any ideas?
I would bet that the Nexus has a different locale set by default. Try using the
SimpleDateFormat(String pattern, Locale locale)
variant of the constructor to explicitly set the locale you expect in your date string.