1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package tck.java.time.format; 24 25 import static org.testng.AssertJUnit.assertEquals; 26 27 import java.time.LocalDateTime; 28 import java.time.OffsetDateTime; 29 import java.time.ZoneId; 30 import java.time.ZoneOffset; 31 import java.time.ZonedDateTime; 32 import java.time.format.DateTimeFormatter; 33 import java.util.Locale; 34 35 import org.testng.annotations.BeforeMethod; 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 /** 40 * Testing DateTimeFormatter Parsing with 4 different test conditions: 41 * 1. When Zone and Offset not provided 42 * 2. When Zone and Offset provided 43 * 3. When Offset is not provided and Zone is provided 44 * 4. When Zone is not provided and Offset is provided 45 */ 46 47 @Test 48 public class TCKDTFParsedInstant { 49 50 private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin"); 51 private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul"); 52 53 private DateTimeFormatter dtFormatter; 54 private ZonedDateTime zdt1, zdt2; 55 private LocalDateTime ldt1; 56 private OffsetDateTime odt1; 57 58 @BeforeMethod setUp()59 public void setUp() throws Exception { 60 dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; 61 } 62 63 @DataProvider(name="parseWithoutZoneWithoutOffset") data_parse_WithoutOffset_WithoutZone()64 Object[][] data_parse_WithoutOffset_WithoutZone() { 65 return new Object[][] { 66 {"1966-12-31T00:01:10", LocalDateTime.of(1966, 12, 31, 0, 1, 10)}, 67 {"1970-01-01T00:00:00", LocalDateTime.of(1970, 1, 1, 0, 0, 0)}, 68 {"2004-02-29T00:30:00", LocalDateTime.of(2004, 2, 29, 0, 30, 0)}, 69 {"2015-12-31T23:59:59", LocalDateTime.of(2015, 12, 31, 23, 59, 59)} 70 }; 71 } 72 73 @Test(dataProvider="parseWithoutZoneWithoutOffset") testWithoutZoneWithoutOffset(String ldtString, LocalDateTime expectedLDT)74 public void testWithoutZoneWithoutOffset(String ldtString, LocalDateTime expectedLDT) { 75 dtFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; 76 ldt1 = LocalDateTime.parse(ldtString, dtFormatter); 77 assertEquals(expectedLDT, ldt1); 78 } 79 80 @DataProvider(name="parseWithZoneWithOffset") data_parse_WithZone_WithOffset()81 Object[][] data_parse_WithZone_WithOffset() { 82 return new Object[][] { 83 {"2012-10-28T01:45:00-02:30[Europe/Berlin]", 84 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN}, 85 {"2012-10-28T01:45:00-01:00[Europe/Berlin]", 86 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN}, 87 {"2012-10-28T01:45:00-00:00[Europe/Berlin]", 88 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN}, 89 {"2012-10-28T01:45:00+00:00[Europe/Berlin]", 90 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN}, 91 {"2012-10-28T01:45:00+01:00[Europe/Berlin]", 92 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN}, 93 {"2012-10-28T01:45:00+02:00[Europe/Berlin]", 94 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN}, 95 {"2012-10-28T01:45:00+03:00[Europe/Berlin]", 96 LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN}, 97 {"2012-10-28T02:45:00-02:30[Europe/Berlin]", 98 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN}, 99 {"2012-10-28T02:45:00-01:00[Europe/Berlin]", 100 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN}, 101 {"2012-10-28T02:45:00-00:00[Europe/Berlin]", 102 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN}, 103 {"2012-10-28T02:45:00+00:00[Europe/Berlin]", 104 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN}, 105 {"2012-10-28T02:45:00+01:00[Europe/Berlin]", 106 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN}, 107 {"2012-10-28T02:45:00+02:00[Europe/Berlin]", 108 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN}, 109 {"2012-10-28T02:45:00+03:00[Europe/Berlin]", 110 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN}, 111 {"2012-10-28T03:45:00-02:30[Europe/Berlin]", 112 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), EUROPE_BERLIN}, 113 {"2012-10-28T03:45:00-01:00[Europe/Berlin]", 114 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), EUROPE_BERLIN}, 115 {"2012-10-28T03:45:00-00:00[Europe/Berlin]", 116 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), EUROPE_BERLIN}, 117 {"2012-10-28T03:45:00+00:00[Europe/Berlin]", 118 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), EUROPE_BERLIN}, 119 {"2012-10-28T03:45:00+01:00[Europe/Berlin]", 120 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), EUROPE_BERLIN}, 121 {"2012-10-28T03:45:00+02:00[Europe/Berlin]", 122 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), EUROPE_BERLIN}, 123 {"2012-10-28T03:45:00+03:00[Europe/Berlin]", 124 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), EUROPE_BERLIN}, 125 126 {"2012-10-28T02:45:00-02:30[Asia/Istanbul]", 127 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL}, 128 {"2012-10-28T02:45:00-01:00[Asia/Istanbul]", 129 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL}, 130 {"2012-10-28T02:45:00-00:00[Asia/Istanbul]", 131 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL}, 132 {"2012-10-28T02:45:00+00:00[Asia/Istanbul]", 133 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL}, 134 {"2012-10-28T02:45:00+01:00[Asia/Istanbul]", 135 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL}, 136 {"2012-10-28T02:45:00+02:00[Asia/Istanbul]", 137 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL}, 138 {"2012-10-28T02:45:00+03:00[Asia/Istanbul]", 139 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL}, 140 {"2012-10-28T03:45:00-02:30[Asia/Istanbul]", 141 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL}, 142 {"2012-10-28T03:45:00-01:00[Asia/Istanbul]", 143 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL}, 144 {"2012-10-28T03:45:00-00:00[Asia/Istanbul]", 145 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL}, 146 {"2012-10-28T03:45:00+00:00[Asia/Istanbul]", 147 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL}, 148 {"2012-10-28T03:45:00+01:00[Asia/Istanbul]", 149 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL}, 150 {"2012-10-28T03:45:00+02:00[Asia/Istanbul]", 151 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL}, 152 {"2012-10-28T03:45:00+03:00[Asia/Istanbul]", 153 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL}, 154 {"2012-10-28T04:45:00-02:30[Asia/Istanbul]", 155 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-02:30"), ASIA_ISTANBUL}, 156 {"2012-10-28T04:45:00-01:00[Asia/Istanbul]", 157 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-01:00"), ASIA_ISTANBUL}, 158 {"2012-10-28T04:45:00-00:00[Asia/Istanbul]", 159 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("-00:00"), ASIA_ISTANBUL}, 160 {"2012-10-28T04:45:00+00:00[Asia/Istanbul]", 161 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+00:00"), ASIA_ISTANBUL}, 162 {"2012-10-28T04:45:00+01:00[Asia/Istanbul]", 163 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+01:00"), ASIA_ISTANBUL}, 164 {"2012-10-28T04:45:00+02:00[Asia/Istanbul]", 165 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+02:00"), ASIA_ISTANBUL}, 166 {"2012-10-28T04:45:00+03:00[Asia/Istanbul]", 167 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0), ZoneOffset.of("+03:00"), ASIA_ISTANBUL} 168 }; 169 } 170 171 @Test(dataProvider="parseWithZoneWithOffset") testWithZoneWithOffset(String zdtString, LocalDateTime ldt, ZoneOffset offset, ZoneId zone)172 public void testWithZoneWithOffset(String zdtString, LocalDateTime ldt, ZoneOffset offset, ZoneId zone) { 173 dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; 174 zdt1 = ZonedDateTime.ofInstant(ldt, offset, zone); 175 zdt2 = ZonedDateTime.parse(zdtString, dtFormatter); 176 assertEquals(zdt1, zdt2); 177 } 178 179 @DataProvider(name="parseWithZoneWithoutOffset") data_parse_WithZone_WithoutOffset()180 Object[][] data_parse_WithZone_WithoutOffset() { 181 return new Object[][] { 182 {"28 Oct 00:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 0, 45, 0, 0, EUROPE_BERLIN)}, 183 {"28 Oct 01:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, EUROPE_BERLIN)}, 184 {"28 Oct 02:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, EUROPE_BERLIN)}, 185 {"28 Oct 03:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, EUROPE_BERLIN)}, 186 {"28 Oct 04:45:00 2012 Europe/Berlin", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, EUROPE_BERLIN)}, 187 188 {"28 Oct 01:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 1, 45, 0, 0, ASIA_ISTANBUL)}, 189 {"28 Oct 02:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ASIA_ISTANBUL)}, 190 {"28 Oct 03:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 3, 45, 0, 0, ASIA_ISTANBUL)}, 191 {"28 Oct 04:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 4, 45, 0, 0, ASIA_ISTANBUL)}, 192 {"28 Oct 05:45:00 2012 Asia/Istanbul", ZonedDateTime.of(2012, 10, 28, 5, 45, 0, 0, ASIA_ISTANBUL)} 193 }; 194 } 195 196 @Test(dataProvider="parseWithZoneWithoutOffset") testWithZoneWithoutOffset(String withZoneWithoutOffset, ZonedDateTime expectedZDT)197 public void testWithZoneWithoutOffset(String withZoneWithoutOffset, ZonedDateTime expectedZDT) { 198 dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV").withLocale(Locale.ENGLISH); 199 zdt1 = ZonedDateTime.parse(withZoneWithoutOffset, dtFormatter); 200 assertEquals(expectedZDT, zdt1); 201 } 202 203 @DataProvider(name="parseWithOffsetWithoutZone") data_parse_WithOffset_WithoutZone()204 Object[][] data_parse_WithOffset_WithoutZone() { 205 return new Object[][] { 206 {"2015-12-14T00:45:00-11:30", OffsetDateTime.of(2015, 12, 14, 0, 45, 0, 0, ZoneOffset.of("-11:30"))}, 207 {"2015-12-14T01:45:00-05:00", OffsetDateTime.of(2015, 12, 14, 1, 45, 0, 0, ZoneOffset.of("-05:00"))}, 208 {"2015-12-14T02:45:00-00:00", OffsetDateTime.of(2015, 12, 14, 2, 45, 0, 0, ZoneOffset.of("-00:00"))}, 209 {"2015-12-14T03:45:00+00:00", OffsetDateTime.of(2015, 12, 14, 3, 45, 0, 0, ZoneOffset.of("+00:00"))}, 210 {"2015-12-14T04:45:00+03:30", OffsetDateTime.of(2015, 12, 14, 4, 45, 0, 0, ZoneOffset.of("+03:30"))}, 211 {"2015-12-14T05:45:00+10:00", OffsetDateTime.of(2015, 12, 14, 5, 45, 0, 0, ZoneOffset.of("+10:00"))} 212 }; 213 } 214 215 @Test(dataProvider="parseWithOffsetWithoutZone") testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD)216 public void testWithOffsetWithoutZone(String odtString, OffsetDateTime expectedOTD) { 217 dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; 218 odt1 = OffsetDateTime.parse(odtString, dtFormatter); 219 assertEquals(expectedOTD, odt1); 220 } 221 } 222