Java TimeZone Usage mergeTime(Calendar date, Date time)

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

Description

Set the TIME (Hour, Minute and Second) values for the start (and end mergeTime if set) to match the mergeTime (HMS) values of the given date value.

License

Apache License

Parameter

Parameter Description
date the DATETIME calendar - only the TIME values are extracted
time the TIME calendar value - only the TIME values are extracted

Return

a calendar with date values from the date calendar and TIME values from the mergeTime calendar.

Declaration

private static Calendar mergeTime(Calendar date, Date time) 

Method Source Code

//package com.java2s;
/*//from  ww  w.  j av a2 s  .  c  om
 * Copyright 2014 Jesse Caulfield <jesse@caulfield.org>.
 *
 * 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.*;

public class Main {
    public static final java.util.TimeZone TIMEZONE_UTC = java.util.TimeZone
            .getTimeZone("UTC");

    /**
     * Set the TIME (Hour, Minute and Second) values for the start (and end
     * mergeTime if set) to match the mergeTime (HMS) values of the given date
     * value.
     * <p>
     * @param date the DATETIME calendar - only the TIME values are extracted
     * @param time the TIME calendar value - only the TIME values are extracted
     * @return a calendar with date values from the date calendar and TIME values
     *         from the mergeTime calendar.
     */
    private static Calendar mergeTime(Calendar date, Date time) {
        Calendar timeCalendar = Calendar.getInstance(TIMEZONE_UTC);
        timeCalendar.setTime(time);
        return mergeTime(date, timeCalendar);
    }

    /**
     * Set the TIME (Hour, Minute and Second) values for the start (and end
     * mergeTime if set) to match the mergeTime (HMS) values of the given date
     * value.
     * <p>
     * @param date the DATETIME calendar - only the TIME values are extracted
     * @param time the TIME calendar value - only the TIME values are extracted
     * @return a calendar with date values from the date calendar and TIME values
     *         from the mergeTime calendar.
     */
    private static Calendar mergeTime(Calendar date, Calendar time) {
        final Calendar datetime = (Calendar) date.clone();
        for (int calendarField : new int[] { Calendar.HOUR_OF_DAY,
                Calendar.MINUTE, Calendar.SECOND }) {
            datetime.set(calendarField, time.get(calendarField));
        }
        return datetime;
    }
}

Related

  1. isTimeZoneDisponible(TimeZone timeZone)
  2. isValidTimeZone(final String timeZone)
  3. isValidTimezone(String tz)
  4. localeToTimeZone(Locale locale)
  5. localTimeZoneString()
  6. offset(long time)
  7. parseBackendTimeZone(String timeZone)
  8. runInTimeZone(TimeZone timeZone, Runnable action)
  9. serializeTimeZone(TimeZone timeZone)