from java 11 doc for ISO_OFFSET_DATE_TIME
The ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30+01:00'.
- But when i use a DateTimeFormatter with the above formatting i am seeing different output.
- Setting timezone for DateTimeFormatter seems to have no effect.
Below code should clarify it -
public void j8DateTimeWithFormatter() {
DateTimeFormatter odtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String zdt = ZonedDateTime.now().format(odtf);
System.out.println(zdt); // Output 2021-06-06T22:44:28.4410102+05:30 //what is this 4410102, is it nano seconds or some thing else. How to not see this.
//further
odtf.withZone(ZoneId.of("US/Eastern"));
zdt = ZonedDateTime.now().format(odtf);
//after setting the zoneid to EST why am i still seeing time in IST
System.out.println(zdt); // Output 2021-06-06T22:44:28.4430055+05:30
}
How to fix these? Please advice. I want to still use ISO_OFFSET_DATE_TIME
and see the output as in docs - 2021-06-06T22:44:28-04:00