src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 53018 8bf9268df0e2
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    83 import java.time.LocalDateTime;
    83 import java.time.LocalDateTime;
    84 import java.time.LocalTime;
    84 import java.time.LocalTime;
    85 import java.time.ZoneId;
    85 import java.time.ZoneId;
    86 import java.time.ZoneOffset;
    86 import java.time.ZoneOffset;
    87 import java.time.chrono.ChronoLocalDate;
    87 import java.time.chrono.ChronoLocalDate;
    88 import java.time.chrono.ChronoLocalDateTime;
       
    89 import java.time.chrono.Chronology;
    88 import java.time.chrono.Chronology;
    90 import java.time.chrono.Era;
    89 import java.time.chrono.Era;
    91 import java.time.chrono.IsoChronology;
    90 import java.time.chrono.IsoChronology;
    92 import java.time.format.DateTimeTextProvider.LocaleStore;
    91 import java.time.format.DateTimeTextProvider.LocaleStore;
    93 import java.time.temporal.ChronoField;
    92 import java.time.temporal.ChronoField;
   120 import java.util.concurrent.ConcurrentMap;
   119 import java.util.concurrent.ConcurrentMap;
   121 
   120 
   122 import sun.text.spi.JavaTimeDateTimePatternProvider;
   121 import sun.text.spi.JavaTimeDateTimePatternProvider;
   123 import sun.util.locale.provider.CalendarDataUtility;
   122 import sun.util.locale.provider.CalendarDataUtility;
   124 import sun.util.locale.provider.LocaleProviderAdapter;
   123 import sun.util.locale.provider.LocaleProviderAdapter;
   125 import sun.util.locale.provider.LocaleResources;
       
   126 import sun.util.locale.provider.TimeZoneNameUtility;
   124 import sun.util.locale.provider.TimeZoneNameUtility;
   127 
   125 
   128 /**
   126 /**
   129  * Builder to create date-time formatters.
   127  * Builder to create date-time formatters.
   130  * <p>
   128  * <p>
   306 
   304 
   307     //-----------------------------------------------------------------------
   305     //-----------------------------------------------------------------------
   308     /**
   306     /**
   309      * Changes the parse style to be strict for the remainder of the formatter.
   307      * Changes the parse style to be strict for the remainder of the formatter.
   310      * <p>
   308      * <p>
   311      * Parsing can be strict or lenient - by default its strict.
   309      * Parsing can be strict or lenient - by default it is strict.
   312      * This controls the degree of flexibility in matching the text and sign styles.
   310      * This controls the degree of flexibility in matching the text and sign styles.
   313      * <p>
   311      * <p>
   314      * When used, this method changes the parsing to be strict from this point onwards.
   312      * When used, this method changes the parsing to be strict from this point onwards.
   315      * As strict is the default, this is normally only needed after calling {@link #parseLenient()}.
   313      * As strict is the default, this is normally only needed after calling {@link #parseLenient()}.
   316      * The change will remain in force until the end of the formatter that is eventually
   314      * The change will remain in force until the end of the formatter that is eventually
   325 
   323 
   326     /**
   324     /**
   327      * Changes the parse style to be lenient for the remainder of the formatter.
   325      * Changes the parse style to be lenient for the remainder of the formatter.
   328      * Note that case sensitivity is set separately to this method.
   326      * Note that case sensitivity is set separately to this method.
   329      * <p>
   327      * <p>
   330      * Parsing can be strict or lenient - by default its strict.
   328      * Parsing can be strict or lenient - by default it is strict.
   331      * This controls the degree of flexibility in matching the text and sign styles.
   329      * This controls the degree of flexibility in matching the text and sign styles.
   332      * Applications calling this method should typically also call {@link #parseCaseInsensitive()}.
   330      * Applications calling this method should typically also call {@link #parseCaseInsensitive()}.
   333      * <p>
   331      * <p>
   334      * When used, this method changes the parsing to be lenient from this point onwards.
   332      * When used, this method changes the parsing to be lenient from this point onwards.
   335      * The change will remain in force until the end of the formatter that is eventually
   333      * The change will remain in force until the end of the formatter that is eventually
  3200             int pos = position;
  3198             int pos = position;
  3201             while (pos < maxEndPos) {
  3199             while (pos < maxEndPos) {
  3202                 char ch = text.charAt(pos++);
  3200                 char ch = text.charAt(pos++);
  3203                 int digit = context.getDecimalStyle().convertToDigit(ch);
  3201                 int digit = context.getDecimalStyle().convertToDigit(ch);
  3204                 if (digit < 0) {
  3202                 if (digit < 0) {
  3205                     if (pos < minEndPos) {
  3203                     if (pos <= minEndPos) {
  3206                         return ~position;  // need at least min width digits
  3204                         return ~position;  // need at least min width digits
  3207                     }
  3205                     }
  3208                     pos--;
  3206                     pos--;
  3209                     break;
  3207                     break;
  3210                 }
  3208                 }
  3869         public boolean format(DateTimePrintContext context, StringBuilder buf) {
  3867         public boolean format(DateTimePrintContext context, StringBuilder buf) {
  3870             Long offsetSecs = context.getValue(OFFSET_SECONDS);
  3868             Long offsetSecs = context.getValue(OFFSET_SECONDS);
  3871             if (offsetSecs == null) {
  3869             if (offsetSecs == null) {
  3872                 return false;
  3870                 return false;
  3873             }
  3871             }
  3874             String gmtText = "GMT";  // TODO: get localized version of 'GMT'
  3872             String key = "timezone.gmtZeroFormat";
       
  3873             String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
       
  3874             if (gmtText == null) {
       
  3875                 gmtText = "GMT";  // Default to "GMT"
       
  3876             }
  3875             buf.append(gmtText);
  3877             buf.append(gmtText);
  3876             int totalSecs = Math.toIntExact(offsetSecs);
  3878             int totalSecs = Math.toIntExact(offsetSecs);
  3877             if (totalSecs != 0) {
  3879             if (totalSecs != 0) {
  3878                 int absHours = Math.abs((totalSecs / 3600) % 100);  // anything larger than 99 silently dropped
  3880                 int absHours = Math.abs((totalSecs / 3600) % 100);  // anything larger than 99 silently dropped
  3879                 int absMinutes = Math.abs((totalSecs / 60) % 60);
  3881                 int absMinutes = Math.abs((totalSecs / 60) % 60);
  3915 
  3917 
  3916         @Override
  3918         @Override
  3917         public int parse(DateTimeParseContext context, CharSequence text, int position) {
  3919         public int parse(DateTimeParseContext context, CharSequence text, int position) {
  3918             int pos = position;
  3920             int pos = position;
  3919             int end = text.length();
  3921             int end = text.length();
  3920             String gmtText = "GMT";  // TODO: get localized version of 'GMT'
  3922             String key = "timezone.gmtZeroFormat";
       
  3923             String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
       
  3924             if (gmtText == null) {
       
  3925                 gmtText = "GMT";  // Default to "GMT"
       
  3926             }
  3921             if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
  3927             if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
  3922                     return ~position;
  3928                     return ~position;
  3923                 }
  3929                 }
  3924             pos += gmtText.length();
  3930             pos += gmtText.length();
  3925             // parse normal plus/minus offset
  3931             // parse normal plus/minus offset