use of java.text.DateFormatSymbols in project logging-log4j2 by apache.
the class FastDatePrinter method parsePattern.
// Parse the pattern
//-----------------------------------------------------------------------
/**
* <p>Returns a list of Rules given a pattern.</p>
*
* @return a {@code List} of Rule objects
* @throws IllegalArgumentException if pattern is invalid
*/
protected List<Rule> parsePattern() {
final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
final List<Rule> rules = new ArrayList<>();
final String[] ERAs = symbols.getEras();
final String[] months = symbols.getMonths();
final String[] shortMonths = symbols.getShortMonths();
final String[] weekdays = symbols.getWeekdays();
final String[] shortWeekdays = symbols.getShortWeekdays();
final String[] AmPmStrings = symbols.getAmPmStrings();
final int length = mPattern.length();
final int[] indexRef = new int[1];
for (int i = 0; i < length; i++) {
indexRef[0] = i;
final String token = parseToken(mPattern, indexRef);
i = indexRef[0];
final int tokenLen = token.length();
if (tokenLen == 0) {
break;
}
Rule rule;
final char c = token.charAt(0);
switch(c) {
case // era designator (text)
'G':
rule = new TextField(Calendar.ERA, ERAs);
break;
// year (number)
case 'y':
case // week year
'Y':
if (tokenLen == 2) {
rule = TwoDigitYearField.INSTANCE;
} else {
rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen);
}
if (c == 'Y') {
rule = new WeekYear((NumberRule) rule);
}
break;
case // month in year (text and number)
'M':
if (tokenLen >= 4) {
rule = new TextField(Calendar.MONTH, months);
} else if (tokenLen == 3) {
rule = new TextField(Calendar.MONTH, shortMonths);
} else if (tokenLen == 2) {
rule = TwoDigitMonthField.INSTANCE;
} else {
rule = UnpaddedMonthField.INSTANCE;
}
break;
case // day in month (number)
'd':
rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
break;
case // hour in am/pm (number, 1..12)
'h':
rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
break;
case // hour in day (number, 0..23)
'H':
rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
break;
case // minute in hour (number)
'm':
rule = selectNumberRule(Calendar.MINUTE, tokenLen);
break;
case // second in minute (number)
's':
rule = selectNumberRule(Calendar.SECOND, tokenLen);
break;
case // millisecond (number)
'S':
rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
break;
case // day in week (text)
'E':
rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
break;
case // day in week (number)
'u':
rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen));
break;
case // day in year (number)
'D':
rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
break;
case // day of week in month (number)
'F':
rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
break;
case // week in year (number)
'w':
rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
break;
case // week in month (number)
'W':
rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
break;
case // am/pm marker (text)
'a':
rule = new TextField(Calendar.AM_PM, AmPmStrings);
break;
case // hour in day (1..24)
'k':
rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
break;
case // hour in am/pm (0..11)
'K':
rule = selectNumberRule(Calendar.HOUR, tokenLen);
break;
case // ISO 8601
'X':
rule = Iso8601_Rule.getRule(tokenLen);
break;
case // time zone (text)
'z':
if (tokenLen >= 4) {
rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
} else {
rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
}
break;
case // time zone (value)
'Z':
if (tokenLen == 1) {
rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
} else if (tokenLen == 2) {
rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
} else {
rule = TimeZoneNumberRule.INSTANCE_COLON;
}
break;
case // literal text
'\'':
final String sub = token.substring(1);
if (sub.length() == 1) {
rule = new CharacterLiteral(sub.charAt(0));
} else {
rule = new StringLiteral(sub);
}
break;
default:
throw new IllegalArgumentException("Illegal pattern component: " + token);
}
rules.add(rule);
}
return rules;
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class DateFormatSymbolsTest method testSerialization.
public void testSerialization() throws Exception {
// The Polish language needs stand-alone month and weekday names.
Locale pl = new Locale("pl");
DateFormatSymbols originalDfs = new DateFormatSymbols(pl);
// Serialize...
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(originalDfs);
byte[] bytes = out.toByteArray();
// Deserialize...
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
DateFormatSymbols deserializedDfs = (DateFormatSymbols) in.readObject();
assertEquals(-1, in.read());
// The two objects should claim to be equal, even though they aren't really.
assertEquals(originalDfs, deserializedDfs);
// The original differentiates between regular month names and stand-alone month names...
assertEquals("stycznia", formatDate(pl, "MMMM", originalDfs));
assertEquals("styczeń", formatDate(pl, "LLLL", originalDfs));
// But the deserialized object is screwed because the RI's serialized form doesn't
// contain the locale or the necessary strings. Don't serialize DateFormatSymbols, folks!
assertEquals("stycznia", formatDate(pl, "MMMM", deserializedDfs));
assertEquals("January", formatDate(pl, "LLLL", deserializedDfs));
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class DateFormatSymbolsTest method assertLocaleIsEquivalentToRoot.
private void assertLocaleIsEquivalentToRoot(Locale locale) {
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
assertEquals(DateFormatSymbols.getInstance(Locale.ROOT), dfs);
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class CalendarTest method test_getDisplayNameIILjava_util_Locale.
/**
* {@link java.util.Calendar#getDisplayName(int, int, Locale)}
* @since 1.6
*/
public void test_getDisplayNameIILjava_util_Locale() {
Calendar cal = Calendar.getInstance();
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
for (Locale locale : locales) {
DateFormatSymbols symbols = new DateFormatSymbols(locale);
String value = null;
switch(field) {
case Calendar.AM_PM:
cal.set(Calendar.AM_PM, Calendar.AM);
value = symbols.getAmPmStrings()[0];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
cal.set(Calendar.AM_PM, Calendar.PM);
value = symbols.getAmPmStrings()[1];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
break;
case Calendar.ERA:
cal.set(Calendar.ERA, GregorianCalendar.BC);
value = symbols.getEras()[0];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
cal.set(Calendar.ERA, GregorianCalendar.AD);
value = symbols.getEras()[1];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
break;
case Calendar.MONTH:
cal.set(Calendar.DAY_OF_MONTH, 1);
for (int month = 0; month <= 11; month++) {
cal.set(Calendar.MONTH, month);
value = symbols.getShortMonths()[month];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
value = symbols.getMonths()[month];
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
}
break;
case Calendar.DAY_OF_WEEK:
for (int day = 1; day <= 7; day++) {
cal.set(Calendar.DAY_OF_WEEK, day);
value = symbols.getShortWeekdays()[day];
assertEquals(cal.getDisplayName(field, Calendar.SHORT, locale), value);
value = symbols.getWeekdays()[day];
assertEquals(cal.getDisplayName(field, Calendar.LONG, locale), value);
}
break;
default:
assertNull(cal.getDisplayName(field, Calendar.SHORT, locale));
assertNull(cal.getDisplayName(field, Calendar.LONG, locale));
}
}
}
cal.setLenient(true);
try {
cal.getDisplayName(-1, Calendar.SHORT, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.MONTH, -1, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.MONTH, 3, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, null);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
try {
cal.getDisplayName(-1, Calendar.SHORT, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.MONTH, -1, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
// in lenient mode, following cases pass
cal.set(Calendar.SECOND, 999);
cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
// test for ALL_STYLES, it is equal to use SHORT
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
for (Locale locale : locales) {
String result = cal.getDisplayName(field, Calendar.ALL_STYLES, locale);
if (field == Calendar.AM_PM || field == Calendar.ERA || field == Calendar.MONTH || field == Calendar.DAY_OF_WEEK) {
assertEquals(result, cal.getDisplayName(field, Calendar.SHORT, locale));
} else {
assertNull(result);
}
}
}
// invalid value for an un-related field when the calendar is not
// lenient
cal.setLenient(false);
assertNotNull(cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));
cal.set(Calendar.SECOND, 999);
try {
cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayName(Calendar.MONTH, Calendar.ALL_STYLES, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class CalendarTest method test_getDisplayNamesIILjava_util_Locale.
/**
* {@link java.util.Calendar#getDisplayNames(int, int, Locale)}
* @since 1.6
*/
public void test_getDisplayNamesIILjava_util_Locale() {
assertEquals(0, Calendar.ALL_STYLES);
assertEquals(1, Calendar.SHORT);
assertEquals(2, Calendar.LONG);
Calendar cal = Calendar.getInstance(Locale.US);
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
for (Locale locale : locales) {
Map<String, Integer> shortResult = cal.getDisplayNames(field, Calendar.SHORT, locale);
Map<String, Integer> longResult = cal.getDisplayNames(field, Calendar.LONG, locale);
Map<String, Integer> allResult = cal.getDisplayNames(field, Calendar.ALL_STYLES, locale);
DateFormatSymbols symbols = new DateFormatSymbols(locale);
String[] values = null;
switch(field) {
case Calendar.AM_PM:
case Calendar.ERA:
values = (field == Calendar.AM_PM) ? symbols.getAmPmStrings() : symbols.getEras();
assertDisplayNameMap(values, shortResult, 0);
assertDisplayNameMap(values, longResult, 0);
assertDisplayNameMap(values, allResult, 0);
break;
case Calendar.MONTH:
values = symbols.getShortMonths();
assertDisplayNameMap(values, shortResult, 0);
values = symbols.getMonths();
assertDisplayNameMap(values, longResult, 0);
assertTrue(allResult.size() >= shortResult.size());
assertTrue(allResult.size() >= longResult.size());
assertTrue(allResult.size() <= shortResult.size() + longResult.size());
break;
case Calendar.DAY_OF_WEEK:
values = symbols.getShortWeekdays();
assertDisplayNameMap(values, shortResult, 1);
values = symbols.getWeekdays();
assertDisplayNameMap(values, longResult, 1);
assertTrue(allResult.size() >= shortResult.size());
assertTrue(allResult.size() >= longResult.size());
assertTrue(allResult.size() <= shortResult.size() + longResult.size());
break;
default:
assertNull(shortResult);
assertNull(longResult);
assertNull(allResult);
}
}
}
cal.setLenient(true);
try {
cal.getDisplayNames(-1, Calendar.SHORT, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayNames(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayNames(Calendar.MONTH, -1, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayNames(Calendar.MONTH, 3, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, null);
fail("Should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
try {
cal.getDisplayNames(-1, Calendar.SHORT, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
try {
cal.getDisplayNames(Calendar.MONTH, -1, null);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
cal.set(Calendar.SECOND, 999);
cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US);
// RI fails here
// invalid value for an un-related field when the calendar is not
// lenient
cal.setLenient(false);
cal.set(Calendar.SECOND, 999);
try {
cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US);
fail("Should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// expected
}
}
Aggregations