View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.context;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.HashMap;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  public class DateTimeI18n {
32  
33    private static final Logger LOG = LoggerFactory.getLogger(DateTimeI18n.class);
34  
35    private static final Map<Locale, DateTimeI18n> CACHE = new HashMap<Locale, DateTimeI18n>();
36  
37    private final String[] monthNames = new String[12];
38    private final String[] monthNamesShort = new String[12];
39    private final String[] dayNames = new String[7];
40    private final String[] dayNamesShort = new String[7];
41    private final String[] dayNamesMin = new String[7];
42    private final int firstDay;
43  
44    private DateTimeI18n(Locale locale) {
45  
46      LOG.debug("Creating DateTimeI18n for locale: " + locale);
47  
48      final Calendar calendar = Calendar.getInstance(locale);
49  
50      calendar.set(2000, Calendar.JANUARY, 1);
51      final SimpleDateFormat dateFormatMMMMM = new SimpleDateFormat("MMMMM", locale);
52      final SimpleDateFormat dateFormatMMM = new SimpleDateFormat("MMM", locale);
53      for (int i = 0; i < monthNames.length; i++) {
54        monthNames[i] = dateFormatMMMMM.format(calendar.getTime());
55        monthNamesShort[i] = dateFormatMMM.format(calendar.getTime());
56        calendar.add(java.util.Calendar.MONTH, 1);
57      }
58  
59      final SimpleDateFormat dateFormatEEEEE = new SimpleDateFormat("EEEEE", locale);
60      final SimpleDateFormat dateFormatEEE = new SimpleDateFormat("EEE", locale);
61      final SimpleDateFormat dateFormatE = new SimpleDateFormat("E", locale);
62      calendar.set(2000, Calendar.JANUARY, 2);
63      for (int i = 0; i < dayNames.length; i++) {
64        dayNames[i] = dateFormatEEEEE.format(calendar.getTime());
65        dayNamesShort[i] = dateFormatEEE.format(calendar.getTime());
66        dayNamesMin[i] = dateFormatE.format(calendar.getTime());
67        calendar.add(Calendar.DAY_OF_YEAR, 1);
68      }
69  
70      firstDay = calendar.getFirstDayOfWeek() - 1; // because Java: 1 = Sunday and jQuery UI DatePicker: 0 = Sunday
71    }
72  
73    public static synchronized DateTimeI18n valueOf(Locale locale) {
74      DateTimeI18n dateTimeI18n;
75      dateTimeI18n = CACHE.get(locale);
76      if (dateTimeI18n == null) {
77        dateTimeI18n = new DateTimeI18n(locale);
78        CACHE.put(locale, dateTimeI18n);
79      }
80      return dateTimeI18n;
81    }
82  
83    public String[] getMonthNames() {
84      return monthNames;
85    }
86  
87    public String[] getMonthNamesShort() {
88      return monthNamesShort;
89    }
90  
91    public String[] getDayNames() {
92      return dayNames;
93    }
94  
95    public String[] getDayNamesShort() {
96      return dayNamesShort;
97    }
98  
99    public String[] getDayNamesMin() {
100     return dayNamesMin;
101   }
102 
103   public int getFirstDay() {
104     return firstDay;
105   }
106 }