Android Date Interval Get getFragment(Date date, int fragment, int unit)

Here you can find the source of getFragment(Date date, int fragment, int unit)

Description

Date-version for fragment-calculation in any unit

License

Apache License

Parameter

Parameter Description
date the date to work with, not null
fragment the Calendar field part of date to calculate
unit the Calendar field defining the 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 date

Declaration

private static long getFragment(Date date, int fragment, int unit) 

Method Source Code

//package com.java2s;
/*//from w  ww  . j a va 2 s . c  om
 * 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;

public class Main {
    /**
     * Number of milliseconds in a standard second.
     * @since 2.1
     */
    public static final long MILLIS_PER_SECOND = 1000;
    /**
     * Number of milliseconds in a standard minute.
     * @since 2.1
     */
    public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
    /**
     * Number of milliseconds in a standard hour.
     * @since 2.1
     */
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
    /**
     * Number of milliseconds in a standard day.
     * @since 2.1
     */
    public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;

    /**
     * Date-version for fragment-calculation in any unit
     * 
     * @param date the date to work with, not null
     * @param fragment the Calendar field part of date to calculate 
     * @param unit the {@code Calendar} field defining the 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(Date date, int fragment, int unit) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getFragment(calendar, fragment, unit);
    }

    /**
     * Calendar-version for fragment-calculation in any unit
     * 
     * @param calendar the calendar to work with, not null
     * @param fragment the Calendar field part of calendar to calculate 
     * @param unit the {@code Calendar} field defining the 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(Calendar calendar, int fragment,
            int unit) {
        if (calendar == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        long millisPerUnit = getMillisPerUnit(unit);
        long result = 0;

        // Fragments bigger than a day require a breakdown to days
        switch (fragment) {
        case Calendar.YEAR:
            result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY)
                    / millisPerUnit;
            break;
        case Calendar.MONTH:
            result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY)
                    / millisPerUnit;
            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 += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR)
                    / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.HOUR_OF_DAY:
            result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE)
                    / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.MINUTE:
            result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND)
                    / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.SECOND:
            result += (calendar.get(Calendar.MILLISECOND) * 1)
                    / millisPerUnit;
            break;
        case Calendar.MILLISECOND:
            break;//never useful
        default:
            throw new IllegalArgumentException("The fragment " + fragment
                    + " is not supported");
        }
        return result;
    }

    /**
     * Returns the number of milliseconds of a {@code Calendar} field, if this is a constant value.
     * This handles millisecond, second, minute, hour and day (even though days can very in length).
     * 
     * @param unit  a {@code Calendar} field constant which is a valid unit for a fragment
     * @return the number of milliseconds in the field
     * @throws IllegalArgumentException if date can't be represented in milliseconds
     * @since 2.4 
     */
    private static long getMillisPerUnit(int unit) {
        long result = Long.MAX_VALUE;
        switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default:
            throw new IllegalArgumentException("The unit " + unit
                    + " cannot be represented is milleseconds");
        }
        return result;
    }
}

Related

  1. getDiffDays(String startDate, String endDate)
  2. getDiffDays(String startDate, String endDate)
  3. getDiffHour(String startTime, String endTime)
  4. getDiffHour(String startTime, String endTime)
  5. getFragment(Calendar calendar, int fragment, int unit)
  6. getFragmentInDays(Date date, int fragment)
  7. getFragmentInHours(Date date, int fragment)
  8. getInterval(String beginMonth, String endMonth)
  9. getIntervalDays(Date minDate, Date maxDate)