Java.text.ParseException: persistent date: "" (at offset 0)

I have read many answers to this problem, but the answer does not solve my problem.

I am trying to parse this line:

"2013-10-07T23:21:00+01:00"

for a Date object with a simplified format:

"yyyy-MM-dd'T'HH:mm:ssZZZZZ"

but it continues to generate an error:

java.text.ParseException: persistent date: "" (at offset 0)

Note. I am trying to do this on Android, I am new.

+6
source share
2 answers

Try the following code

 public static Calendar parseDate(String dateTimeStr) throws ParseException { Calendar calendar = GregorianCalendar.getInstance(); String s = dateTimeStr.replace("Z", "+00:00"); try { s = s.substring(0, 22) + s.substring(23); } catch (IndexOutOfBoundsException e) { throw new ParseException("Invalid length", 0); } Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s); calendar.setTime(date); return calendar; } 
+2
source

If you are using java 7, you can use:

 yyyy-MM-dd'T'HH:mm:ssXXX 

You can check more here

-2
source

Source: https://habr.com/ru/post/955484/


All Articles