View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.time;
19  
20  import java.util.Calendar;
21  import java.util.Locale;
22  import java.util.Locale.Category;
23  import java.util.Map;
24  import java.util.Objects;
25  
26  /**
27   * Helps use {@link Calendar}s.
28   *
29   * @since 3.10
30   */
31  public class CalendarUtils {
32  
33      /**
34       * The singleton instance for {@link Calendar#getInstance()}. The instance is created when the class is initialized and is based on the current time in the
35       * default time zone with the default {@link Category#FORMAT} locale.
36       *
37       * @see CalendarUtils#getInstance()
38       */
39      public static final CalendarUtils INSTANCE = getInstance();
40  
41      /**
42       * Creates a new instance based on the current time in the default time zone with the default {@link Category#FORMAT} locale.
43       *
44       * @return a new instance.
45       * @since 3.14.0
46       */
47      public static CalendarUtils getInstance() {
48          return new CalendarUtils(Calendar.getInstance());
49      }
50  
51      /**
52       * Gets a CalendarUtils using the default time zone and specified locale. The <code>CalendarUtils</code> returned is based on the current time in the
53       * default time zone with the given locale.
54       *
55       * @param locale the locale for the week data
56       * @return a Calendar.
57       */
58      static CalendarUtils getInstance(final Locale locale) {
59          return new CalendarUtils(Calendar.getInstance(locale), locale);
60      }
61  
62      private final Calendar calendar;
63  
64      private final Locale locale;
65  
66      /**
67       * Creates an instance for the given Calendar.
68       *
69       * @param calendar A Calendar.
70       */
71      public CalendarUtils(final Calendar calendar) {
72          this(calendar, Locale.getDefault());
73      }
74  
75      /**
76       * Creates an instance for the given Calendar.
77       *
78       * @param calendar A Calendar.
79       * @param locale A Locale.
80       */
81      CalendarUtils(final Calendar calendar, final Locale locale) {
82          this.calendar = Objects.requireNonNull(calendar, "calendar");
83          this.locale = Objects.requireNonNull(locale, "locale");
84      }
85      /**
86       * Gets the current day of month.
87       *
88       * @return the current day of month.
89       */
90      public int getDayOfMonth() {
91          return calendar.get(Calendar.DAY_OF_MONTH);
92      }
93  
94      /**
95       * Gets the current day of year.
96       *
97       * @return the current day of year.
98       * @since 3.13.0
99       */
100     public int getDayOfYear() {
101         return calendar.get(Calendar.DAY_OF_YEAR);
102     }
103 
104     /**
105      * Gets the current month.
106      *
107      * @return the current month.
108      */
109     public int getMonth() {
110         return calendar.get(Calendar.MONTH);
111     }
112 
113     /**
114      * Gets month names in the requested style.
115      * @param style Must be a valid {@link Calendar#getDisplayNames(int, int, Locale)} month style.
116      * @return Styled names of months
117      */
118     String[] getMonthDisplayNames(final int style) {
119         // Unfortunately standalone month names are not available in DateFormatSymbols,
120         // so we have to extract them.
121         final Map<String, Integer> displayNames = calendar.getDisplayNames(Calendar.MONTH, style, locale);
122         if (displayNames == null) {
123             return null;
124         }
125         final String[] monthNames = new String[displayNames.size()];
126         displayNames.forEach((k, v) -> monthNames[v] = k);
127         return monthNames;
128     }
129 
130     /**
131      * Gets full standalone month names as used in "LLLL" date formatting.
132      * @return Long names of months
133      */
134     String[] getStandaloneLongMonthNames() {
135         return getMonthDisplayNames(Calendar.LONG_STANDALONE);
136     }
137 
138     /**
139      * Gets short standalone month names as used in "LLLL" date formatting.
140      * @return Short names of months
141      */
142     String[] getStandaloneShortMonthNames() {
143         return getMonthDisplayNames(Calendar.SHORT_STANDALONE);
144     }
145 
146     /**
147      * Gets the current year.
148      *
149      * @return the current year.
150      */
151     public int getYear() {
152         return calendar.get(Calendar.YEAR);
153     }
154 }