Skip to content
Permalink
Browse files
Move new internal Calendar APIs to CalendarUtils and refactor.
  • Loading branch information
garydgregory committed May 15, 2022
1 parent eff4386 commit 25e91c4242283d118ba978d4f38cd18e10c890dc
Showing 5 changed files with 103 additions and 75 deletions.
@@ -18,6 +18,8 @@
package org.apache.commons.lang3.time;

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
@@ -32,17 +34,40 @@ public class CalendarUtils {
*/
public static final CalendarUtils INSTANCE = new CalendarUtils(Calendar.getInstance());

/**
* Gets a CalendarUtils using the default time zone and specified locale. The <code>CalendarUtils</code> returned is based on the current time in the
* default time zone with the given locale.
*
* @param locale the locale for the week data
* @return a Calendar.
*/
static CalendarUtils getInstance(final Locale locale) {
return new CalendarUtils(Calendar.getInstance(locale), locale);
}

private final Calendar calendar;

private final Locale locale;

/**
* Creates an instance for the given Calendar.
*
* @param calendar A Calendar.
*/
public CalendarUtils(final Calendar calendar) {
this.calendar = Objects.requireNonNull(calendar, "calendar");
this(calendar, Locale.getDefault());
}

/**
* Creates an instance for the given Calendar.
*
* @param calendar A Calendar.
* @param locale A Locale.
*/
CalendarUtils(final Calendar calendar, final Locale locale) {
this.calendar = Objects.requireNonNull(calendar, "calendar");
this.locale = Objects.requireNonNull(locale, "locale");
}
/**
* Gets the current day of month.
*
@@ -61,6 +86,43 @@ public int getMonth() {
return calendar.get(Calendar.MONTH);
}

/**
* Gets month names in the requested style.
* @param locale Locale
* @param style Must be a valid {@link Calendar#getDisplayNames(int, int, Locale)} month style.
* @return Styled names of months
*/
String[] getMonthDisplayNames(final int style) {
// Unfortunately standalone month names are not available in DateFormatSymbols,
// so we have to extract them.
final Calendar calendar = Calendar.getInstance(locale);
final Map<String, Integer> displayNames = calendar.getDisplayNames(Calendar.MONTH, style, locale);
if (displayNames == null) {
return null;
}
final String[] monthNames = new String[displayNames.size()];
for (final Map.Entry<String, Integer> entry: displayNames.entrySet()) {
monthNames[entry.getValue()] = entry.getKey();
}
return monthNames;
}

/**
* Gets full standalone month names as used in "LLLL" date formatting.
* @return Long names of months
*/
String[] getStandaloneLongMonthNames() {
return getMonthDisplayNames(Calendar.LONG_STANDALONE);
}

/**
* Gets short standalone month names as used in "LLLL" date formatting.
* @return Short names of months
*/
String[] getStandaloneShortMonthNames() {
return getMonthDisplayNames(Calendar.SHORT_STANDALONE);
}

/**
* Gets the current year.
*
@@ -22,7 +22,6 @@
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
@@ -1832,40 +1831,4 @@ public void remove() {
}
}

/**
* Gets full standalone month names as used in "LLLL" date formatting.
* @param locale Locale
* @return Long names of months
*/
static String[] getStandaloneLongMonths(final Locale locale) {
return getMonthNames(locale, Calendar.LONG_STANDALONE);
}

/**
* Gets short standalone month names as used in "LLLL" date formatting.
* @param locale Locale
* @return Short names of months
*/
static String[] getStandaloneShortMonths(final Locale locale) {
return getMonthNames(locale, Calendar.SHORT_STANDALONE);
}

/**
* Gets month names in the requested style.
* @param locale Locale
* @param style Must be a valid {@link Calendar#getDisplayNames(int, int, Locale)} month style.
* @return Styled names of months
*/
private static String[] getMonthNames(final Locale locale, final int style) {
// Unfortunately standalone month names are not available in DateFormatSymbols,
// so we have to extract them.
final Calendar calendar = Calendar.getInstance(locale);
final Map<String, Integer> displayNames = calendar.getDisplayNames(Calendar.MONTH, style, locale);
final String[] monthNames = new String[displayNames.size()];
for (final Map.Entry<String, Integer> entry: displayNames.entrySet()) {
monthNames[entry.getValue()] = entry.getKey();
}
return monthNames;
}

}
@@ -235,9 +235,9 @@ protected List<Rule> parsePattern() {
break;
case 'L': // month in year (text and number)
if (tokenLen >= 4) {
rule = new TextField(Calendar.MONTH, DateUtils.getStandaloneLongMonths(mLocale));
rule = new TextField(Calendar.MONTH, CalendarUtils.getInstance(mLocale).getStandaloneLongMonthNames());
} else if (tokenLen == 3) {
rule = new TextField(Calendar.MONTH, DateUtils.getStandaloneShortMonths(mLocale));
rule = new TextField(Calendar.MONTH, CalendarUtils.getInstance(mLocale).getStandaloneShortMonthNames());
} else if (tokenLen == 2) {
rule = TwoDigitMonthField.INSTANCE;
} else {
@@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Calendar;
import java.util.Locale;

import org.junit.jupiter.api.Test;

@@ -35,9 +36,46 @@ public void testGetMonth() {
assertEquals(Calendar.getInstance().get(Calendar.MONTH), CalendarUtils.INSTANCE.getMonth());
}

@Test
public void testGetStandaloneLongMonthNames() {
final String[] monthNames = CalendarUtils.getInstance(Locale.GERMAN).getStandaloneLongMonthNames();
assertEquals(12, monthNames.length);
assertEquals("Januar", monthNames[0]);
assertEquals("Februar", monthNames[1]);
assertEquals("M\u00e4rz", monthNames[2]);
assertEquals("April", monthNames[3]);
assertEquals("Mai", monthNames[4]);
assertEquals("Juni", monthNames[5]);
assertEquals("Juli", monthNames[6]);
assertEquals("August", monthNames[7]);
assertEquals("September", monthNames[8]);
assertEquals("Oktober", monthNames[9]);
assertEquals("November", monthNames[10]);
assertEquals("Dezember", monthNames[11]);
}

@Test
public void testGetStandaloneShortMonthNames() {
final String[] monthNames = CalendarUtils.getInstance(Locale.GERMAN).getStandaloneShortMonthNames();
assertEquals(12, monthNames.length);
assertEquals("Jan", monthNames[0]);
assertEquals("Feb", monthNames[1]);
assertEquals("M\u00e4r", monthNames[2]);
assertEquals("Apr", monthNames[3]);
assertEquals("Mai", monthNames[4]);
assertEquals("Jun", monthNames[5]);
assertEquals("Jul", monthNames[6]);
assertEquals("Aug", monthNames[7]);
assertEquals("Sep", monthNames[8]);
assertEquals("Okt", monthNames[9]);
assertEquals("Nov", monthNames[10]);
assertEquals("Dez", monthNames[11]);
}

@Test
public void testGetYear() {
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
}


}
@@ -1695,40 +1695,5 @@ public void testWeekIterator() {
}
}

