I set two TextClock views in my activity, one using format24hours:"dd/MM/yyyy", the other format24hours:"HH:mm:ss".
I tested it on my phone (Motorola G6 with Android O) the correct format is displayed.
Now i'm working on a tablet (Galaxy Tab E with Android Nougat -> Lineage ROM) and the format displayed for both views is "hh:mm aa".
I tried forcing the format programmatically in addiction to the xml, but it is just ignored.
Any idea?
I had the same issue and I forced the 12 hours format.
I wanted the app to display MMMM dd, yyyy | HH:mm:ss,
so I coded
tvTimeStamp.format12Hour = "MMMM dd, yyyy | HH:mm:ss"
tvTimeStamp.format24Hour = "MMMM dd, yyyy | HH:mm:ss"
Just use SingleDateFormat to show all formatted dates and times in correct for you format. You can find example in http://tutorials.jenkov.com/java-internationalization/simpledateformat.html
Related
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"
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
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.
I need to compare two GregorianCalendar objects within an android application. I tried:
myCalendar.compareTo(otherCalendar)
myCalendar.getTimeInMillis() > otherCalendar.getTimeMillis()
In the emulator, running under ubuntu 64bit, both works fine. I tried then on a samsung galaxy s2 device with no luck. Comparison on real device is inverted, the bigger results smaller and viceversa. Printing the value on both systems with:
Long.toString(myCalendar.getTimeMillis())
I found out that a value of 1359716008000 in the emulator (I think this is the correct one) corresponds to -636369904720 in the actual device.
Any help would be really appreciated!
just for posterity, the problem was the GregorianCalendar value itself, I assigned the value with
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
myCalendar.setTime(sdf.parse(myStringDate));
It just needed a
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss",Locale.US)
as said here, the default locale of emulator (maybe US) was not the same of device (italian) so I think it was misparsing the day of the week in letters ("EEE")
I have been working on problem of date format selected by users in the Android Phone.
I have gone through almost many question and answers on the stackoverlow and havent got the perfect answer.
For example:-
I have selected dateformat in phone is "Sat, 31 Dec 2011"
But when I used as based question answered on stackoverflow:-
DateFormat df = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());
tv.setText(df.format(date));
Above code returns me 06/08/2011 (mm/dd/yyyy) format.
But see the date format i selected on phone. How can i get the exact format?
In an application I'm currently developing I use the following, which I believe will do what you require
final String format = Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);
if (TextUtils.isEmpty(format)) {
dateFormat = android.text.format.DateFormat.getMediumDateFormat(getApplicationContext());
} else {
dateFormat = new SimpleDateFormat(format);
}
The DateFormat.getDateFormat works as expected. It returns one of three possible formats:
MM/DD/YYYY
DD/MM/YYYY
YYYY/MM/DD
I'm not sure how you set your "Sat, 31 Dec 2011" format, but as far as I know stock Android allows to set only one of three mentioned formats (Settings -> Date and Time -> Select Date Format).
Try this
Date inDate = inFormat.parse(in);
DateFormat outFormat = android.text.format.DateFormat.getDateFormat(context);
out=outFormat.format(inDate);