0

I have a Date String in ISO format with Time Zone. I have to convert it into Timestamp with +/- Zone into time using Java 8.0 E.g.

2016-10-14T10:12:18.000+02:00 needs to be converted to 2016-10-14T08:12:18.000+00:00 or 2016-10-14T8:12:18.000Z

String utcDate = "2016-10-14T10:12:18.100+02:00";
ZonedDateTime result = ZonedDateTime.parse(utcDate, DateTimeFormatter.ISO_DATE_TIME);
System.out.println("Parsed: " + result);

Output:

Parsed: 2016-10-14T10:12:18.100+02:00

It doesn't add the zone +02:00 to Date.

3
  • 3
    For a start, I think you've misunderstood what "UTC" means - it's not a format... it's a frame of reference, mostly used for time zones. The string "2016-10-14T10:12:18.000+02:00" has an offset of 2 hours from UTC. Commented Mar 17, 2017 at 15:30
  • My Bad. I mean ISO format with time zones. I need to convert it to UTC with +/- offset. Commented Mar 17, 2017 at 15:38
  • @AtulKumar Please edit your Question to fix your mistake, and add clarity. Please explain what you mean by "SQL Timestamp" as your example code seems unrelated. Commented Mar 17, 2017 at 18:19

1 Answer 1

1

Well currently you're not doing anything to convert the value - you're using a ZonedDateTime, so it's preserving the time zone (at least, the offset) that was provided. (I'd use OffsetDateTime for this rather than ZonedDateTime, as you don't actually have a time zone.)

You could either use withZoneSameInstant:

ZonedDateTime utc = result.withZoneSameInstant(ZoneOffset.UTC);

Or just convert to an Instant:

Instant instant = result.toInstant();

(and then format appropriately, of course).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.