Fixes Issue #1271.
DateTimeFormat.parse method now uses 00:00:00 (as opposed to the current time)
to fill in any missing parts when parsing a partial date string.
Found by: calinm
Patch by: jlabanca
Review by: knorton
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1281 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/i18n/client/DateTimeFormat.java b/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
index f93c269..4fa0aa9 100644
--- a/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
+++ b/user/src/com/google/gwt/i18n/client/DateTimeFormat.java
@@ -198,8 +198,9 @@
*
* <h3>Parsing Dates and Times</h3>
* <p>
- * This implementation could parse partial date/time. Current date/time will be
- * used to fill in the unavailable part.
+ * This implementation could parse partial date/time. Current date will be
+ * used to fill in the unavailable date part. 00:00:00 will be used to
+ * fill in the time part.
* </p>
*
* <p>
@@ -591,6 +592,9 @@
*/
public Date parse(String text) {
Date date = new Date();
+ date.setHours(0);
+ date.setMinutes(0);
+ date.setSeconds(0);
int charsConsumed = parse(text, 0, date);
if (charsConsumed == 0 || charsConsumed < text.length()) {
throw new IllegalArgumentException(text);
diff --git a/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java b/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
index 5d8ce47..047cacd 100644
--- a/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
+++ b/user/test/com/google/gwt/i18n/client/DateTimeParse_en_Test.java
@@ -432,6 +432,22 @@
assertTrue(date.getDate() == 29);
}
+ public void testPartialParsing() {
+ // Only specify a date
+ DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
+ Date dateOnly = fmt.parse("05-06-1987");
+ assertEquals(dateOnly.getHours(), 0);
+ assertEquals(dateOnly.getMinutes(), 0);
+ assertEquals(dateOnly.getSeconds(), 0);
+
+ // Only specify a time, should use current date
+ fmt = DateTimeFormat.getFormat("hha");
+ dateOnly = fmt.parse("4PM");
+ assertEquals(dateOnly.getHours(), 16);
+ assertEquals(dateOnly.getMinutes(), 0);
+ assertEquals(dateOnly.getSeconds(), 0);
+ }
+
public void testRFC3339() {
Date date = new Date();