Java TimeUnit Calculate getFragment(final Calendar calendar, final int fragment, final TimeUnit unit)

Here you can find the source of getFragment(final Calendar calendar, final int fragment, final TimeUnit unit)

Description

Gets a Calendar fragment for any unit.

License

Apache License

Parameter

Parameter Description
calendar the calendar to work with, not null
fragment the Calendar field part of calendar to calculate
unit the time unit

Exception

Parameter Description
IllegalArgumentException if the date is <code>null</code> or fragment is not supported

Return

number of units within the fragment of the calendar

Declaration

private static long getFragment(final Calendar calendar, final int fragment, final TimeUnit unit) 

Method Source Code

//package com.java2s;
/*/* w  w w .j a  v a  2  s.c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;
import java.util.Date;

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Gets a Date fragment for any unit.
     * 
     * @param date the date to work with, not null
     * @param fragment the Calendar field part of date to calculate 
     * @param unit the time unit
     * @return number of units within the fragment of the date
     * @throws IllegalArgumentException if the date is <code>null</code> or 
     * fragment is not supported
     * @since 2.4
     */
    private static long getFragment(final Date date, final int fragment, final TimeUnit unit) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getFragment(calendar, fragment, unit);
    }

    /**
     * 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
     */
    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;

        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;
    }
}

Related

  1. getDate(final TimeUnit unit, final int offset)
  2. getDateDiff(Date date1, Date date2, TimeUnit timeUnit)
  3. getDateDiff(final Date d1, final Date d2, final TimeUnit timeUnit)
  4. getDifference(Calendar initDate, Calendar endDate, TimeUnit units)
  5. getExpiringMap(long time, TimeUnit unit)
  6. getFutureDate(long delay, TimeUnit timeUnit)
  7. getInterval(final Date startDate, final Date endDate, final TimeUnit timeUnit)
  8. getIntervalInfo(long intervalDuration, TimeUnit unit)
  9. getMilliseconds(int interval, TimeUnit unit)