1
1
/*
2
- * Copyright 2001-2014 Stephen Colebourne
2
+ * Copyright 2001-2015 Stephen Colebourne
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -146,6 +146,7 @@ public DateTimeZone getZone() {
146
146
return DateTimeZone .UTC ;
147
147
}
148
148
149
+ @ Override
149
150
public long getDateTimeMillis (
150
151
int year , int monthOfYear , int dayOfMonth , int millisOfDay )
151
152
throws IllegalArgumentException {
@@ -156,9 +157,10 @@ public long getDateTimeMillis(
156
157
157
158
FieldUtils .verifyValueBounds
158
159
(DateTimeFieldType .millisOfDay (), millisOfDay , 0 , DateTimeConstants .MILLIS_PER_DAY - 1 );
159
- return getDateMidnightMillis (year , monthOfYear , dayOfMonth ) + millisOfDay ;
160
+ return getDateTimeMillis0 (year , monthOfYear , dayOfMonth , millisOfDay ) ;
160
161
}
161
162
163
+ @ Override
162
164
public long getDateTimeMillis (
163
165
int year , int monthOfYear , int dayOfMonth ,
164
166
int hourOfDay , int minuteOfHour , int secondOfMinute , int millisOfSecond )
@@ -173,12 +175,29 @@ public long getDateTimeMillis(
173
175
FieldUtils .verifyValueBounds (DateTimeFieldType .minuteOfHour (), minuteOfHour , 0 , 59 );
174
176
FieldUtils .verifyValueBounds (DateTimeFieldType .secondOfMinute (), secondOfMinute , 0 , 59 );
175
177
FieldUtils .verifyValueBounds (DateTimeFieldType .millisOfSecond (), millisOfSecond , 0 , 999 );
176
-
177
- return getDateMidnightMillis (year , monthOfYear , dayOfMonth )
178
- + hourOfDay * DateTimeConstants .MILLIS_PER_HOUR
179
- + minuteOfHour * DateTimeConstants .MILLIS_PER_MINUTE
180
- + secondOfMinute * DateTimeConstants .MILLIS_PER_SECOND
181
- + millisOfSecond ;
178
+ long millisOfDay = hourOfDay * DateTimeConstants .MILLIS_PER_HOUR
179
+ + minuteOfHour * DateTimeConstants .MILLIS_PER_MINUTE
180
+ + secondOfMinute * DateTimeConstants .MILLIS_PER_SECOND
181
+ + millisOfSecond ;
182
+ return getDateTimeMillis0 (year , monthOfYear , dayOfMonth , (int ) millisOfDay );
183
+ }
184
+
185
+ private long getDateTimeMillis0 (int year , int monthOfYear , int dayOfMonth , int millisOfDay ) {
186
+ long dayInstant = getDateMidnightMillis (year , monthOfYear , dayOfMonth );
187
+ // try reversed calculation from next day for MIN
188
+ if (dayInstant == Long .MIN_VALUE ) {
189
+ dayInstant = getDateMidnightMillis (year , monthOfYear , dayOfMonth + 1 );
190
+ millisOfDay = millisOfDay - 86400000 ;
191
+ }
192
+ // check for limit caused by millisOfDay addition
193
+ // even if dayInstant already MIN or MAX, this still works fine with int math
194
+ long result = dayInstant + millisOfDay ;
195
+ if (result < 0 && dayInstant > 0 ) {
196
+ return Long .MAX_VALUE ;
197
+ } else if (result > 0 && dayInstant < 0 ) {
198
+ return Long .MIN_VALUE ;
199
+ }
200
+ return result ;
182
201
}
183
202
184
203
public int getMinimumDaysInFirstWeek () {
@@ -608,10 +627,17 @@ int getDaysInMonthMaxForSet(long instant, int value) {
608
627
* @return the milliseconds
609
628
*/
610
629
long getDateMidnightMillis (int year , int monthOfYear , int dayOfMonth ) {
611
- FieldUtils .verifyValueBounds (DateTimeFieldType .year (), year , getMinYear (), getMaxYear ());
630
+ FieldUtils .verifyValueBounds (DateTimeFieldType .year (), year , getMinYear () - 1 , getMaxYear () + 1 );
612
631
FieldUtils .verifyValueBounds (DateTimeFieldType .monthOfYear (), monthOfYear , 1 , getMaxMonth (year ));
613
632
FieldUtils .verifyValueBounds (DateTimeFieldType .dayOfMonth (), dayOfMonth , 1 , getDaysInYearMonth (year , monthOfYear ));
614
- return getYearMonthDayMillis (year , monthOfYear , dayOfMonth );
633
+ long instant = getYearMonthDayMillis (year , monthOfYear , dayOfMonth );
634
+ // check for limit caused by min/max year +1/-1
635
+ if (instant < 0 && year == getMaxYear () + 1 ) {
636
+ return Long .MAX_VALUE ;
637
+ } else if (instant > 0 && year == getMinYear () - 1 ) {
638
+ return Long .MIN_VALUE ;
639
+ }
640
+ return instant ;
615
641
}
616
642
617
643
/**
0 commit comments