code: @GwtIncompatible("incompatible method")
      private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) {
          if(calendar == null) {
              throw  new IllegalArgumentException("The date must not be null");
          }
  
          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;
      }
docs: Gets a Calendar fragment for any unit.
  
  @param calendar the calendar to work with, not null
  @param fragment the Calendar field part of calendar to calculate
  @param unit the time unit
  @return number of units within the fragment of the calendar
  @throws IllegalArgumentException if the date is <code>null</code> or
  fragment is not supported
  @since 2.4