Java Calendar Time merge(Calendar date, Calendar time)

Here you can find the source of merge(Calendar date, Calendar time)

Description

Merge two calendars, one with the day date component and the other with a time component.

License

Apache License

Parameter

Parameter Description
date a parameter
time a parameter

Declaration

public static Calendar merge(Calendar date, Calendar time) 

Method Source Code


//package com.java2s;
/*// w ww  .  j  a v  a2 s  . com
 * Copyright 2007-2011 the original author or authors.
 *
 * 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 {
    /**
     * Merge two calendars, one with the day date component and the other
     * with a time component. The resutling calendar is of the date's
     * locale and timezone. If the time argument is null the method returns
     * only the date's date with 00:00:00 time. 
     * If the date is null current date is used
     * 
     * @param date
     * @param time
     * @return
     */
    public static Calendar merge(Calendar date, Calendar time) {
        Calendar resCal = Calendar.getInstance();
        if (date == null && time == null)
            return resCal;
        if (date != null)
            resCal = (Calendar) date.clone();
        resCal.clear();
        if (null == time) {
            resCal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
            return resCal;
        }
        resCal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH),
                time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
        return resCal;
    }

    /**
     * Merge a date's date part with another dates time part
     * @param date
     * @param time
     * @return
     * @deprecated
     */
    public static Date merge(Date date, Date time) {
        Calendar dateCal = Calendar.getInstance();
        dateCal.setTime(date);
        if (time == null)
            return merge(dateCal, null).getTime();
        Calendar timeCal = Calendar.getInstance();
        timeCal.setTime(time);
        return merge(dateCal, timeCal).getTime();
    }
}

Related

  1. isSameTime(final Calendar aCal1, final Calendar aCal2)
  2. isSleepTime(Calendar calendar, String sleepTimeStart, String sleepTimeEnd)
  3. isTime0(final Calendar calendar)
  4. isWorkTime(Calendar calendar, String workTimeStart, String workTimeEnd)
  5. maximizeTimeFields(final Calendar calendar)
  6. mergeDateAndTime(final Calendar date, final Calendar time)
  7. mergeDateTime(Calendar date, Calendar time)
  8. newCalendar(final long time)
  9. newCalendarInstance(TimeZone timeZone)