@Test
public void testGetStandaloneLongMonthNames() {
final String[] monthNames = DateUtils.getStandaloneLongMonths(Locale.GERMAN);
assertEquals(12, monthNames.length);
assertEquals("Januar", monthNames[0]);
assertEquals("Februar", monthNames[1]);
assertEquals("M\u00e4rz", monthNames[2]);
assertEquals("April", monthNames[3]);
assertEquals("Mai", monthNames[4]);
assertEquals("Juni", monthNames[5]);
assertEquals("Juli", monthNames[6]);
assertEquals("August", monthNames[7]);
assertEquals("September", monthNames[8]);
assertEquals("Oktober", monthNames[9]);
assertEquals("November", monthNames[10]);
assertEquals("Dezember", monthNames[11]);
}

@Test
public void testGetStandaloneShortMonthNames() {
final String[] monthNames = DateUtils.getStandaloneShortMonths(Locale.GERMAN);
assertEquals(12, monthNames.length);
assertEquals("Jan", monthNames[0]);
assertEquals("Feb", monthNames[1]);
assertEquals("M\u00e4r", monthNames[2]);
assertEquals("Apr", monthNames[3]);
assertEquals("Mai", monthNames[4]);
assertEquals("Jun", monthNames[5]);
assertEquals("Jul", monthNames[6]);
assertEquals("Aug", monthNames[7]);
assertEquals("Sep", monthNames[8]);
assertEquals("Okt", monthNames[9]);
assertEquals("Nov", monthNames[10]);
assertEquals("Dez", monthNames[11]);
}
}

0 comments on commit 25e91c4

Please sign in to comment.