Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm supposed to show different time formats according to the language in my app. When the device is English the user should get the time format like this:

18 March 2018, 2.30 pm

But when the user's device is German he should get the time format like this:

18.03.2018, 14:30 Uhr

Is there any way to do this by formatting the time String with SimpleDateFormat or am I supposed to do this in another way and if so, how am I able to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
228 views
Welcome To Ask or Share your Answers For Others

1 Answer

I didn't think it would work, but it did. Just put the format you like into the string xml file for me it was:

<string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
<string name="date_time_format">dd.MM.yyyy, HH:mm</string>

Then use it in the SDF like this:

SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());

for the "Uhr" at the end of the german format i added a placeholder String that looks like this:

<string name="date_time_string">%s</string>
<string name="date_time_string">%s Uhr</string>

and i return the formated date with the String.format() method:

return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...