Android Question Dateformat and Phone Language

Discussion in 'Android Questions' started by ducphu, Feb 14, 2015.

  1. ducphu

    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.
     
  2. Mahares

    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:
    Code:
    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 StringAs 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
     
  3. ducphu

    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.
     
  4. Peter Simpson

    Peter Simpson Expert Licensed User

    Last edited: Feb 15, 2015
  5. ducphu

    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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Run this code when your app starts:
    Code:
    Dim loc As JavaObject
    loc.InitializeStatic(
    "java.util.Locale")
    loc.RunMethod(
    "setDefault"Array(loc.GetField("US")))
     
    Devv likes this.
  7. udg

    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)?
     
  8. Peter Simpson

    Peter Simpson Expert Licensed 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...
     
  9. udg

    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.
     
  10. ducphu

    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?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    No. It will only affect the current process.
     
  12. ducphu

    ducphu Active Member Licensed User


    Ok. Your code works perfectly.Thank you.
     
  13. ducphu

    ducphu Active Member Licensed User

    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?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Why does it affect the date parsing? Are you using the months names for parsing?
     
  15. ducphu

    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.
     
  16. Erel

    Erel Administrator Staff Member Licensed 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.
     
  17. ducphu

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice