Jump to content
ertank

Android short time format 24h/12h

Recommended Posts

Hello,

 

Android has 24H/12H hour display format setting.

I wanted to format my time values displayed as in system settings but I couldn't find how to read that specific is24HourFormat value.

Delphi TFormatSettings does not include that information.

There are earlier SO threads like this one. But that code gives me

Invoke error: method not found.

 

I also tried to import "android/icu/text/DateFormat" that I found in another SO answer and the app crashes without any error displayed.

 

What would be the correct way to read that system setting?

 

Thanks & Regards,

Ertan

 

Share this post


Link to post
On 12/10/2024 at 5:48 PM, ertank said:

But that code gives me


Invoke error: method not found.

 

The method name is24HourFormat, not is24hourformat, and is also a static method, so the declaration should be:

  JDateFormatClass = interface(JObjectClass)
    ['{E9A75876-EDA1-44CE-B159-46BACF1805F7}']
    {class} function is24HourFormat(context: JContext): Boolean; cdecl;
  end;

  [JavaSignature('android/text/format/DateFormat')]
  JDateFormat = interface(JObject)
    ['{65E305D7-04D6-4C33-8AB0-9FE366F3F24D}']
  end;
  TJDateFormat = class(TJavaGenericImport<JDateFormatClass, JDateFormat>) end;

..and called like this:

uses
  Androidapi.Helpers;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if TJDateFormat.JavaClass.is24HourFormat(TAndroidHelper.Context) then
    // is 24 hour
  else
    // is 12 hour
end;

Note that I have not tested the code above - I am just going by the documentation (which I linked to) 

Share this post


Link to post
2 hours ago, Dave Nottage said:

Note that I have not tested the code above - I am just going by the documentation (which I linked to) 

I tested it on Android 12 and and it is working just fine.

Original SO answer was missing the case match of the function and its declaration type which I should be careful for the next time.

Thanks for the help.

 

  • Like 1

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

×