Parse microseconds datetime format in android kotlin - android

I want to parse the following String :
2022-02-14T04:40:33.000000Z
So I'll be able to convert it to this date format:
14/Feb/2022 04:40 or dd/MMM/yyyy HH:mm.
But it throws an exception :
Caused by: java.text.ParseException: Unparseable date: "2022-02-14T04:40:33.000000Z"
My code is a basic SimpleDateFormat with the following code :
val datetime = "2022-02-14T04:40:33.000000Z"
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val outputFormat = SimpleDateFormat("dd/MMM/yyyy HH:mm", Locale.getDefault())
val inputDate = inputFormat.parse(date) // exception thrown here.
val dateString = outputFormat.format(inputDate)
I had tried the following format :
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ss.SSS'000'Z (I was expecting to ignore the extra 000)
I had read about the PHP DateTime::format which had the u format character for microseconds.
But, the android SimpleDateFormat had no support for the microseconds character.

Please use following format
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Related

How to format string to date in Android

In my application I one string such as 2023-2-14 and I want convert this to 2023-02-14.
I write below codes:
val format = SimpleDateFormat("yyyy-MM-dd")
val date: Date = format.parse(startDateStr)
Log.e("dateLog",""+date)
But in logcat show me this : Wed Feb 15 00:00:00 GMT+03:30 2023
Why? I used this format : yyyy-MM-dd.
Why not used this format?
you are just parsing date, without a time, thus date object have set 00 for hour, day etc. now use format method for converting old String to new one
val formatAs = "yyyy-MM-dd"
var format = SimpleDateFormat(formatAs)
val date: Date = format.parse(startDateStr)
Log.e("dateLog","date:"+date)
String dateAsStringFormatted = format.format(date);
Log.e("dateLog","dateAsStringFormatted:"+dateAsStringFormatted)
some other answers in HERE

Kotlin convert DateTime to 10 digit TimeStamp

I'm trying to find out how I can convert DateTime to 10 digit timestamp in Kotlin (Android Studio), I can't find any equivalent of it in Kotlin.
For example:
I have a val with date-time value like :
val dateTime = "2020-12-13 17:54:00"
Now I want to convert it to 10 digit timestamp like "1607842496"
Please give me a simple sample to show how I can resolve this problem. Thanks in advance.
Use the SimpleDateFormat class to parse your date String to a Date object. You can then get the timestamp (in milliseconds) from that Date object like so:
val dateTime = "2020-12-13 17:54:00"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val date = simpleDateFormat.parse(dateTime)
val timestamp = date?.time
Divide the timestamp by a 1000 to get the 10 digit (in seconds) timestamp.

Convert string into LocalDateTime

I have the following String:
18/07/2019 16:20
I try to convert this string into LocalDateTime with the following code:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
java.time.format.DateTimeParseException: Text '18/07/2019 04:30:00'
could not be parsed: Unable to obtain LocalDateTime from
TemporalAccessor
What I'm missing?
I think this will answer your question:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
Edit 1:
It's probably crashing because you are using a 12hr Hour, instead of a 24hr pattern.
Changing the hour to 24hr pattern by using a capital H should fix it:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
Use below to convert the time from String to LocalDateTime, but make sure you are getting the time in String form.
String str = "2016-03-04 11:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Btw, If your String contains seconds as well like "2016-03-04 11:30: 40", then you can change your date time format to yyyy-MM-dd HH:mm:ss" as shown below:
String str = "2016-03-04 11:30: 40";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Change your datetime format to "dd/MM/yyyy hh:mm a" and provide a string date with additional AM/PM information, e.g. val stringDate = "18/07/2019 04:20 PM" or just use the 24 hour format "dd/MM/yyyy HH:mm".
You may try using "-" instead of "/" on the date.

Joda time convert ISO8601 GMT to local

I have this from my server:
2017-04-07T11:00:00.000Z
How can I convert it to local time? Here is my code, but all I got is an exception:)
java.lang.IllegalArgumentException: Invalid format: "2017-04-07T11:00:00.000Z" is malformed at "Z"
DateTimeFormatter inputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ
.withLocale(Locale.US);
DateTime parsed = inputFormatter.parseDateTime(gmtDate);
DateTimeFormatter outputFormatter = DateTimeFormat
.forPattern("HH:mm")
.withLocale(Locale.US)
.withZone(DateTimeZone.getDefault());
return outputFormatter.print(parsed);
Just change your pattern to yyyy-MM-dd'T'HH:mm:ss.SSSZ, like this:
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(Locale.US);
In my case, the result of outputFormatter.print(parsed) is 08:00 (my local time, as my default timezone is America/Sao_Paulo)

Android date time parsing

I am trying to parse the following str to Date:
2013-10-23T11:00:00EDT
I am using follwoing to do so:
String TIMEZONE_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
Date date = new SimpleDateFormat(TIMEZONE_DATE_TIME_FORMAT, Locale.US).parse(strDate);
its working good on most devices but on some it throws the following exception:
java.text.ParseException: Unparseable date: "2013-10-23T11:00:00EDT" (at offset 19)
Can someone explain why?
Try this code. This may help you to fix the exception
SimpleDateTime format = new SimpleDateTime("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);
Date date = format.parse(text);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ")
.withLocale(Locale.US);
DateTime dateTime = formatter.parseDateTime(text);
In fact, there's an ISODateTimeFormat class to make this even simpler:
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();

Categories

Resources