Illegal argument for datetime pattern in App Inventor 2 - android

I'm using a smartphone to save daily reports to database (Mysql), using App Inventor 2.
When i try my Apps using all type of Samsung Smartphone, it shows Error like this.Samsung notification Error
This is the notification Error :
"Sorry to be so picky Illegal argument for pattern in Clock.FormatDateTime. Acceptable patterns are empty string, MM/dd/YYYY HH:mm:ss a, or MMM d, yyyy HH:mm. The empty string will provide the default format, which is "MMM d, yyyy HH:mm:ss a" for FormatDateTime, "MMM d, yyyy" for FormatDate. To see all possible pattern, see docs.oracle.com/javase/7/docs/api/java/text/… END APPLICATION"
But when I use Xiaomi A1, no error notification appears. And the data can smoothly insert to Database.
This is the datetime pattern that i use in Block script.
Block Pattern Script
I'm using "yyyy-MM-dd HH:mm:ss" clock pattern in App Inventor 2.

I finally solve the problem. By using procedure that join "yyyy","MMMM","dd", and "HH:mm:ss".
Block Procedure

To do it easily you just have to do the following
InstantInTime MakeInstant(text from):
Returns an instant specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm. An example text input is "06/22/2015 12:18"

Related

SimpleDateFormat: parsing exception on android 6 devices

I'm trying to receive a string to a date. Here's an example of the string:
val date = "10/10/2016 12:00:00 AM" //format month/day/year
Now, I'd like to convert this string into a date. To do that, I'm trying to run the following:
val formatter = SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa")
var date = formatter.parse(date)
Whenever this code is running on devices running android 8, everything works out great. However, if I try to run in older devices (ex.: phones using Android 6), I end up with a ParseException:
Unparseable date: "10/10/2016 12:00:00 AM" (at offset 20)
I've noticed that removing the AM/PM characters (aa) from the string solves the parsing exception. Can someone please tell me what's going on here?
thanks
Regards,
Luis
PS: the code runs without any problem in the emulator, but not in real devices
Try this :
val date = "10/10/2016 12:00:00 AM"
val formatter = SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US)
var date = formatter.parse(date)
Got from here : https://developer.android.com/reference/java/text/SimpleDateFormat?hl=pt-br
Looks like they never use "aa" for "PM/AM" value but rather "a" or "aaa".
Also from this response : Unable to parse DateTime-string with AM/PM marker
They recommend changing your default Locale To Locale.US if you have different symbols for PM/AM

Android 12 hours Time Format getting wrong

I am using
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
String dateAsString = dateFormat.format(gmt);
And getting String 06-06-2017 08:15 a.m.
Why I am getting a.m. instated of AM or PM?
The AM/PM/a.m. actually depends on the device. Try the same code on other devices and you might get to see a different result. If you need AM/PM only, then you need to do it manually by replacing the dots and converting it to uppercase.
It depends on the locale. If you use SimpleDateFormat (which you may not want to do, see below), I recommend you give it an explicit locale. The one you construct uses the device’s default, which explains why you get different results on different devices. If you want that, use new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.getDefault()) so the reader knows you have thought about it. To make sure you get AM and PM, use for example new SimpleDateFormat("MM-dd-yyyy hh:mm a", Locale.ENGLISH).
Why would you not want to use SimpleDateFormat? I consider it long outdated since the much better replacement for the Java 1.0 and 1.1 classes came out with Java 8 in 2014. They have also been backported to Android Java 7 in the ThreeTenABP. Get this and write for example:
LocalDateTime gmt = LocalDateTime.of(2017, Month.JUNE, 6, 8, 15);
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm a",
Locale.ENGLISH);
String dateAsString = gmt.format(dateTimeFormat);
The result is
06-06-2017 08:15 AM
To make explicit that the time is in GMT, you may use an OffsetDateTime with offset ZoneOffset.UTC.
Link: How to use ThreeTenABP in Android Project.

SQLite Datetime Conversion?

I m developing android application. I need to convert datetime into date. I want to convert '25-07-2013 11:44AM' (datetime) into '25-07-2013' (date).
I am trying this function to convert SELECT date('25-07-2013 11:44AM'), but it was not working.Please suggest some solution for this problem.
According to this page, it does not seem that am/pm times are supported in date SQLite function (this should be noted as h or K according to the date format specification of Java at least; also it is explicitly mentioned %H hour: 00-24). Maybe experiment if using 24 hour clock will not trigger the issue.
I am not sure if you are search for Java code or SQL code , but if it is Java code then solution could be this :
String date = "25-07-2014 11:44AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String newDate = dateFormat.format(dateFormat.parse(date));
System.out.println(newDate);
If in case your are looking for SQL code let me know I will share that as well.

