1 /* 2 * Copyright (c) 2015, 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.time.temporal.ChronoField; 34 import java.time.temporal.ChronoUnit; 35 36 import org.testng.annotations.BeforeMethod; 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 private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin"); 50 private static final ZoneId ASIA_ISTANBUL = ZoneId.of("Asia/Istanbul"); 51 private static final ZoneId[] ZONE_IDS = { EUROPE_BERLIN, ASIA_ISTANBUL }; 52 private static DateTimeFormatter dtFormatter; 53 private static ZonedDateTime zdt1, zdt2; 54 private static OffsetDateTime odt1, odt2; 55 private static LocalDateTime ldt1, ldt2; 56 private static String s; 57 58 @BeforeMethod 59 public void setUp() throws Exception { 60 dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; 61 } 62 63 @Test 64 private static void testWithoutZoneAndOffset() { 65 dtFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; 66 ldt1 = LocalDateTime.of(2012, 10, 28, 1, 45, 0); 67 for (int i = 0; i < 6; i++) { 68 s = ldt1.format(dtFormatter); 69 ldt2 = LocalDateTime.parse(s, dtFormatter); 70 assertEquals(ldt1, ldt2); 71 ldt1 = ldt1.plus(15, ChronoUnit.MINUTES); 72 } 73 } 74 75 @Test 76 private static void testWithZoneAndOffset() { 77 dtFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; 78 // Sample Offsets to test from -2 to +3 79 String[] offsetSamples = { "-02:00", "-01:00", "-00:00", "+00:00", "+01:00", "+02:00", "+03:00" }; 80 // 3 Sample ZDT Strings which are before, during and after an Overlap period respectively. 81 String[] zdtStringSamples = { "2012-10-28T01:45:00+00:00[Europe/Berlin]", 82 "2012-10-28T02:45:00+00:00[Europe/Berlin]", 83 "2012-10-28T03:45:00+00:00[Europe/Berlin]", 84 "2012-10-28T02:45:00+00:00[Asia/Istanbul]", 85 "2012-10-28T03:45:00+00:00[Asia/Istanbul]", 86 "2012-10-28T04:45:00+00:00[Asia/Istanbul]" 87 }; 88 // 3 Sample ZDTs which are before, during and after an Overlap period respectively. 89 LocalDateTime[] ldtSamples = { LocalDateTime.of(2012, 10, 28, 1, 45, 0, 0), 90 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), 91 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), 92 LocalDateTime.of(2012, 10, 28, 2, 45, 0, 0), 93 LocalDateTime.of(2012, 10, 28, 3, 45, 0, 0), 94 LocalDateTime.of(2012, 10, 28, 4, 45, 0, 0) 95 }; 96 for (int i = 0; i < zdtStringSamples.length; i++) { 97 for (int j = 0; j < offsetSamples.length; j++) { 98 zdt1 = ZonedDateTime.ofInstant(ldtSamples[i], ZoneOffset.of(offsetSamples[j]), ZONE_IDS[i / 3]); 99 s = zdtStringSamples[i].replace(zdtStringSamples[i].substring(19, 25), offsetSamples[j]); 100 zdt2 = ZonedDateTime.parse(s, dtFormatter); 101 assertEquals(zdt1, zdt2); 102 } 103 } 104 } 105 106 @Test 107 private static void testWithoutOffset() { 108 dtFormatter = DateTimeFormatter.ofPattern("d MMM HH:mm:ss uuuu VV"); 109 zdt1 = ZonedDateTime.of(2012, 10, 28, 2, 45, 0, 0, ZoneId.of("Europe/Berlin")); 110 String s1; 111 for (int i = 0; i < 6; i++) { 112 s = zdt1.format(dtFormatter); 113 zdt2 = ZonedDateTime.parse(s, dtFormatter); 114 s1 = zdt2.format(dtFormatter); 115 assertEquals(s, s1); 116 zdt1 = zdt1.plus(15, ChronoUnit.MINUTES); 117 } 118 } 119 120 @Test 121 private static void testWithoutZone() { 122 dtFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; 123 // Sample Offsets 124 String[] offsetSamples = { "-08:00", "-05:00", "-01:00", "+00:00", "+01:00", "+04:00", "+10:00" }; 125 // 3 Sample ODT Strings 126 String[] odtStringSamples = { "2012-10-28T01:45:00+00:00", "2012-10-28T02:45:00+00:00", "2012-10-28T03:45:00+00:00" }; 127 // 3 Sample ODTs 128 OffsetDateTime[] odtSamples = { 129 OffsetDateTime.of(2012, 10, 28, 1, 45, 0, 0, ZoneOffset.of(offsetSamples[0])), 130 OffsetDateTime.of(2012, 10, 28, 2, 45, 0, 0, ZoneOffset.of(offsetSamples[0])), 131 OffsetDateTime.of(2012, 10, 28, 3, 45, 0, 0, ZoneOffset.of(offsetSamples[0])) 132 }; 133 for (int i = 0; i < odtStringSamples.length; i++) { 134 for (int j = 0; j < offsetSamples.length; j++) { 135 odt1 = odtSamples[i].with(ChronoField.OFFSET_SECONDS, ZoneOffset.of(offsetSamples[j]).getTotalSeconds()); 136 s = odtStringSamples[i].replace(odtStringSamples[i].substring(19, 25), offsetSamples[j]); 137 odt2 = OffsetDateTime.parse(s, dtFormatter); 138 assertEquals(odt1, odt2); 139 } 140 } 141 } 142 }