Jump to content
Sign in to follow this  
Fabian1648

[Android] How to change regional settings

Recommended Posts

Hello,

 

Is it possible to change the regional settings of an Android device via Delphi code?

 

If so, how?

 

Thank you

Share this post


Link to post

I have a problem with FormatSettings.ShortDateFormat: When I use it to display a date according to the country standard (e.g. the order "day, month, year" is not suitable in the USA), the year is only displayed with 2 characters. I'd like to take advantage of the default date format defined by the Android device OS but force the year to be displayed with 4 characters instead of 2. If I force the date format (example with a "dd mm yyyy"), I will lose the automatic adaptation of the date format according to the country...

 

Share this post


Link to post

Why would you like to change the regional settings for that case? Use a custom format to convert the date to string.

Share this post


Link to post
23 hours ago, Lajos Juhász said:

Why would you like to change the regional settings for that case? Use a custom format to convert the date to string.

When you have a software that is used in different countries, the date display format changes from one time to another (In France, you will have a "dd/mm/yyyy", in the USA a "mm-dd-yyyy", etc.). If you use a custom format in the code, you will have to create a custom format for each country!!!!!!!!!!

 

So, I would simply force the display of the year with 4 characters instead of 2...

Edited by Fabian1648

Share this post


Link to post
Just now, Fabian1648 said:

When you have a software that is used in different countries, the date display format changes from one time to another (In France, you will have a "dd/mm/yyyy", in the USA a "mm-dd-yyy", etc.). If you use a custom format in the code, you will have to create a cutom format for each country!!!!!!!!!!

 

If you change the regional settings in your application you will change it in all of the countries the same thing as far as I can see.

Share this post


Link to post
2 minutes ago, Lajos Juhász said:

If you change the regional settings in your application you will change it in all of the countries the same thing as far as I can see.

Here is my problem:

 

Why the default TFormatSettings.ShortDateformat displays the year with 4 characters under Windows, but only with 2 characters under Android?

 

Isn't there a way to notify Android that you want the year with 4 characters instead of 2 without changing the regional settings?

Share this post


Link to post

You failed to show us an example where are you have a problem. You can use FormatDateTime or DateToStr with yuor own format settings.

Share this post


Link to post
4 hours ago, Fabian1648 said:

Here is my problem:

 

Why the default TFormatSettings.ShortDateformat displays the year with 4 characters under Windows, but only with 2 characters under Android?

 

Isn't there a way to notify Android that you want the year with 4 characters instead of 2 without changing the regional settings?

 

You can still use:
 

 var MyFormat: TFormatSettings;
 MyFormat:=TFormatSettings.Create; // obtains default settings

 if pos('yyyy', MyFormat.ShortDateFormat)<=0
  then MyFormat.ShortDateFormat:=StringReplace(MyFormat.ShortDateFormat, 'yy', 'yyyy', []);

 ... DateToStr(now, MyFormat);

 

 

  • Like 1

Share this post


Link to post
4 hours ago, Lajos Juhász said:

You failed to show us an example where are you have a problem. You can use FormatDateTime or DateToStr with yuor own format settings.

1)

If you use FormatDateTime to define in your Delphi code a custom date format as "yyyy-mm-dd", you will have this format regardless of the country where you use the software: 2022-08-05 in France, 2022-08-05 in USA, 2022-08-05 in Deutschland or in Japan.

 

Each country has his standard. In France, the date format is "05/08/2022" and not "2022-08.05"! That's why the regional settings of an OS exist!

 

 

2) Our software must be used worldwide. The date format must be adapted for the country where it is used.

 

3. We don't want to replace the regional settings of the OS with Delphi code that specifies settings for every country in the world! We want that the regional settings display date with 4 characters ans not 2 for the year. That's it!

 

 

Share this post


Link to post
43 minutes ago, Vandrovnik said:

 

You can still use:
 


 var MyFormat: TFormatSettings;
 MyFormat:=TFormatSettings.Create; // obtains default settings

 if pos('yyyy', MyFormat.ShortDateFormat)<=0
  then MyFormat.ShortDateFormat:=StringReplace(MyFormat.ShortDateFormat, 'yy', 'yyyy', []);

 ... DateToStr(now, MyFormat);

 

 

Great! This is exactly what I was looking for!


Your code uses the date format provided by the OS but forces the display of the year to 4 characters.


Thanks a lot Vandrovnik!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×