Java Calendar Time ceilTime(Calendar calendar)

Here you can find the source of ceilTime(Calendar calendar)

Description

Returns a new Calendar with the same date of given calendar but with all time fields setted to their max value ( Calendar#HOUR_OF_DAY to 23, Calendar#MINUTE to 59, Calendar#SECOND to 59, Calendar#MILLISECOND to 999).

License

Apache License

Parameter

Parameter Description
calendar Calendar to ceil

Return

A new Calendar with all time fields setted to their max value, or null if given Calendar was null

Declaration

public static Calendar ceilTime(Calendar calendar) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  . j a v a 2s .com
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed 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 {
    /**
     * Returns a new {@link Calendar} with the same date of given <code>calendar</code> but with all time fields setted
     * to their max value ({@link Calendar#HOUR_OF_DAY} to 23, {@link Calendar#MINUTE} to 59, {@link Calendar#SECOND} to
     * 59, {@link Calendar#MILLISECOND} to 999).
     * @param calendar Calendar to ceil
     * @return A new Calendar with all time fields setted to their max value, or <code>null</code> if given Calendar was
     *         null
     */
    public static Calendar ceilTime(Calendar calendar) {
        if (calendar != null) {
            Calendar c = (Calendar) calendar.clone();
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            c.set(Calendar.MILLISECOND, 999);
            return c;
        }
        return calendar;
    }

    /**
     * Returns a new {@link Date} with the same date of given <code>date</code> but with all time fields setted to their
     * max value ({@link Calendar#HOUR_OF_DAY} to 23, {@link Calendar#MINUTE} to 59, {@link Calendar#SECOND} to 59,
     * {@link Calendar#MILLISECOND} to 999).
     * @param date Date to ceil
     * @return A new Date with all time fields setted to their max value, or <code>null</code> if given Date was null
     */
    public static Date ceilTime(Date date) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            c.set(Calendar.MILLISECOND, 999);
            return c.getTime();
        }
        return date;
    }
}

Related

  1. calendar2TimeStamp(Calendar cal)
  2. calendarFromString(String dateString, String timeString)
  3. calendarToGPSTime(Calendar calendar)
  4. clearCalendarTime(Calendar cal)
  5. clearCalendarTimeParts(Calendar cal)
  6. clearTime(Calendar c)
  7. clearTime(Calendar cal)