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()
Related
I have an app on iOS and am working on porting over to android. I am having an issue with handling the time.
On iOS I store points of time using:
Int(NSDate().timeIntervalSince1970-0.5)
and Convert it to HH:MM:SS using:
private let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
return dateFormatter.string(from: Date(timeIntervalSince1970: value))
On Android(Kotlin) to store the point of time I use: System.currentTimeMillis()/1000
And for conversion:
LocalTime.MIN.plus(Duration.ofSeconds(xVal)).toString())
The issue is that the value after being converted is 6 hours ahead(could be more or less but I only look at the HH:MM:SS). As far as I can tell, the way that I get the time on android is correct and I think the issue lies in when I am converting it. I have looked around for more methods of converting time since an an interval to time of day but I could not get it to work. Most posts are just converting seconds to HH:MM:SS and so those are right out of the question. What would the equivalent to my iOS code be?
EDIT
I've also tried using this method to convert it still is 6 hours off even though I am giving it a timezone:
val cal = Calendar.getInstance()
cal.timeInMillis = xVal * 1000L
cal.timeZone = TimeZone.getTimeZone(TimeZone.getDefault().displayName)
Log.i("This is the cal value",cal.get(Calendar.HOUR_OF_DAY).toString())
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
val date = sdf.format(Date(alarm.getNextTrigger()));
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
Hello I think I have a very simple question but i'm having trouble figuring it out.
I Got the Date and Time In Android using this code
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
It gave me back this value
Apr 21, 2016 9:30:16 PM
how do I compare using to dates with that value so if I want to see if
Apr 21, 2016 9:30:16 PM
is newer or older than
Apr 21, 2016 9:35:16 PM
How would I check that Thanks
Attempt One
I Tried This
DateFormat format = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date fileDate = format.parse(date1);
DateFormat format2 = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date metaDate = format.parse(date2);
this the value for date 1 and 2 being
Apr 21, 2016 9:35:16 PM
But it threw a parse exception. I must use that value above so What do I Need to do so it doesn't break the code when it tries to parse the date
The easiest way is to use Date.before(), rather than comparing the strings. In fact its easier to convert the string back into a date than use the strings.
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")
Let's consider this code sample:
DateFormat sdf = SimpleDateFormat.getTimeInstance(SimpleDateFormat.LONG,
new Locale("ru", "RU"));
Date date = sdf.parse("8:13:05 PDT");
When I run this code on my desktop(java 1.6) all passes well, however on android devices I get exception, I think this is due to locale TimeZone:
java.text.ParseException: Unparseable date: 8:13:05 PDT
Why?
I don't believe UNIX can parse the PDT timezone. I'm having the same issue. It can handle PST & PST8PDT, but not PDT. I believe the recommended solution is to use PST8PDT instead.