< prev index next >

src/java.base/share/classes/sun/util/calendar/LocalGregorianCalendar.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.security.AccessController;
  29 import java.util.TimeZone;

  30 
  31 /**
  32  *
  33  * @author Masayoshi Okutsu
  34  * @since 1.6
  35  */
  36 
  37 public class LocalGregorianCalendar extends BaseCalendar {
  38     private static final Era[] JAPANESE_ERAS = {
  39         new Era("Meiji",  "M", -3218832000000L, true),
  40         new Era("Taisho", "T", -1812153600000L, true),
  41         new Era("Showa",  "S", -1357603200000L, true),
  42         new Era("Heisei", "H",   600220800000L, true),
  43     };
  44 
  45     private static boolean isValidEra(Era newEra, Era[] eras) {
  46         Era last = eras[eras.length - 1];
  47         if (last.getSinceDate().getYear() >= newEra.getSinceDate().getYear()) {
  48             return false;
  49         }


 125                 String abbr = era.getAbbreviation();
 126                 if (abbr != null) {
 127                     sb.append(abbr);
 128                 }
 129             }
 130             sb.append(getYear()).append('.');
 131             CalendarUtils.sprintf0d(sb, getMonth(), 2).append('.');
 132             CalendarUtils.sprintf0d(sb, getDayOfMonth(), 2);
 133             sb.append(time);
 134             return sb.toString();
 135         }
 136     }
 137 
 138     static LocalGregorianCalendar getLocalGregorianCalendar(String name) {
 139         // Only the Japanese calendar is supported.
 140         if (!"japanese".equals(name)) {
 141             return null;
 142         }
 143 
 144         // Append an era to the predefined eras if it's given by the property.
 145         String prop = AccessController.doPrivileged(
 146                 new sun.security.action.GetPropertyAction("jdk.calendar.japanese.supplemental.era"));
 147         if (prop != null) {
 148             Era era = parseEraEntry(prop);
 149             if (era != null) {
 150                 if (isValidEra(era, JAPANESE_ERAS)) {
 151                     int length = JAPANESE_ERAS.length;
 152                     Era[] eras = new Era[length + 1];
 153                     System.arraycopy(JAPANESE_ERAS, 0, eras, 0, length);
 154                     eras[length] = era;
 155                     return new LocalGregorianCalendar(name, eras);
 156                 }
 157             }
 158         }
 159         return new LocalGregorianCalendar(name, JAPANESE_ERAS);
 160     }
 161 
 162     private static Era parseEraEntry(String entry) {
 163         String[] keyValuePairs = entry.split(",");
 164         String eraName = null;
 165         boolean localTime = true;
 166         long since = 0;




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.security.AccessController;
  29 import java.util.TimeZone;
  30 import sun.security.action.GetPropertyAction;
  31 
  32 /**
  33  *
  34  * @author Masayoshi Okutsu
  35  * @since 1.6
  36  */
  37 
  38 public class LocalGregorianCalendar extends BaseCalendar {
  39     private static final Era[] JAPANESE_ERAS = {
  40         new Era("Meiji",  "M", -3218832000000L, true),
  41         new Era("Taisho", "T", -1812153600000L, true),
  42         new Era("Showa",  "S", -1357603200000L, true),
  43         new Era("Heisei", "H",   600220800000L, true),
  44     };
  45 
  46     private static boolean isValidEra(Era newEra, Era[] eras) {
  47         Era last = eras[eras.length - 1];
  48         if (last.getSinceDate().getYear() >= newEra.getSinceDate().getYear()) {
  49             return false;
  50         }


 126                 String abbr = era.getAbbreviation();
 127                 if (abbr != null) {
 128                     sb.append(abbr);
 129                 }
 130             }
 131             sb.append(getYear()).append('.');
 132             CalendarUtils.sprintf0d(sb, getMonth(), 2).append('.');
 133             CalendarUtils.sprintf0d(sb, getDayOfMonth(), 2);
 134             sb.append(time);
 135             return sb.toString();
 136         }
 137     }
 138 
 139     static LocalGregorianCalendar getLocalGregorianCalendar(String name) {
 140         // Only the Japanese calendar is supported.
 141         if (!"japanese".equals(name)) {
 142             return null;
 143         }
 144 
 145         // Append an era to the predefined eras if it's given by the property.
 146         String prop = GetPropertyAction
 147                 .getProperty("jdk.calendar.japanese.supplemental.era");
 148         if (prop != null) {
 149             Era era = parseEraEntry(prop);
 150             if (era != null) {
 151                 if (isValidEra(era, JAPANESE_ERAS)) {
 152                     int length = JAPANESE_ERAS.length;
 153                     Era[] eras = new Era[length + 1];
 154                     System.arraycopy(JAPANESE_ERAS, 0, eras, 0, length);
 155                     eras[length] = era;
 156                     return new LocalGregorianCalendar(name, eras);
 157                 }
 158             }
 159         }
 160         return new LocalGregorianCalendar(name, JAPANESE_ERAS);
 161     }
 162 
 163     private static Era parseEraEntry(String entry) {
 164         String[] keyValuePairs = entry.split(",");
 165         String eraName = null;
 166         boolean localTime = true;
 167         long since = 0;


< prev index next >