Android DateTimeFormatter - Time Conversion not working on samsung devices - android

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.

Related

Get GMT time in Android always in European letters

I'm currently running to the following problem:
I'm running this command
TimeZone.getDefault();
While my phone (Galaxy S6 with Android 7.0) is configured to with system language of Arabic (العربية to be precise).
The output of this call is: GMT+٠٢:٠٠ and not GMT+02:00 as I would expect it to be.
I've noticed that this issue happens only on Samsung devices (at least from the errors that I saw in my logs).
Is there a way to make sure that the timezone which I'm getting will be in the format of GMT+02:00 regardless of the locale of the phone?
The solution which I've found:
final static String am = "GMT-%02d:%02d";
final static String pm = "GMT+%02d:%02d";
final String timezone = String.format(Locale.ENGLISH, offset < 0 ? am : pm, hours, minutes);

Android getRelativeTimeSpanString not working

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.

Android Watch Face TextClock

I have an Android Watch Face that works for 12-hour formats. I am confused how to implement a 24-hour format based on the system preference of the phone, using TextClock or any other method.
SimpleDateFormat twelvehour = new SimpleDateFormat("h:mm a", Locale.US);
String TimeAmPmNoSec = String.format("%s", twelvehour.format(cal.getTime()));
canvas.drawText(TimeAmPmNoSec, (centerX - (timeXLength / 2.0f ), timeYOffset, timePaint);
That setting is bridged from your phone to the watch automatically and you can get it on your watch app by calling DateFormat.is24HourFormat(context).

SimpleDateFormat behaves differently on different tablet

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.

ParseException only on Galaxy Nexus

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.

Categories

Resources