LANG-1192 partial: implement format 'u' for FastDateFormat, day number of week
This commit is contained in:
parent
bfcdeeaf44
commit
2ebf9a21d2
|
@ -601,6 +601,8 @@ private Strategy getStrategy(char f, int width, final Calendar definingCalendar)
|
|||
return MINUTE_STRATEGY;
|
||||
case 's':
|
||||
return SECOND_STRATEGY;
|
||||
case 'u':
|
||||
return DAY_OF_WEEK_STRATEGY;
|
||||
case 'w':
|
||||
return WEEK_OF_YEAR_STRATEGY;
|
||||
case 'y':
|
||||
|
@ -941,6 +943,12 @@ int modify(FastDateParser parser, final int iValue) {
|
|||
private static final Strategy WEEK_OF_MONTH_STRATEGY = new NumberStrategy(Calendar.WEEK_OF_MONTH);
|
||||
private static final Strategy DAY_OF_YEAR_STRATEGY = new NumberStrategy(Calendar.DAY_OF_YEAR);
|
||||
private static final Strategy DAY_OF_MONTH_STRATEGY = new NumberStrategy(Calendar.DAY_OF_MONTH);
|
||||
private static final Strategy DAY_OF_WEEK_STRATEGY = new NumberStrategy(Calendar.DAY_OF_WEEK) {
|
||||
@Override
|
||||
int modify(FastDateParser parser, final int iValue) {
|
||||
return iValue != 7 ? iValue + 1 : Calendar.SUNDAY;
|
||||
}
|
||||
};
|
||||
private static final Strategy DAY_OF_WEEK_IN_MONTH_STRATEGY = new NumberStrategy(Calendar.DAY_OF_WEEK_IN_MONTH);
|
||||
private static final Strategy HOUR_OF_DAY_STRATEGY = new NumberStrategy(Calendar.HOUR_OF_DAY);
|
||||
private static final Strategy HOUR24_OF_DAY_STRATEGY = new NumberStrategy(Calendar.HOUR_OF_DAY) {
|
||||
|
|
|
@ -249,6 +249,9 @@ protected List<Rule> parsePattern() {
|
|||
case 'E': // day in week (text)
|
||||
rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays);
|
||||
break;
|
||||
case 'u': // day in week (number)
|
||||
rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen));
|
||||
break;
|
||||
case 'D': // day in year (number)
|
||||
rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen);
|
||||
break;
|
||||
|
@ -1172,6 +1175,33 @@ public void appendTo(final Appendable buffer, final int value) throws IOExceptio
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Inner class to output the numeric day in week.</p>
|
||||
*/
|
||||
private static class DayInWeekField implements NumberRule {
|
||||
private final NumberRule mRule;
|
||||
|
||||
DayInWeekField(final NumberRule rule) {
|
||||
mRule = rule;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int estimateLength() {
|
||||
return mRule.estimateLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
|
||||
int value = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
mRule.appendTo(buffer, value != Calendar.SUNDAY ? value - 1 : 7);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendTo(final Appendable buffer, final int value) throws IOException {
|
||||
mRule.appendTo(buffer, value);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
private static final ConcurrentMap<TimeZoneDisplayKey, String> cTimeZoneDisplayCache =
|
||||
|
|
|
@ -702,4 +702,19 @@ public void testParseOffset() throws ParseException {
|
|||
cal.set(2015, Calendar.JULY, 4);
|
||||
Assert.assertEquals(cal.getTime(), date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDayNumberOfWeek() throws ParseException {
|
||||
final DateParser parser = getInstance("u");
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
calendar.setTime(parser.parse("1"));
|
||||
Assert.assertEquals(Calendar.MONDAY, calendar.get(Calendar.DAY_OF_WEEK));
|
||||
|
||||
calendar.setTime(parser.parse("6"));
|
||||
Assert.assertEquals(Calendar.SATURDAY, calendar.get(Calendar.DAY_OF_WEEK));
|
||||
|
||||
calendar.setTime(parser.parse("7"));
|
||||
Assert.assertEquals(Calendar.SUNDAY, calendar.get(Calendar.DAY_OF_WEEK));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -420,4 +420,19 @@ public void testAppendableOptions() {
|
|||
long epoch = date.getTime();
|
||||
assertEquals(expected, format.format(epoch, sb).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDayNumberOfWeek() {
|
||||
final DatePrinter printer = getInstance("u");
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
||||
assertEquals("1", printer.format(calendar.getTime()));
|
||||
|
||||
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
|
||||
assertEquals("6", printer.format(calendar.getTime()));
|
||||
|
||||
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
||||
assertEquals("7", printer.format(calendar.getTime()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue