Android Question Dateformat and Phone Language

ducphu

Active Member
Licensed User
Longtime 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

Expert
Licensed User
Longtime 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
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime 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.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime 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.
 
Upvote 0

udg

Expert
Licensed User
Longtime 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)?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@udg @ducphu only want to get the month, that whole procedure happens extremely quickly so I don't think that there will be any issues. The code above is how I would do it and it would work perfectly fine too. Now obviously there are plenty of other ways to get the results, the above code it just my solution...
 
Upvote 0

udg

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

ducphu

Active Member
Licensed User
Longtime 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?
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime 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?
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that it is a good idea to let the user write the month name. It will be difficult. Either use a date picker (such as AnotherDatePicker) or use a simpler format.

About the locale setting, I don't see how it can be affected by USB debug mode. You need to make sure that you call it when immediately when your app starts.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime 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.
 
Upvote 0
Top