• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Map;
23 import java.util.Objects;
24 
25 /**
26  * Helps use {@link Calendar}s.
27  *
28  * @since 3.10
29  */
30 public class CalendarUtils {
31 
32     /**
33      * The singleton instance for {@link Calendar#getInstance()}.
34      */
35     public static final CalendarUtils INSTANCE = new CalendarUtils(Calendar.getInstance());
36 
37     /**
38      * Gets a CalendarUtils using the default time zone and specified locale. The <code>CalendarUtils</code> returned is based on the current time in the
39      * default time zone with the given locale.
40      *
41      * @param locale the locale for the week data
42      * @return a Calendar.
43      */
getInstance(final Locale locale)44     static CalendarUtils getInstance(final Locale locale) {
45         return new CalendarUtils(Calendar.getInstance(locale), locale);
46     }
47 
48     private final Calendar calendar;
49 
50     private final Locale locale;
51 
52     /**
53      * Creates an instance for the given Calendar.
54      *
55      * @param calendar A Calendar.
56      */
CalendarUtils(final Calendar calendar)57     public CalendarUtils(final Calendar calendar) {
58         this(calendar, Locale.getDefault());
59     }
60 
61     /**
62      * Creates an instance for the given Calendar.
63      *
64      * @param calendar A Calendar.
65      * @param locale A Locale.
66      */
CalendarUtils(final Calendar calendar, final Locale locale)67     CalendarUtils(final Calendar calendar, final Locale locale) {
68         this.calendar = Objects.requireNonNull(calendar, "calendar");
69         this.locale = Objects.requireNonNull(locale, "locale");
70     }
71     /**
72      * Gets the current day of month.
73      *
74      * @return the current day of month.
75      */
getDayOfMonth()76     public int getDayOfMonth() {
77         return calendar.get(Calendar.DAY_OF_MONTH);
78     }
79 
80     /**
81      * Gets the current day of year.
82      *
83      * @return the current day of year.
84      * @since 3.13.0
85      */
getDayOfYear()86     public int getDayOfYear() {
87         return calendar.get(Calendar.DAY_OF_YEAR);
88     }
89 
90     /**
91      * Gets the current month.
92      *
93      * @return the current month.
94      */
getMonth()95     public int getMonth() {
96         return calendar.get(Calendar.MONTH);
97     }
98 
99     /**
100      * Gets month names in the requested style.
101      * @param style Must be a valid {@link Calendar#getDisplayNames(int, int, Locale)} month style.
102      * @return Styled names of months
103      */
getMonthDisplayNames(final int style)104     String[] getMonthDisplayNames(final int style) {
105         // Unfortunately standalone month names are not available in DateFormatSymbols,
106         // so we have to extract them.
107         final Map<String, Integer> displayNames = calendar.getDisplayNames(Calendar.MONTH, style, locale);
108         if (displayNames == null) {
109             return null;
110         }
111         final String[] monthNames = new String[displayNames.size()];
112         displayNames.forEach((k, v) -> monthNames[v] = k);
113         return monthNames;
114     }
115 
116     /**
117      * Gets full standalone month names as used in "LLLL" date formatting.
118      * @return Long names of months
119      */
getStandaloneLongMonthNames()120     String[] getStandaloneLongMonthNames() {
121         return getMonthDisplayNames(Calendar.LONG_STANDALONE);
122     }
123 
124     /**
125      * Gets short standalone month names as used in "LLLL" date formatting.
126      * @return Short names of months
127      */
getStandaloneShortMonthNames()128     String[] getStandaloneShortMonthNames() {
129         return getMonthDisplayNames(Calendar.SHORT_STANDALONE);
130     }
131 
132     /**
133      * Gets the current year.
134      *
135      * @return the current year.
136      */
getYear()137     public int getYear() {
138         return calendar.get(Calendar.YEAR);
139     }
140 }
141