Java Calendar Time copyLocalTime(final Calendar from, final Calendar to)

Here you can find the source of copyLocalTime(final Calendar from, final Calendar to)

Description

Copy the local time from one calendar to another.

License

Open Source License

Parameter

Parameter Description
from the source calendar, e.g. 23/12/2011 11:55:33.066 GMT-12.
to the destination calendar, e.g. 01/01/2000 0:00 GMT+13.

Exception

Parameter Description
IllegalArgumentException if both calendars aren't from the same class and none ofthem are Gregorian.

Return

the modified destination calendar, e.g. 23/12/2011 11:55:33.066 GMT+13.

Declaration

public final static Calendar copyLocalTime(final Calendar from, final Calendar to)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*//  w  w  w.ja  v a2s .c  o m
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */

import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {
    /**
     * Copy the local time from one calendar to another. Except if both calendars have the same time
     * zone, from.getTimeInMillis() will be different from to.getTimeInMillis().
     * <p>
     * NOTE : In case the two calendars are not from the same class but one of them is a
     * {@link GregorianCalendar} then this method will use a GregorianCalendar with the time zone
     * and absolute time of the other.
     * </p>
     * <p>
     * Also note that the local time of <code>from</code> can be {@link #isAmbiguous(Calendar)
     * ambiguous} in <code>to</code> or skipped. In the former case, the absolute time is
     * unspecified (e.g. 2h30 in WET can either be 2h30 CEST or CET in fall). In the latter case, a
     * round trip back to <code>from</code> will yield a different local time (e.g. in spring 2h30
     * in WET to 3h30 in CEST, back to 3h30 in WEST).
     * </p>
     * 
     * @param from the source calendar, e.g. 23/12/2011 11:55:33.066 GMT-12.
     * @param to the destination calendar, e.g. 01/01/2000 0:00 GMT+13.
     * @return the modified destination calendar, e.g. 23/12/2011 11:55:33.066 GMT+13.
     * @throws IllegalArgumentException if both calendars aren't from the same class and none of
     *         them are Gregorian.
     */
    public final static Calendar copyLocalTime(final Calendar from, final Calendar to)
            throws IllegalArgumentException {
        final boolean sameClass = from.getClass() == to.getClass();
        final boolean createGregSource = !sameClass && to.getClass() == GregorianCalendar.class;
        final boolean createGregDest = !sameClass && from.getClass() == GregorianCalendar.class;
        if (!sameClass && !createGregSource && !createGregDest)
            throw new IllegalArgumentException("Calendars mismatch " + from.getClass() + " != " + to.getClass());

        final Calendar source = createGregSource ? new GregorianCalendar(from.getTimeZone()) : from;
        if (createGregSource) {
            source.setTime(from.getTime());
        }
        final Calendar dest = createGregDest ? new GregorianCalendar(to.getTimeZone()) : to;
        assert source.getClass() == dest.getClass();
        if (source.getTimeZone().equals(dest.getTimeZone())) {
            dest.setTimeInMillis(source.getTimeInMillis());
        } else {
            dest.clear();
            for (final int field : new int[] { Calendar.ERA, Calendar.YEAR, Calendar.DAY_OF_YEAR,
                    Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND }) {
                dest.set(field, source.get(field));
            }
        }
        if (createGregDest) {
            to.setTime(dest.getTime());
        }
        return to;
    }
}

Related

  1. convertTimeToString(Calendar time)
  2. convertToActiveDirectoryTime(Calendar calendar)
  3. convertToCalendar(long adTimeValue)
  4. convertToTimeStpam(Calendar cal)
  5. convertToTimeZone(Calendar time, TimeZone timeZone)
  6. copyLocalTime(final Calendar from, final Calendar to)
  7. currentTimeAsCalendar()
  8. dateTimeToFMDateTime(Calendar dateTime)
  9. elapsedTime(Calendar before, Calendar after)