< prev index next >

test/java/time/tck/java/time/format/TCKDateTimeFormatterBuilder.java

Print this page


   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  */


 615             {"xx"},
 616             {"xxx"},
 617             {"xxxx"},
 618             {"xxxxx"},
 619 
 620             {"ppH"},
 621             {"pppDD"},
 622 
 623             {"yyyy[-MM[-dd"},
 624             {"yyyy[-MM[-dd]]"},
 625             {"yyyy[-MM[]-dd]"},
 626 
 627             {"yyyy-MM-dd'T'HH:mm:ss.SSS"},
 628 
 629             {"e"},
 630             {"w"},
 631             {"ww"},
 632             {"W"},
 633             {"W"},
 634 


 635         };
 636     }
 637 
 638     @Test(dataProvider="validPatterns")
 639     public void test_appendPattern_valid(String input) throws Exception {
 640         builder.appendPattern(input);  // test is for no error here
 641     }
 642 
 643     //-----------------------------------------------------------------------
 644     @DataProvider(name="invalidPatterns")
 645     Object[][] dataInvalid() {
 646         return new Object[][] {
 647             {"'"},
 648             {"'hello"},
 649             {"'hel''lo"},
 650             {"'hello''"},
 651             {"{"},
 652             {"}"},
 653             {"{}"},
 654             {"#"},


 716             {"Q", date(2012, 2, 10), "1"},
 717             {"QQ", date(2012, 2, 10), "01"},
 718             {"QQQ", date(2012, 2, 10), "Q1"},
 719             {"QQQQ", date(2012, 2, 10), "1st quarter"},
 720             {"QQQQQ", date(2012, 2, 10), "1"},
 721         };
 722     }
 723 
 724     @Test(dataProvider="patternPrint")
 725     public void test_appendPattern_patternPrint(String input, Temporal temporal, String expected) throws Exception {
 726         DateTimeFormatter f = builder.appendPattern(input).toFormatter(Locale.UK);
 727         String test = f.format(temporal);
 728         assertEquals(test, expected);
 729     }
 730 
 731     private static Temporal date(int y, int m, int d) {
 732         return LocalDate.of(y, m, d);
 733     }
 734 
 735     //-----------------------------------------------------------------------































 736     @Test
 737     public void test_adjacent_strict_firstFixedWidth() throws Exception {
 738         // succeeds because both number elements are fixed width
 739         DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
 740         ParsePosition pp = new ParsePosition(0);
 741         TemporalAccessor parsed = f.parseUnresolved("12309", pp);
 742         assertEquals(pp.getErrorIndex(), -1);
 743         assertEquals(pp.getIndex(), 5);
 744         assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
 745         assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
 746     }
 747 
 748     @Test
 749     public void test_adjacent_strict_firstVariableWidth_success() throws Exception {
 750         // succeeds greedily parsing variable width, then fixed width, to non-numeric Z
 751         DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK);
 752         ParsePosition pp = new ParsePosition(0);
 753         TemporalAccessor parsed = f.parseUnresolved("12309Z", pp);
 754         assertEquals(pp.getErrorIndex(), -1);
 755         assertEquals(pp.getIndex(), 6);


   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  */


 615             {"xx"},
 616             {"xxx"},
 617             {"xxxx"},
 618             {"xxxxx"},
 619 
 620             {"ppH"},
 621             {"pppDD"},
 622 
 623             {"yyyy[-MM[-dd"},
 624             {"yyyy[-MM[-dd]]"},
 625             {"yyyy[-MM[]-dd]"},
 626 
 627             {"yyyy-MM-dd'T'HH:mm:ss.SSS"},
 628 
 629             {"e"},
 630             {"w"},
 631             {"ww"},
 632             {"W"},
 633             {"W"},
 634 
 635             {"g"},
 636             {"ggggg"},
 637         };
 638     }
 639 
 640     @Test(dataProvider="validPatterns")
 641     public void test_appendPattern_valid(String input) throws Exception {
 642         builder.appendPattern(input);  // test is for no error here
 643     }
 644 
 645     //-----------------------------------------------------------------------
 646     @DataProvider(name="invalidPatterns")
 647     Object[][] dataInvalid() {
 648         return new Object[][] {
 649             {"'"},
 650             {"'hello"},
 651             {"'hel''lo"},
 652             {"'hello''"},
 653             {"{"},
 654             {"}"},
 655             {"{}"},
 656             {"#"},


 718             {"Q", date(2012, 2, 10), "1"},
 719             {"QQ", date(2012, 2, 10), "01"},
 720             {"QQQ", date(2012, 2, 10), "Q1"},
 721             {"QQQQ", date(2012, 2, 10), "1st quarter"},
 722             {"QQQQQ", date(2012, 2, 10), "1"},
 723         };
 724     }
 725 
 726     @Test(dataProvider="patternPrint")
 727     public void test_appendPattern_patternPrint(String input, Temporal temporal, String expected) throws Exception {
 728         DateTimeFormatter f = builder.appendPattern(input).toFormatter(Locale.UK);
 729         String test = f.format(temporal);
 730         assertEquals(test, expected);
 731     }
 732 
 733     private static Temporal date(int y, int m, int d) {
 734         return LocalDate.of(y, m, d);
 735     }
 736 
 737     //-----------------------------------------------------------------------
 738     @DataProvider(name="modJulianFieldPattern")
 739     Object[][] data_modJuilanFieldPattern() {
 740         return new Object[][] {
 741             {"g", "1"},
 742             {"g", "123456"},
 743             {"gggggg", "123456"},
 744         };
 745     }
 746 
 747     @Test(dataProvider="modJulianFieldPattern")
 748     public void test_modJulianFieldPattern(String pattern, String input) throws Exception {
 749         DateTimeFormatter.ofPattern(pattern).parse(input);
 750     }
 751 
 752     @DataProvider(name="modJulianFieldValues")
 753     Object[][] data_modJuilanFieldValues() {
 754         return new Object[][] {
 755             {1970, 1, 1, "40587"},
 756             {1858, 11, 17, "0"},
 757             {1858, 11, 16, "-1"},
 758         };
 759     }
 760 
 761     @Test(dataProvider="modJulianFieldValues")
 762     public void test_modJulianFieldValues(int y, int m, int d, String expected) throws Exception {
 763         DateTimeFormatter df = new DateTimeFormatterBuilder().appendPattern("g").toFormatter();
 764          assertEquals(LocalDate.of(y, m, d).format(df), expected);
 765     }
 766 
 767 
 768     //-----------------------------------------------------------------------
 769     @Test
 770     public void test_adjacent_strict_firstFixedWidth() throws Exception {
 771         // succeeds because both number elements are fixed width
 772         DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
 773         ParsePosition pp = new ParsePosition(0);
 774         TemporalAccessor parsed = f.parseUnresolved("12309", pp);
 775         assertEquals(pp.getErrorIndex(), -1);
 776         assertEquals(pp.getIndex(), 5);
 777         assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
 778         assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
 779     }
 780 
 781     @Test
 782     public void test_adjacent_strict_firstVariableWidth_success() throws Exception {
 783         // succeeds greedily parsing variable width, then fixed width, to non-numeric Z
 784         DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK);
 785         ParsePosition pp = new ParsePosition(0);
 786         TemporalAccessor parsed = f.parseUnresolved("12309Z", pp);
 787         assertEquals(pp.getErrorIndex(), -1);
 788         assertEquals(pp.getIndex(), 6);


< prev index next >