in src/main/java/org/apache/commons/lang3/time/DateUtils.java [1598:1638]
private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) {
Objects.requireNonNull(calendar, "calendar");
long result = 0;
final int offset = (unit == TimeUnit.DAYS) ? 0 : 1;
// Fragments bigger than a day require a breakdown to days
switch (fragment) {
case Calendar.YEAR:
result += unit.convert(calendar.get(Calendar.DAY_OF_YEAR) - offset, TimeUnit.DAYS);
break;
case Calendar.MONTH:
result += unit.convert(calendar.get(Calendar.DAY_OF_MONTH) - offset, TimeUnit.DAYS);
break;
default:
break;
}
switch (fragment) {
// Number of days already calculated for these cases
case Calendar.YEAR:
case Calendar.MONTH:
// The rest of the valid cases
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result += unit.convert(calendar.get(Calendar.HOUR_OF_DAY), TimeUnit.HOURS);
//$FALL-THROUGH$
case Calendar.HOUR_OF_DAY:
result += unit.convert(calendar.get(Calendar.MINUTE), TimeUnit.MINUTES);
//$FALL-THROUGH$
case Calendar.MINUTE:
result += unit.convert(calendar.get(Calendar.SECOND), TimeUnit.SECONDS);
//$FALL-THROUGH$
case Calendar.SECOND:
result += unit.convert(calendar.get(Calendar.MILLISECOND), TimeUnit.MILLISECONDS);
break;
case Calendar.MILLISECOND: break; //never useful
default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
}
return result;
}