1 /*
2 * Copyright (c) 2012, 2013, 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 */
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time.format;
61
62 import static java.time.format.DateTimeFormatter.BASIC_ISO_DATE;
63 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
64 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
65 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
66 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
67 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
68 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
69 import static java.time.temporal.ChronoField.YEAR;
70 import static org.testng.Assert.assertEquals;
71
72 import java.text.ParsePosition;
73 import java.time.LocalDate;
74 import java.time.YearMonth;
75 import java.time.ZoneOffset;
76 import java.time.format.DateTimeFormatter;
77 import java.time.format.DateTimeFormatterBuilder;
78 import java.time.format.DateTimeParseException;
79 import java.time.format.SignStyle;
80 import java.time.format.TextStyle;
81 import java.time.temporal.Temporal;
82 import java.time.temporal.TemporalAccessor;
83 import java.util.HashMap;
84 import java.util.Locale;
85 import java.util.Map;
86
87 import org.testng.annotations.BeforeMethod;
88 import org.testng.annotations.DataProvider;
89 import org.testng.annotations.Test;
90
91 /**
92 * Test DateTimeFormatterBuilder.
93 */
865 TemporalAccessor parsed = f.parseUnresolved("1230", pp);
866 assertEquals(pp.getErrorIndex(), -1);
867 assertEquals(pp.getIndex(), 4);
868 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
869 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
870 }
871
872 @Test
873 public void test_adjacent_lenient_fractionFollows() throws Exception {
874 // succeeds because hour/min are fixed width
875 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
876 ParsePosition pp = new ParsePosition(0);
877 TemporalAccessor parsed = f.parseUnresolved("1230567", pp);
878 assertEquals(pp.getErrorIndex(), -1);
879 assertEquals(pp.getIndex(), 7);
880 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
881 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
882 assertEquals(parsed.getLong(NANO_OF_SECOND), 567_000_000L);
883 }
884
885 @Test
886 public void test_adjacent_lenient_fractionFollows_2digit() throws Exception {
887 // succeeds because hour/min are fixed width
888 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
889 ParsePosition pp = new ParsePosition(0);
890 TemporalAccessor parsed = f.parseUnresolved("123056", pp);
891 assertEquals(pp.getErrorIndex(), -1);
892 assertEquals(pp.getIndex(), 6);
893 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
894 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
895 assertEquals(parsed.getLong(NANO_OF_SECOND), 560_000_000L);
896 }
897
898 @Test
899 public void test_adjacent_lenient_fractionFollows_0digit() throws Exception {
900 // succeeds because hour/min are fixed width
901 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
902 ParsePosition pp = new ParsePosition(0);
903 TemporalAccessor parsed = f.parseUnresolved("1230", pp);
904 assertEquals(pp.getErrorIndex(), -1);
905 assertEquals(pp.getIndex(), 4);
906 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
907 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
908 }
909
910 @DataProvider(name="lenientOffsetParseData")
911 Object[][] data_lenient_offset_parse() {
912 return new Object[][] {
913 {"+HH", "+01", 3600},
914 {"+HH", "+0101", 3660},
915 {"+HH", "+010101", 3661},
916 {"+HH", "+01", 3600},
917 {"+HH", "+01:01", 3660},
918 {"+HH", "+01:01:01", 3661},
919 {"+HHmm", "+01", 3600},
920 {"+HHmm", "+0101", 3660},
921 {"+HHmm", "+010101", 3661},
922 {"+HH:mm", "+01", 3600},
923 {"+HH:mm", "+01:01", 3660},
924 {"+HH:mm", "+01:01:01", 3661},
925 {"+HHMM", "+01", 3600},
926 {"+HHMM", "+0101", 3660},
927 {"+HHMM", "+010101", 3661},
|
1 /*
2 * Copyright (c) 2012, 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 */
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time.format;
61
62 import static java.time.format.DateTimeFormatter.BASIC_ISO_DATE;
63 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
64 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
65 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
66 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
67 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
68 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
69 import static java.time.temporal.ChronoField.YEAR;
70 import static org.testng.Assert.assertEquals;
71
72 import java.text.ParsePosition;
73 import java.time.LocalDate;
74 import java.time.LocalDateTime;
75 import java.time.YearMonth;
76 import java.time.ZoneOffset;
77 import java.time.format.DateTimeFormatter;
78 import java.time.format.DateTimeFormatterBuilder;
79 import java.time.format.DateTimeParseException;
80 import java.time.format.SignStyle;
81 import java.time.format.TextStyle;
82 import java.time.temporal.Temporal;
83 import java.time.temporal.TemporalAccessor;
84 import java.util.HashMap;
85 import java.util.Locale;
86 import java.util.Map;
87
88 import org.testng.annotations.BeforeMethod;
89 import org.testng.annotations.DataProvider;
90 import org.testng.annotations.Test;
91
92 /**
93 * Test DateTimeFormatterBuilder.
94 */
866 TemporalAccessor parsed = f.parseUnresolved("1230", pp);
867 assertEquals(pp.getErrorIndex(), -1);
868 assertEquals(pp.getIndex(), 4);
869 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
870 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
871 }
872
873 @Test
874 public void test_adjacent_lenient_fractionFollows() throws Exception {
875 // succeeds because hour/min are fixed width
876 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
877 ParsePosition pp = new ParsePosition(0);
878 TemporalAccessor parsed = f.parseUnresolved("1230567", pp);
879 assertEquals(pp.getErrorIndex(), -1);
880 assertEquals(pp.getIndex(), 7);
881 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
882 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
883 assertEquals(parsed.getLong(NANO_OF_SECOND), 567_000_000L);
884 }
885
886 @Test(expectedExceptions=NullPointerException.class)
887 public void test_adjacent_lenient_fractionFollows_2digit() throws Exception {
888 // fails because hour,min and fraction of seconds are fixed width
889 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
890 ParsePosition pp = new ParsePosition(0);
891 TemporalAccessor parsed = f.parseUnresolved("123056", pp);
892 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
893 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
894 assertEquals(parsed.getLong(NANO_OF_SECOND), 560_000_000L); // fails here
895 }
896
897 @Test(expectedExceptions=NullPointerException.class)
898 public void test_adjacent_lenient_fractionFollows_0digit() throws Exception {
899 // fails because hour, min and fraction of seconds are fixed width
900 DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
901 ParsePosition pp = new ParsePosition(0);
902 TemporalAccessor parsed = f.parseUnresolved("1230", pp);
903 assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
904 assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
905 assertEquals(parsed.getLong(NANO_OF_SECOND), 0L); // fails here
906 }
907
908 @DataProvider(name="adjacentFractionParseData")
909 Object[][] data_adjacent_fraction_parse() {
910 return new Object[][] {
911 {"20130812214600025", "yyyyMMddHHmmssSSS", LocalDateTime.of(2013, 8, 12, 21, 46, 00, 25000000)},
912 {"201308122146000256", "yyyyMMddHHmmssSSSS", LocalDateTime.of(2013, 8, 12, 21, 46, 00, 25600000)},
913 };
914 }
915
916 @Test(dataProvider = "adjacentFractionParseData")
917 public void test_adjacent_fraction(String input, String pattern, LocalDateTime expected) {
918 DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
919 LocalDateTime actual = LocalDateTime.parse(input, dtf);
920 assertEquals(actual, expected);
921 }
922
923 @DataProvider(name="lenientOffsetParseData")
924 Object[][] data_lenient_offset_parse() {
925 return new Object[][] {
926 {"+HH", "+01", 3600},
927 {"+HH", "+0101", 3660},
928 {"+HH", "+010101", 3661},
929 {"+HH", "+01", 3600},
930 {"+HH", "+01:01", 3660},
931 {"+HH", "+01:01:01", 3661},
932 {"+HHmm", "+01", 3600},
933 {"+HHmm", "+0101", 3660},
934 {"+HHmm", "+010101", 3661},
935 {"+HH:mm", "+01", 3600},
936 {"+HH:mm", "+01:01", 3660},
937 {"+HH:mm", "+01:01:01", 3661},
938 {"+HHMM", "+01", 3600},
939 {"+HHMM", "+0101", 3660},
940 {"+HHMM", "+010101", 3661},
|