Question about date-time format string (from StackOverflow)

42 views
Skip to first unread message

P5music

unread,
Aug 22, 2020, 4:04:21 AM8/22/20
to CodenameOne Discussions
I opened this SO question
https://stackoverflow.com/questions/63492828/ios-getting-the-current-date-time-format-string
but I couldn't include the CodenameOne tag.
I wonder if there is a native function (or a workaround) to have the current date-time format in iOS as a string like

dd MMM y HH:mm:ss

It's letters not the real values. I have to show it to the user.

Thanks in advance

Shai Almog

unread,
Aug 22, 2020, 10:51:07 PM8/22/20
to CodenameOne Discussions
You use SimpleDateFormat with that string and it would write the name of the month instead of MMM in the short form.
You construct a SimpleDateFormat with that string and then use the format(Date) method from that class.

P5music

unread,
Aug 23, 2020, 6:31:37 AM8/23/20
to CodenameOne Discussions
I do not need to format a date by a format string, I need the format string itself, the current one, like the one it is now on your smartphone, it is secrectly stored somewhere. If you change the date format on your phone, another string is used. Android SDK lets me get it by a method call. Is iOS so liberal too? Where I could find that method in iOS to call natively from my app.
Thanks

Shai Almog

unread,
Aug 23, 2020, 9:56:32 PM8/23/20
to CodenameOne Discussions
It's defined somewhere but not using this string and I'm unaware of the API where it's defined natively. This is what we do for native date formatting (Objective-C code) in our native implementation:

#ifndef CN1_USE_ARC
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
#else
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
#endif
if (currentLocale != NULL) {
     formatter.locale = currentLocale;
} else {
     formatter.locale = cn1DeviceLocale();
}
NSDate* date = [NSDate dateWithTimeIntervalSince1970:(d / 1000)];
[formatter setDateStyle:NSDateFormatterMediumStyle];
JAVA_OBJECT o = fromNSString(CN1_THREAD_STATE_PASS_ARG [formatter stringFromDate:date]);
POOL_END();
return o;

P5music

unread,
Aug 24, 2020, 4:41:40 AM8/24/20
to CodenameOne Discussions
Thank you.
I would like to know whether the SimpleDateFormatter implementation is overridden by the user settings.
I read in Apple documentation that despite the format string, some values can be changed on behalf of the user, like when 24H format is replaced with AM/PM.
So is the implementation override-prone?
I am asking this because I think Android does not override and I rely on that on the Android platform, I read also the documentation and I do not remember having read something like that.
But iOS does so, and this could impact my app. It is important for me to know.

Shai Almog

unread,
Aug 24, 2020, 11:42:15 PM8/24/20
to CodenameOne Discussions
No. It doesn't.

P5music

unread,
Aug 25, 2020, 3:31:01 AM8/25/20
to CodenameOne Discussions
I read this documentation (and other):
It is stated that there is overriding if you do not use special methods for fixed formats. I think the code you provided uses simple methods that have the dateFormat that can be overridden.
As to my original question, I think it is useful for me to use the "dateFormat" property, I mean read it and then set it with another value, but I think I have to use the special  case like this
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

I cannot test it now but I hope it works.

Shai Almog

unread,
Aug 26, 2020, 2:53:04 AM8/26/20
to CodenameOne Discussions
AFAIK this won't work. We generally provide such things in L10NManager. If it's missing a method that can address that feel free to file an RFE or pull request.

P5music

unread,
Aug 26, 2020, 5:04:55 AM8/26/20
to CodenameOne Discussions
I do not understand, do you deem that the native field dateFormat is not good to read from after a date instance has been read from the system? Do you think it is read-only or simply it does not yield meaningful values?

Shai Almog

unread,
Aug 26, 2020, 10:26:08 PM8/26/20
to CodenameOne Discussions
That's swift code. We work with objective-c. Notice that this specific syntax from Apple might be incompatible with our SimpleDateFormat class. You can probably adapt and port it using native interfaces but you might run into problems where a specific device might have a format we don't support.

P5music

unread,
Aug 27, 2020, 4:42:56 AM8/27/20
to CodenameOne Discussions
Do you mean that your SimpleDateFormat is missing some format string combinations among the ones that can be found on Apple devices?
I think those are standard too and you certainly created a list of formats from some official documentation table, maybe from Java (Android?),  but it is likely it's a standard anyway. What about that? 
What is your concern?
I think that an Obj-C version of that code snippet can be easily found. I am not asking to introduce a new feature anyway, it is up to you.
For my app I need the current format (I think it does not fail the standard) and then use SimpleDateFormat for other formats I took from general Android documentation. I think they are compatible. The only concern is about the current dateFormat value from iOS, as I said I think it is as standard as what is in SimpleDateFormat.

Shai Almog

unread,
Aug 28, 2020, 12:52:32 AM8/28/20
to CodenameOne Discussions
Yes.
You can probably accomplish this very easily with a native interface.
Reply all
Reply to author
Forward
0 new messages