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 
  24 /*
  25  * @test
  26  * @bug 8139572
  27  * @summary SimpleDateFormat parse month stand-alone format bug
  28  * @compile -encoding utf-8 Bug8139572.java
  29  * @modules jdk.localedata
  30  * @run main Bug8139572
  31  */
  32 import java.text.ParseException;
  33 import java.text.SimpleDateFormat;
  34 import java.util.Calendar;
  35 import java.util.Date;
  36 import java.util.GregorianCalendar;
  37 import java.util.Locale;
  38 
  39 public class Bug8139572 {
  40 
  41     private static final Locale RUSSIAN = new Locale("ru");
  42     private static final Date SEPT12 = new GregorianCalendar(2015, Calendar.SEPTEMBER, 12).getTime();
  43 
  44     private static final String[] PATTERNS = {
  45         "L",
  46         "dd L",
  47         "dd L yy",
  48         "dd L yyyy",
  49         "LL",
  50         "dd LL",
  51         "dd LL yy",
  52         "dd LL yyyy",
  53         "LLL",
  54         "dd LLL",
  55         "dd LLL yy",
  56         "dd LLL yyyy",
  57         "LLLL",
  58         "dd LLLL",
  59         "dd LLLL yy",
  60         "dd LLLL yyyy"
  61     };
  62 
  63     private static final String[] APPLIED = {
  64         "9",
  65         "12 09",
  66         "12 09 15",
  67         "12 09 2015",
  68         "09",
  69         "12 09",
  70         "12 09 15",
  71         "12 09 2015",
  72         "сентября",
  73         "12 сентября",
  74         "12 сентября 15",
  75         "12 сентября 2015",
  76         "сентября",
  77         "12 сентября",
  78         "12 сентября 15",
  79         "12 сентября 2015"
  80     };
  81 
  82     private static final String[] EXPECTED = {
  83         "9",
  84         "12 9",
  85         "12 9 15",
  86         "12 9 2015",
  87         "09",
  88         "12 09",
  89         "12 09 15",
  90         "12 09 2015",
  91         "сент.",
  92         "12 сент.",
  93         "12 сент. 15",
  94         "12 сент. 2015",
  95         "сентябрь",
  96         "12 сентябрь",
  97         "12 сентябрь 15",
  98         "12 сентябрь 2015"
  99     };
 100 
 101     public static void main(String[] args) throws ParseException {
 102 
 103         for (int i = 0; i < PATTERNS.length; i++) {
 104             SimpleDateFormat fmt = new SimpleDateFormat(PATTERNS[i], RUSSIAN);
 105             Date standAloneDate = fmt.parse(APPLIED[i]);
 106             String str = fmt.format(standAloneDate);
 107             if (!EXPECTED[i].equals(str)) {
 108                 throw new RuntimeException("bad result: got '" + str + "', expected '" + EXPECTED[i] + "'");
 109             }
 110         }
 111 
 112         SimpleDateFormat fmt = new SimpleDateFormat("", RUSSIAN);
 113         for (int j = 0; j < PATTERNS.length; j++) {
 114             fmt.applyPattern(PATTERNS[j]);
 115             String str = fmt.format(SEPT12);
 116             if (!EXPECTED[j].equals(str)) {
 117                 throw new RuntimeException("bad result: got '" + str + "', expected '" + EXPECTED[j] + "'");
 118             }
 119         }
 120     }
 121 }