tl;dr
OffsetDateTime.parse( "2001-05-03T00:00:00+00:00" )
Avoid legacy date-time classes
Never use SimpleDateFormat
, Date
, Calendar
, etc. These terrible classes were supplanted years ago by the java.time classes.
ISO 8601
trying to convert a string into an ISO 8601 date-time format
String s1 = "2001-05-03T00:00:00+00:00"
Your Question is quite confused. Your input string is in standard ISO 8601 format. The T
in the middle separates the year-month-day from the hour-minute-second, and the +00:00
at the end indicates an offset-from-UTC of zero hours and zero minutes, that is, UTC itself. All standard, all proper.
Perhaps you are conflating strings representing date-time values and date-time objects containing date-time values. A date-time object has no “format”; it has its own internally-defined representation of a date-time value. Only text has a format. A date-time class can parse a formatted string as input, and a date-time class can generate a formatted sting as output, but within the date-time there is no format at all.
OffsetDateTime
The java.time classes use the ISO 8601 formats by default when parsing/generating strings representing date-time values. So no need to specify a formatting pattern for such inputs.
OffsetDateTime odt = OffsetDateTime.parse( "2001-05-03T00:00:00+00:00" ) ;
See this code run live at IdeOne.com.
odt.toString(): 2001-05-03T00:00Z
Z
There is a common abbreviation for an offset of zero: a simple Z
letter, meaning UTC, and pronounced “Zulu”. Example: 2019-02-26T00:44:28Z
The Z
is quite commonly used. But if for some reason you prefer the numeric +00:00
, use DateTimeFormatter
options.
COLON character
By the way, you mentioned the COLON character omitted from the offset: +0000
. That is actually tolerated by the ISO 8601 standard. But I do not advise it. I have seen libraries and systems break on such inputs. Best to use full-length, hours and minutes, padding zeros, and the colon character: +00:00
rather than +0000
, and -07:00
rather than -7
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.