How do I display time in 24-hour format using a TextClock?

I am having trouble trying to get a time to display in 24-hour format. I have got it to display the time in another time zone but I can't get it to display in 24-hour format.
<TextClock
android:id="#+id/hk_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timeZone="GMT+0800"
android:format24Hour="MMM dd, yyyy k:mm" />
Also is it possible to display a date using a TextClock?
If your system is in 12-hour mode, you shouldn't have to write a subclass to get what you want. The JavaDocs say:
In 12-hour mode:
Use the value returned by getFormat12Hour() when non-null
Otherwise, use the value returned by getFormat24Hour() when non-null
Otherwise, use a default value appropriate for the user's locale, such as HH:mm
Therefore, I would expect the last line in this to make getFormat12Hour() return null and thus use your custom getFormat24Hour() format:
<android.widget.TextClock
android:id="#+id/hk_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timeZone="GMT+0800"
android:format24Hour="MMM dd, yyyy k:mm"
android:format12Hour="#null" />
(BTW, as others have mentioned, forced formats should be used with care. If your users wanted a 24-hour clock, they would have specified this in their system preferences. And apps that force numerically formatted, topsy-turvy date formats on their users, like MM/dd/yyyy, invite one-star ratings (except, in this case, from US and Filipino users), so it's a good thing you're using MMM! You'd still be better off using getBestDateTimePattern(), though, which, I believe, would require subclassing TextClock.)
My device using 12h format. To force it to use 24h format, I write this:
textClock.setFormat12Hour("kk:mm");
instead of set time format in xml i'm suggest to use with java file i.e
mTextClock.setFormat24Hour(format);
For Kotlin
I used this line for making time format to 24-Hours and it's working fine.
mTextClock.format12Hour = "kk:mm"
Whether it shows the time in 12- or 24-hour format depends on the system setting. Perhaps your Android device or emulator is set to 12-hour format. That is why your android:format24Houris ignored. If you would like to show the 24-hour format by overwriting the system setting (which I would not recommend), add android:format12Hour="MMM dd, yyyy k:mm" as another attribute.
However, it is always recommended to show the time and date according to the system setting. If there are some other (e.g. layout) constraints in your app that require separate handling of 12- or 24-hour mode, you can check the setting dynamically in code by calling is12HourModeEnabled() or is24HourModeEnabled() on your TextClock.
To your second question, the docs say
TextClock can display the current date and/or time as a formatted
string.
and that's what it is capable of -- display a the time and date. If you mean "another date than the current", you would have to stick to a simple TextView and format the date String manually.
Hope this will help you:
<TextClock
android:id="#+id/tc_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="h:mm:ss a - d MMM, yyyy"
android:format24Hour="k:mm:ss - d MMM, yyyy"
/>
1st question:
I've just checked it and when on your device is set:
12-h format(US, GB) - TextClock is 12h
24-h format(Europe) - TextClock is 24h
As mentioned above, it's better when you'll give the choice to the user and you won't force him to use ex. your favorite format.
2nd question:
Yes, it is. You just need to use "android:format12Hour" and/or "android:format24Hour".
android:format12Hour="MMM dd, yyyy"
android:format24Hour="MMM dd, yyyy"
MMM means 3 characters for a month's name
dd means 2 characters for a day
yyyy means 4 characters for a year

Time zone offset format +HH:mm versus +HHmm

I need to communicate with one application that is not implementing the full ISO 8601 and only accept the format +HH:mm for time zone offset.
Android seems to only generate the format +HHmm (no ':' character in between hours and minutes) with the 'z' in SampleDateFormat.
Code example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String str = String.format(Locale.US, "%s", sdf.format(new Date(0)));
generates: 1970-01-01T00:00+0000
and I would like to generate: 1970-01-01T00:00+00:00
Is there any simple way of producing the desirable output without writing the code to manipulate the string?
Thanks,
Luis
I think the easiest way is to manipulate a string.
a) SimpleDateFormat doesn't support what you need.
b) The method appendNumericTimeZone in SimpleDateFormat class is private. So, you can't override it.
2) You can create your own formater (implement java.text.DataFormat). However, it will be way more hassle than string manipulation.
BTW. Interesting thing which I found while looking into SimpleDateFormat source code. There is some code which generates almost what you need (it adds to the end "GMT+XX:XX"). However, this code will be only called, if you specified "Z" in the format and system can't find a timezone name for current timezone.

Categories

Resources