import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import org.junit.Test; 
import static org.junit.Assert.assertEquals; 

/** 
 * Testing the following paragraph from {@link DateTimeFormatter#withZone(ZoneId)}: 
 * <p> 
 * When parsing, there are two distinct cases to consider. 
 * If a zone has been parsed directly from the text, perhaps because 
 * {@link DateTimeFormatterBuilder#appendZoneId()} was used, then 
 * this override zone has no effect. 
 * If no zone has been parsed, then this override zone will be included in 
 * the result of the parse where it can be used to build instants and date-times. 
 */ 

public class JI9048046 {
	// Parser with optional time zone, which should use UTC if zone is missing 
	DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm[Z]").withZone(ZoneOffset.UTC); 
	// These two strings represent the same instant in time 
	String withoutTz = "2001-01-01T01:00"; 
	String withTz = "2001-01-01T02:00+0100"; 
	// And this is the correct Instant that they represent 
	Instant expected = Instant.parse("2001-01-01T01:00:00Z"); 

	@Test 
	public void testParseInstantWithOptionalZone() { 
		// This passes 
		assertEquals(expected, parser.parse(withoutTz, Instant::from)); 
		// This fails (the +01:00 offset is ignored, and the override zone of UTC is used instead) 
		assertEquals(expected, parser.parse(withTz, Instant::from)); 
		// java.lang.AssertionError: expected:<2001-01-01T01:00:00Z> but was:<2001-01-01T02:00:00Z> 
	} 

	@Test 
	public void testParseOffsetDateTimeWithOptionalZone() { 
		// This passes (Parsing as OffsetDateTime correctly preserves the offset from the string) 
		assertEquals(expected, parser.parse(withTz, OffsetDateTime::from).toInstant()); 
		// This fails (the override zone isn't used) 
		assertEquals(expected, parser.parse(withoutTz, OffsetDateTime::from).toInstant()); 
		// java.time.format.DateTimeParseException: Text '2001-01-01T01:00' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=978310800},ISO,Z resolved to 2001-01-01T01:00 of type java.time.format.Parsed 
	} 

}
