View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.util;
24  
25  import java.util.MissingResourceException;
26  import java.util.TimeZone;
27  
28  import de.bea.domingo.i18n.ResourceManager;
29  import de.bea.domingo.i18n.Resources;
30  
31  /***
32   * Static utility methods for time zone operations and conversions.
33   *
34   * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede </a>
35   */
36  public final class Timezones {
37  
38      /*** Maps time zone strings to Lotus Notes format. */
39      private static final Resources TIME_ZONES = ResourceManager.getClassResources(Timezones.class).withFailOnError(true);
40  
41      /*** Default time zone. */
42      public static final String DEFAULT = getLotusTimeZoneString(TimeZone.getDefault().getID());
43  
44      // TODO complete file TimezonesResources.properties
45  
46      /***
47       * Private default constructor to prevent instantiation.
48       */
49      private Timezones() {
50      }
51  
52      /***
53       * Converts a Java time zone to a Lotus timezone string.
54       * if the given java time zone string is null, empry or unknown,
55       * the default lotus time zone string is returned (<tt>""</tt>).
56       *
57       * @param timeZone the time zone
58       * @return Lotus time zone string
59       * @throws NullPointerException if the given time zone string is null
60       * @throws IllegalArgumentException if the length of the given time zone string is zero
61       */
62      public static String getLotusTimeZoneString(final TimeZone timeZone) throws NullPointerException, IllegalArgumentException {
63          return getLotusTimeZoneString(timeZone.getID());
64      }
65  
66      /***
67       * Converts a Java time zone string to a Lotus timezone string.
68       * if the given java time zone string is null, empry or unknown,
69       * the default lotus time zone string is returned (<tt>""</tt>).
70       *
71       * @param javaTimeZoneString the java time zone string
72       * @return Lotus time zone string
73       * @throws NullPointerException if the given time zone string is null
74       * @throws IllegalArgumentException if the given time zone string is empty or unknown
75       */
76      public static String getLotusTimeZoneString(final String javaTimeZoneString) throws NullPointerException,
77              IllegalArgumentException {
78          if (javaTimeZoneString == null) {
79              throw new NullPointerException("timezone must not be null");
80          }
81          if (javaTimeZoneString.length() == 0) {
82              throw new IllegalArgumentException("timezone must not be empty");
83          }
84          try {
85              return TIME_ZONES.getString(javaTimeZoneString);
86          } catch (MissingResourceException e) {
87              throw new IllegalArgumentException("unknown timezone: " + javaTimeZoneString);
88          }
89      }
90  }