Converting ISO 8601 string to java.util.Date: best practices
Hello everyone! I'm a beginner Java developer and I've encountered a problem with parsing dates. I need to convert strings in ISO 8601 format (for example, "2023-10-05T14:30:00Z" or "2023-10-05T17:30:00+03:00") into a java.util.Date object. \n\nThe problem is that I'm using SimpleDateFormat, but it often gives errors or incorrect results. For example, the string "2023-10-05T14:30:00Z" is parsed incorrectly, and "2023-10-05T17:30:00+03:00" even causes a ParseException. Here's my code:\n\n```java\nString dateString = "2023-10-05T14:30:00Z";\nSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");\ntry {\n Date date = sdf.parse(dateString);\n System.out.println(date);\n} catch (ParseException e) {\n e.printStackTrace();\n}\n```\n\nI understand that 'Z' at the end denotes UTC, but Simple
No answers yet.
Log in to leave an answer