commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c...@apache.org
Subject [lang] LANG-1192 partial: implement format 'u' for FastDateFormat, day number of week
Date Sat, 12 Dec 2015 04:39:09 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master bfcdeeaf4 -> 2ebf9a21d


LANG-1192 partial: implement format 'u' for FastDateFormat, day number of week


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/2ebf9a21
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/2ebf9a21
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/2ebf9a21

Branch: refs/heads/master
Commit: 2ebf9a21d2dc99ab2f434111cac9dcd6da99d574
Parents: bfcdeea
Author: Chas Honton <chas@apache.org>
Authored: Fri Dec 11 20:36:22 2015 -0800
Committer: Chas Honton <chas@apache.org>
Committed: Fri Dec 11 20:36:22 2015 -0800

----------------------------------------------------------------------
 .../commons/lang3/time/FastDateParser.java      |  8 ++++++
 .../commons/lang3/time/FastDatePrinter.java     | 30 ++++++++++++++++++++
 .../commons/lang3/time/FastDateParserTest.java  | 15 ++++++++++
 .../commons/lang3/time/FastDatePrinterTest.java | 15 ++++++++++
 4 files changed, 68 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2ebf9a21/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
index 26538d7..4dc897b 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -601,6 +601,8 @@ public class FastDateParser implements DateParser, Serializable {
             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 @@ public class FastDateParser implements DateParser, Serializable {
     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)
{

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2ebf9a21/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
index 3f18cbc..4f84cc7 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
@@ -249,6 +249,9 @@ public class FastDatePrinter implements DatePrinter, Serializable {
             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 class FastDatePrinter implements DatePrinter, Serializable {
         }
     }
 
+    /**
+     * <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
=

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2ebf9a21/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
index 4c18e64..5c52a6c 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
@@ -702,4 +702,19 @@ public class FastDateParserTest {
         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));
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2ebf9a21/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
index 5de68d6..1a2701b 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
@@ -420,4 +420,19 @@ public class FastDatePrinterTest {
         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()));
+    }
 }


Mime
View raw message