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.apache.myfaces.tobago.util.ResourceUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.faces.context.FacesContext;
27  import java.lang.invoke.MethodHandles;
28  import java.text.SimpleDateFormat;
29  import java.util.Calendar;
30  import java.util.HashMap;
31  import java.util.Locale;
32  import java.util.Map;
33  
34  public class DateTimeI18n {
35  
36    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
37  
38    private static final Map<Locale, DateTimeI18n> CACHE = new HashMap<>();
39  
40    private final String[] monthNames = new String[12];
41    private final String[] monthNamesShort = new String[12];
42    private final String[] dayNames = new String[7];
43    private final String[] dayNamesShort = new String[7];
44    private final String[] dayNamesMin = new String[7];
45    private final int firstDay;
46    private final int minDays;
47    private final String today;
48    private final String cancel;
49    private final String clear;
50    private final String week;
51  
52    private DateTimeI18n(final Locale locale) {
53  
54      LOG.debug("Creating DateTimeI18n for locale: " + locale);
55  
56      final Calendar calendar = Calendar.getInstance(locale);
57  
58      calendar.set(2000, Calendar.JANUARY, 1);
59      final SimpleDateFormat dateFormatMMMMM = new SimpleDateFormat("MMMMM", locale);
60      final SimpleDateFormat dateFormatMMM = new SimpleDateFormat("MMM", locale);
61      for (int i = 0; i < monthNames.length; i++) {
62        monthNames[i] = dateFormatMMMMM.format(calendar.getTime());
63        monthNamesShort[i] = dateFormatMMM.format(calendar.getTime());
64        calendar.add(Calendar.MONTH, 1);
65      }
66  
67      final SimpleDateFormat dateFormatEEEEE = new SimpleDateFormat("EEEEE", locale);
68      final SimpleDateFormat dateFormatEEE = new SimpleDateFormat("EEE", locale);
69      final SimpleDateFormat dateFormatE = new SimpleDateFormat("E", locale);
70      calendar.set(2000, Calendar.JANUARY, 2);
71      for (int i = 0; i < dayNames.length; i++) {
72        dayNames[i] = dateFormatEEEEE.format(calendar.getTime());
73        dayNamesShort[i] = dateFormatEEE.format(calendar.getTime());
74        dayNamesMin[i] = dateFormatE.format(calendar.getTime());
75        calendar.add(Calendar.DAY_OF_YEAR, 1);
76      }
77  
78      firstDay = calendar.getFirstDayOfWeek() - 1; // because Java: 1 = Sunday and JavaScript: 0 = Sunday
79      minDays = calendar.getMinimalDaysInFirstWeek();
80  
81      FacesContext facesContext = FacesContext.getCurrentInstance();
82      today = ResourceUtils.getString(facesContext, "date.today");
83      cancel = ResourceUtils.getString(facesContext, "date.cancel");
84      clear = ResourceUtils.getString(facesContext, "date.clear");
85      week = ResourceUtils.getString(facesContext, "date.week");
86    }
87  
88    public static synchronized DateTimeI18n valueOf(final Locale locale) {
89      DateTimeI18n dateTimeI18n;
90      dateTimeI18n = CACHE.get(locale);
91      if (dateTimeI18n == null) {
92        dateTimeI18n = new DateTimeI18n(locale);
93        CACHE.put(locale, dateTimeI18n);
94      }
95      return dateTimeI18n;
96    }
97  
98    public String[] getMonthNames() {
99      return monthNames;
100   }
101 
102   public String[] getMonthNamesShort() {
103     return monthNamesShort;
104   }
105 
106   public String[] getDayNames() {
107     return dayNames;
108   }
109 
110   public String[] getDayNamesShort() {
111     return dayNamesShort;
112   }
113 
114   public String[] getDayNamesMin() {
115     return dayNamesMin;
116   }
117 
118   public int getFirstDay() {
119     return firstDay;
120   }
121 
122   public int getMinDays() {
123     return minDays;
124   }
125 
126   public String getToday() {
127     return today;
128   }
129 
130   public String getCancel() {
131     return cancel;
132   }
133 
134   public String getClear() {
135     return clear;
136   }
137 
138   public String getWeek() {
139     return week;
140   }
141 }