Java Calendar Truncate truncCalendarToQuarter(Calendar c)

Here you can find the source of truncCalendarToQuarter(Calendar c)

Description

truncate the calendar to half year (i.e.

License

Open Source License

Declaration

public static void truncCalendarToQuarter(Calendar c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://w  w  w  .j  ava 2 s  .  c o m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Calendar;

public class Main {
    /**
     * truncate the calendar to half year (i.e. jan 1, apr 1, jul 1 or oct 1 of the given year)
     *
     * @since 4.2
     */
    public static void truncCalendarToQuarter(Calendar c) {
        if (c == null) {
            return;
        }
        final int month = c.get(Calendar.MONTH);
        truncCalendarToYear(c);
        int quarterMonth = Calendar.JANUARY;
        switch (month) {
        case Calendar.APRIL:
        case Calendar.MAY:
        case Calendar.JUNE:
            quarterMonth = Calendar.APRIL;
            break;
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.SEPTEMBER:
            quarterMonth = Calendar.JULY;
            break;
        case Calendar.OCTOBER:
        case Calendar.NOVEMBER:
        case Calendar.DECEMBER:
            quarterMonth = Calendar.OCTOBER;
            break;
        }
        if (quarterMonth != Calendar.JANUARY) {
            c.set(Calendar.MONTH, quarterMonth);
        }
    }

    /**
     * truncate the calendar to year
     */
    public static void truncCalendarToYear(Calendar c) {
        if (c == null) {
            return;
        }
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }
}

Related

  1. trimCalendar(Calendar cal)
  2. trunc(Calendar calendar)
  3. truncate(Calendar calendar)
  4. truncateCal(final Calendar cal)
  5. truncCalendar(Calendar c)