Android date time parsing - android

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();

Related

Parse microseconds datetime format in android kotlin

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

Getting Error In DateTimeFormatter

I added this jar file joda-time-2.0.jar in my android project . After adding this jar file it cannot taken the .ofpattern() , please solve this one. thank you.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Using Joda time you would do it like this:
String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));

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)

How to parse threeten DateTIme

I am trying to parse this DateTime:
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("dd/MM/yyyy hh:mm:ss")
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
.toFormatter();
String ultimaAtualizacaoTexto = "17/12/2016 01:41:43";
LocalDateTime ultimaAtualizacaoDateTime =
LocalDateTime.parse(ultimaAtualizacaoTexto, dateTimeFormatter);
But I am getting this error:
DateTimeParseException: Text '17/12/2016 01:41:43' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: DateTimeBuilder[fields={MilliOfSecond=0, MinuteOfHour=41, MicroOfSecond=0, NanoOfSecond=0, HourOfAmPm=1, SecondOfMinute=43}, ISO, null, 2016-12-17, null], type org.threeten.bp.format.DateTimeBuilder
I am not sure what is going on... It should totally work!
Any help with this? Thanks anyway!
So... I had to change "dd/MM/yyyy hh:mm:ss" to "dd/MM/yyyy HH:mm:ss"
Because hh is only for 12 am/pm hours. HH is for 24h.

java.lang.NumberFormatException: Invalid date/time format: 2016-01-21T00:57:03.126+0500

I am setting date and time using this method...
calendar.set(year,monthOfYear,dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
When fetching DateTimeStamp, and setting in Event object, I am facing an error
NumberFormatException: Invalid date/time format:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
DateTime startDateTime = new DateTime(sdf.format(calendar.getTime()));//This Line causing it
EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone(calendar.getTimeZone().toString());
event.setStart(start);
Problem was solved by using "yyyy-MM-dd'T'HH:mm:ss'Z'" this format.
As DateTime accepts RFC3339 where zone ('z') is written in quotes

Categories

Resources