Java Date Format

I have problems finding a java date template that reads this date correctly from a string:

2012-01-17T11:53:40+00:00

If the time zone is standard (+0000), this pattern will work:

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

but this is not so. Small zalso does not match.

+5
source share
4 answers

In Java 7, you can use a letter Xto represent the ISO 8601 time zone .

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
+3
source

Replace the last colon with an empty string, and then parse. The simplest solutions are sometimes the best.

+6
source

, .

, , .
:

String input = "2012-01-17T11:53:40+00:00";
input = input.replaceAll(":(..)$", "$1"); // lose the last colon
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(input);
+2

, +00:00 , ISO 8601. , Java, /.

java.time

Java 8 java.time, .

--UTC, . , OffsetDateTime.

ISO 8601, java.time ISO 8601, . OffsetDateTime . .

String input = "2012-01-17T11:53:40+00:00";
OffsetDateTime odt = OffsetDateTime.parse ( input );

.

System.out.println ( "input: " + input + " odt: " + odt );

: 2012-01-17T11: 53: 40 + 00: 00 odt: 2012-01-17T11: 53: 40Z

Z toString OffsetDateTime. , Z Zulu, offset-from-UTC , +00:00.

, OffsetDateTime , ZonedDateTime.

ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = odt.atZoneSameInstant ( zoneId );

OffsetDateTime ZonedDateTime . , , . "6:53 ", ( UTC) "11:53 ", .

.

System.out.println ( "input: " + input + " odt: " + odt + " zdt: " + zdt );

input: 2012-01-17T11: 53: 40 + 00: 00 odt: 2012-01-17T11: 53: 40Z zdt: 2012-01-17T06: 53: 40-05: 00 [/]

, java.time ISO 8601, .

0

All Articles