Skip to content
Browse files

Fix to avoid exception for times before 1970 with non-zero milli-of-s…

…econd
  • Loading branch information...
1 parent 5d0f10b commit a9f1ae91311276c5321e23f880aa3b0078e1d191 @jodastephen jodastephen committed Sep 13, 2012
View
4 RELEASE-NOTES.txt
@@ -55,6 +55,10 @@ Bug fixes since 2.1
Addition and subtraction in MonthDay was fixed.
It previously didn't work when the start value was 29th February.
+- LocalDateTime.fromDateFields()
+ This used to fail before 1970 when the input had a milli-of-second field
+ Fixed to not throw an exception
+
- DateTimeFormatter.parseInto() [3522138]
The v2.0 changes to handle parsing of month/day on their own (for Feb 29th) cause
parseInto() to lose the year. This fix reverts behaviour to v1.x so that parseInto()
View
2 src/main/java/org/joda/time/LocalDateTime.java
@@ -238,7 +238,7 @@ public static LocalDateTime fromDateFields(Date date) {
date.getHours(),
date.getMinutes(),
date.getSeconds(),
- (int) (date.getTime() % 1000)
+ (((int) (date.getTime() % 1000)) + 1000) % 1000
);
}
View
13 src/test/java/org/joda/time/TestLocalDateTime_Constructors.java
@@ -114,7 +114,7 @@ public void testFactory_FromCalendarFields() throws Exception {
}
//-----------------------------------------------------------------------
- public void testFactory_FromDateFields() throws Exception {
+ public void testFactory_FromDateFields_after1970() throws Exception {
GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
cal.set(Calendar.MILLISECOND, 7);
LocalDateTime expected = new LocalDateTime(1970, 2, 3, 4, 5 ,6, 7);
@@ -125,6 +125,17 @@ public void testFactory_FromDateFields() throws Exception {
} catch (IllegalArgumentException ex) {}
}
+ public void testFactory_FromDateFields_before1970() throws Exception {
+ GregorianCalendar cal = new GregorianCalendar(1969, 1, 3, 4, 5, 6);
+ cal.set(Calendar.MILLISECOND, 7);
+ LocalDateTime expected = new LocalDateTime(1969, 2, 3, 4, 5 ,6, 7);
+ assertEquals(expected, LocalDateTime.fromDateFields(cal.getTime()));
+ try {
+ LocalDateTime.fromDateFields((Date) null);
+ fail();
+ } catch (IllegalArgumentException ex) {}
+ }
+
//-----------------------------------------------------------------------
public void testConstructor() throws Throwable {
LocalDateTime test = new LocalDateTime();

0 comments on commit a9f1ae9

Please sign in to comment.
Something went wrong with that request. Please try again.