Java Calendar Year truncCalendarToHalfYear(Calendar c)

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

Description

truncate the calendar to half year (i.e.

License

Open Source License

Declaration

public static void truncCalendarToHalfYear(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:/*from  w  ww . j a v  a 2  s.com*/
 *     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 or jul 1 of the given year)
     *
     * @since 4.2
     */
    public static void truncCalendarToHalfYear(Calendar c) {
        if (c == null) {
            return;
        }
        int month = c.get(Calendar.MONTH);
        truncCalendarToYear(c);
        if (month >= Calendar.JULY) {
            c.set(Calendar.MONTH, Calendar.JULY);
        }
    }

    /**
     * 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. sameYear(Calendar one, Calendar two)
  2. setCalendarFields(Calendar cal, int year, int month, int day, int hour, int minute, int second, int millis)
  3. setDate(Calendar cal, int year, int month, int day)
  4. toCalendar(int year, int month, int day, int hour, int minutes, int seconds)
  5. toStringYearMonth(Calendar calendar)
  6. truncCalendarToYear(Calendar c)
  7. year(Calendar calendar)
  8. year(Calendar date)