GregorianCalendar comparison in android, device vs emulator discrepancy - android

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")

Related

SimpleDateFormat Unparseable date only on phone

I need to parse a string from my database which has "Mon Jul 18 12:58:05 2022" string as value. Weirdly it only works with emulator. But, if i try to uses my app in my phone it throws Unparseable date error. My phone is using android 9 which use sdk 28, so i think it should be fine. I alread tried to remove the timezone which in many cases could cause the problem. But, after i remove it and the error still happen. It took me sometime to figure this out, but in the end i still got no answer. I just wanted to know what causing such problem and how to fix it?
Here is my code :
ref.child(currentData.id.toString()).child("onwork_start").get().addOnSuccessListener {
val sdf = SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy")
val time = sdf.parse(it.value.toString())
val date = SimpleDateFormat("dd MMM HH:mm")
val show_time = date.format(time)
holder.tv_konfirmasi_bill.text = show_time.toString()

Android TextClock misbehaving

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

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.

Android: Formatting a Calendar object contains p.m. on one device and PM on another device

When formatting a Calendar object in Android using SimpleDateFormat class with a pattern of yyyy-MM-dd hh:mma, produces 2016-02-19 06:31PM on one device and 2016-02-19 06:33p.m. on another device.
Is there any settings on the device that influence this behavior? or is it because of Android OS Level?
I tried on the devices running following OS
Android 6.0.1. (This produces string that contains p.m.)
Android 5.0.2. (This produces string that contains PM)
SimpleDateFormate is device's default Locale Dependent, if you want a constant formatting for all devices just specify your required Locale to SimpleDateFormat object like:
SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", REQUIRED_LOCALE);

Is setTimeZone method is device independent?

My App is heavily dependent on time. If I set the time zone using below code and user device time/date is not set correctly then will it work correctly and return correct time?
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
This will not help if the device time is off. By setting the timezone, you are just telling the formatter to display (what the device thinks is) the current time with an offset of +7/+8 hours.

Categories

Resources