< prev index next >

src/java.base/share/classes/java/time/ZoneId.java

Print this page




  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 
  64 import java.io.DataOutput;
  65 import java.io.IOException;
  66 import java.io.InvalidObjectException;
  67 import java.io.ObjectInputStream;
  68 import java.io.Serializable;
  69 import java.time.format.DateTimeFormatterBuilder;
  70 import java.time.format.TextStyle;
  71 import java.time.temporal.TemporalAccessor;
  72 import java.time.temporal.TemporalField;
  73 import java.time.temporal.TemporalQueries;
  74 import java.time.temporal.TemporalQuery;
  75 import java.time.temporal.UnsupportedTemporalTypeException;
  76 import java.time.zone.ZoneRules;
  77 import java.time.zone.ZoneRulesException;
  78 import java.time.zone.ZoneRulesProvider;
  79 import java.util.Collections;
  80 import java.util.HashMap;
  81 import java.util.Locale;
  82 import java.util.Map;
  83 import java.util.Objects;
  84 import java.util.Set;
  85 import java.util.TimeZone;
  86 


  87 /**
  88  * A time-zone ID, such as {@code Europe/Paris}.
  89  * <p>
  90  * A {@code ZoneId} is used to identify the rules used to convert between
  91  * an {@link Instant} and a {@link LocalDateTime}.
  92  * There are two distinct types of ID:
  93  * <ul>
  94  * <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
  95  *  the same offset for all local date-times
  96  * <li>Geographical regions - an area where a specific set of rules for finding
  97  *  the offset from UTC/Greenwich apply
  98  * </ul>
  99  * Most fixed offsets are represented by {@link ZoneOffset}.
 100  * Calling {@link #normalized()} on any {@code ZoneId} will ensure that a
 101  * fixed offset ID will be represented as a {@code ZoneOffset}.
 102  * <p>
 103  * The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
 104  * This class is simply an ID used to obtain the underlying rules.
 105  * This approach is taken because rules are defined by governments and change
 106  * frequently, whereas the ID is stable.


 203      * <li>CNT - America/St_Johns</li>
 204      * <li>CST - America/Chicago</li>
 205      * <li>CTT - Asia/Shanghai</li>
 206      * <li>EAT - Africa/Addis_Ababa</li>
 207      * <li>ECT - Europe/Paris</li>
 208      * <li>IET - America/Indiana/Indianapolis</li>
 209      * <li>IST - Asia/Kolkata</li>
 210      * <li>JST - Asia/Tokyo</li>
 211      * <li>MIT - Pacific/Apia</li>
 212      * <li>NET - Asia/Yerevan</li>
 213      * <li>NST - Pacific/Auckland</li>
 214      * <li>PLT - Asia/Karachi</li>
 215      * <li>PNT - America/Phoenix</li>
 216      * <li>PRT - America/Puerto_Rico</li>
 217      * <li>PST - America/Los_Angeles</li>
 218      * <li>SST - Pacific/Guadalcanal</li>
 219      * <li>VST - Asia/Ho_Chi_Minh</li>
 220      * </ul>
 221      * The map is unmodifiable.
 222      */
 223     public static final Map<String, String> SHORT_IDS;
 224     static {
 225         Map<String, String> map = new HashMap<>(64);
 226         map.put("ACT", "Australia/Darwin");
 227         map.put("AET", "Australia/Sydney");
 228         map.put("AGT", "America/Argentina/Buenos_Aires");
 229         map.put("ART", "Africa/Cairo");
 230         map.put("AST", "America/Anchorage");
 231         map.put("BET", "America/Sao_Paulo");
 232         map.put("BST", "Asia/Dhaka");
 233         map.put("CAT", "Africa/Harare");
 234         map.put("CNT", "America/St_Johns");
 235         map.put("CST", "America/Chicago");
 236         map.put("CTT", "Asia/Shanghai");
 237         map.put("EAT", "Africa/Addis_Ababa");
 238         map.put("ECT", "Europe/Paris");
 239         map.put("IET", "America/Indiana/Indianapolis");
 240         map.put("IST", "Asia/Kolkata");
 241         map.put("JST", "Asia/Tokyo");
 242         map.put("MIT", "Pacific/Apia");
 243         map.put("NET", "Asia/Yerevan");
 244         map.put("NST", "Pacific/Auckland");
 245         map.put("PLT", "Asia/Karachi");
 246         map.put("PNT", "America/Phoenix");
 247         map.put("PRT", "America/Puerto_Rico");
 248         map.put("PST", "America/Los_Angeles");
 249         map.put("SST", "Pacific/Guadalcanal");
 250         map.put("VST", "Asia/Ho_Chi_Minh");
 251         map.put("EST", "-05:00");
 252         map.put("MST", "-07:00");
 253         map.put("HST", "-10:00");
 254         SHORT_IDS = Collections.unmodifiableMap(map);
 255     }
 256     /**
 257      * Serialization version.
 258      */
 259     private static final long serialVersionUID = 8352817235686L;
 260 
 261     //-----------------------------------------------------------------------
 262     /**
 263      * Gets the system default time-zone.
 264      * <p>
 265      * This queries {@link TimeZone#getDefault()} to find the default time-zone
 266      * and converts it to a {@code ZoneId}. If the system default time-zone is changed,
 267      * then the result of this method will also change.
 268      *
 269      * @return the zone ID, not null
 270      * @throws DateTimeException if the converted zone ID has an invalid format
 271      * @throws ZoneRulesException if the converted zone region ID cannot be found
 272      */
 273     public static ZoneId systemDefault() {
 274         return TimeZone.getDefault().toZoneId();
 275     }




  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 
  64 import java.io.DataOutput;
  65 import java.io.IOException;
  66 import java.io.InvalidObjectException;
  67 import java.io.ObjectInputStream;
  68 import java.io.Serializable;
  69 import java.time.format.DateTimeFormatterBuilder;
  70 import java.time.format.TextStyle;
  71 import java.time.temporal.TemporalAccessor;
  72 import java.time.temporal.TemporalField;
  73 import java.time.temporal.TemporalQueries;
  74 import java.time.temporal.TemporalQuery;
  75 import java.time.temporal.UnsupportedTemporalTypeException;
  76 import java.time.zone.ZoneRules;
  77 import java.time.zone.ZoneRulesException;
  78 import java.time.zone.ZoneRulesProvider;


  79 import java.util.Locale;
  80 import java.util.Map;
  81 import java.util.Objects;
  82 import java.util.Set;
  83 import java.util.TimeZone;
  84 
  85 import static java.util.Map.entry;
  86 
  87 /**
  88  * A time-zone ID, such as {@code Europe/Paris}.
  89  * <p>
  90  * A {@code ZoneId} is used to identify the rules used to convert between
  91  * an {@link Instant} and a {@link LocalDateTime}.
  92  * There are two distinct types of ID:
  93  * <ul>
  94  * <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
  95  *  the same offset for all local date-times
  96  * <li>Geographical regions - an area where a specific set of rules for finding
  97  *  the offset from UTC/Greenwich apply
  98  * </ul>
  99  * Most fixed offsets are represented by {@link ZoneOffset}.
 100  * Calling {@link #normalized()} on any {@code ZoneId} will ensure that a
 101  * fixed offset ID will be represented as a {@code ZoneOffset}.
 102  * <p>
 103  * The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
 104  * This class is simply an ID used to obtain the underlying rules.
 105  * This approach is taken because rules are defined by governments and change
 106  * frequently, whereas the ID is stable.


 203      * <li>CNT - America/St_Johns</li>
 204      * <li>CST - America/Chicago</li>
 205      * <li>CTT - Asia/Shanghai</li>
 206      * <li>EAT - Africa/Addis_Ababa</li>
 207      * <li>ECT - Europe/Paris</li>
 208      * <li>IET - America/Indiana/Indianapolis</li>
 209      * <li>IST - Asia/Kolkata</li>
 210      * <li>JST - Asia/Tokyo</li>
 211      * <li>MIT - Pacific/Apia</li>
 212      * <li>NET - Asia/Yerevan</li>
 213      * <li>NST - Pacific/Auckland</li>
 214      * <li>PLT - Asia/Karachi</li>
 215      * <li>PNT - America/Phoenix</li>
 216      * <li>PRT - America/Puerto_Rico</li>
 217      * <li>PST - America/Los_Angeles</li>
 218      * <li>SST - Pacific/Guadalcanal</li>
 219      * <li>VST - Asia/Ho_Chi_Minh</li>
 220      * </ul>
 221      * The map is unmodifiable.
 222      */
 223     public static final Map<String, String> SHORT_IDS = Map.ofEntries(
 224         entry("ACT", "Australia/Darwin"),
 225         entry("AET", "Australia/Sydney"),
 226         entry("AGT", "America/Argentina/Buenos_Aires"),
 227         entry("ART", "Africa/Cairo"),
 228         entry("AST", "America/Anchorage"),
 229         entry("BET", "America/Sao_Paulo"),
 230         entry("BST", "Asia/Dhaka"),
 231         entry("CAT", "Africa/Harare"),
 232         entry("CNT", "America/St_Johns"),
 233         entry("CST", "America/Chicago"),
 234         entry("CTT", "Asia/Shanghai"),
 235         entry("EAT", "Africa/Addis_Ababa"),
 236         entry("ECT", "Europe/Paris"),
 237         entry("IET", "America/Indiana/Indianapolis"),
 238         entry("IST", "Asia/Kolkata"),
 239         entry("JST", "Asia/Tokyo"),
 240         entry("MIT", "Pacific/Apia"),
 241         entry("NET", "Asia/Yerevan"),
 242         entry("NST", "Pacific/Auckland"),
 243         entry("PLT", "Asia/Karachi"),
 244         entry("PNT", "America/Phoenix"),
 245         entry("PRT", "America/Puerto_Rico"),
 246         entry("PST", "America/Los_Angeles"),
 247         entry("SST", "Pacific/Guadalcanal"),
 248         entry("VST", "Asia/Ho_Chi_Minh"),
 249         entry("EST", "-05:00"),
 250         entry("MST", "-07:00"),
 251         entry("HST", "-10:00")
 252     );



 253     /**
 254      * Serialization version.
 255      */
 256     private static final long serialVersionUID = 8352817235686L;
 257 
 258     //-----------------------------------------------------------------------
 259     /**
 260      * Gets the system default time-zone.
 261      * <p>
 262      * This queries {@link TimeZone#getDefault()} to find the default time-zone
 263      * and converts it to a {@code ZoneId}. If the system default time-zone is changed,
 264      * then the result of this method will also change.
 265      *
 266      * @return the zone ID, not null
 267      * @throws DateTimeException if the converted zone ID has an invalid format
 268      * @throws ZoneRulesException if the converted zone region ID cannot be found
 269      */
 270     public static ZoneId systemDefault() {
 271         return TimeZone.getDefault().toZoneId();
 272     }


< prev index next >