Android Milliseconds Calculate getMillisPerUnit(int unit)

Here you can find the source of getMillisPerUnit(int unit)

Description

Returns the number of milliseconds of a Calendar field, if this is a constant value.

License

Apache License

Parameter

Parameter Description
unit a Calendar field constant which is a valid unit for a fragment

Exception

Parameter Description
IllegalArgumentException if date can't be represented in milliseconds

Return

the number of milliseconds in the field

Declaration

private static long getMillisPerUnit(int unit) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .  ja v  a  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;

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;

    /**
     * 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. millisecondsAfterNearestFifteenMinutes( String date, String time, int milliseconds)
  2. millisecondsAfter(String date, String time, int milliseconds)
  3. formatDuration(final Resources res, final long millis)
  4. getUTCTimeMillis()
  5. getTimeZoneOffsetInMillis()
  6. roundMicrosToMillis(String micros)
  7. currentDateMillis()