Android Question Dateformat and Phone Language

ducphu

Active Member
Licensed User
Hi All,

In my app, I use lots of dateformat and dateparse. In this case, I use Datetime.DateFormat = "dd/MMMM/yyyy". If the phone language is English, it will display like 01/January/2015, but if the phone language is not, for example I change it to Vietnamese, it will display like 01/thang mot/2015 (thang mot = January) My question is, can I always display January regardless of the phone language. Thank you.
 

Mahares

Well Known Member
Licensed User
Would this work for you:
1. Create a map that matches the vietnamese months( key) to the US months (value).
2. Then read the map to convert your date. See example below:
B4X:
Dim sets As Map   'Globals
'In Activity_create
    sets.Initialize
    sets.Put("thang mot", "January")
    sets.Put("tháng hai", "February")
    sets.Put("hành khúc", "March")
    'rest of map here
    File.WriteMap(File.DirRootExternal, "MapMonths", sets)

    Log(MyEnglishDate("14/hành khúc/2015"))   'Displays 14/March/2015

Sub MyEnglishDate(MyDate As String) As String
    sets = File.ReadMap(File.DirRootExternal, "MapMonths")
    Dim D() As String =Regex.Split("/",MyDate)
    Return D(0) & "/" & sets.Get(D(1)) & "/" & D(2)
End Sub
 

ducphu

Active Member
Licensed User
Hi, the problem is, the users may not only use vietnamese but also chinese and other languages. So I need a solution that can work for all.
 

ducphu

Active Member
Licensed User
Hi, my code is something like

Datetime.dateformat = "dd/MMMM/yyyy"
Label1.text = datetime.date(datetime.now)

I want label1.text always shows month as January... regadless of phone language.
 

udg

Expert
Licensed User
I can't try it myself now since I'm writing this from a tablet..

Out of curiosity, changing the default locale will impact other concurrent running services/apps?
I mean, we change default locale in our app then the user presses Home and starts another app.
Will the new app use the locale we set before or it starts with an original (untouched) copy of device locale?
And what about returning to our first app (the one which changed the locale)?
 

udg

Expert
Licensed User
Peter,
personally I would have it done the same way you showed. My curiosity arised from the apparent substitution of a default locale.
 

ducphu

Active Member
Licensed User
Hi Peter, I tried changing my phone language to vietnamese then run your code. It doesn't work. This is the log:

tháng hai
16/02/2015

I will try Erel's code later. But then, the same question like udg asked: will changing the default locale impact other concurrent running services/apps?
 

ducphu

Active Member
Licensed User
Run this code when your app starts:
B4X:
Dim loc As JavaObject
loc.InitializeStatic("java.util.Locale")
loc.RunMethod("setDefault", Array(loc.GetField("US")))
Hi all, I tried this solution and it runs well on my phone but on my friend's phone, I encounter this issue:

1. If I connect the phone to PC (via USB), it works well (at least so far...)
2. If I dont connect to PC, when I first start the apps, or when the apps is killed (sometimes), it shows vietnamese date format, which then causes dateparse error and app stops. After that I re-open the app, this time it correctly shows US date format.

What can be wrong here?
 

ducphu

Active Member
Licensed User
Hi, for example
datetime.dateformat = "MMMM/yyyy"
label1.text = datetime.date(datetime.now)
dim a as string = label1.text.substring2(0,2)
datetime.dateformat = "dd/MMM/yyyy"
dim day as string = "01"
dim year as string = "2015"
dim b as long = datetime.dateparse(day & "/" & a & "/" & year)

if the language is US, a = Jan, Feb... If language is vietnamese, a = tha... which causes error.
 

ducphu

Active Member
Licensed User
Yeah I call your code right after the app starts but it still happens. Maybe I will think of another way to re-write my code. Thanks for helping.
 
Top