Java Calendar Year getYearForWeek(final Calendar cal)

Here you can find the source of getYearForWeek(final Calendar cal)

Description

found here: http://www.odi.ch/prog/design/datetime.php

License

Open Source License

Parameter

Parameter Description
cal a parameter

Declaration

public static int getYearForWeek(final Calendar cal) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005, 2016 Wolfgang Schramm and Contributors
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation version 2 of the License.//  www  . j  a v  a2 s.  c o  m
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *******************************************************************************/

import java.util.Calendar;

public class Main {
    /**
     * found here: http://www.odi.ch/prog/design/datetime.php
     * 
     * @param cal
     * @return
     */
    public static int getYearForWeek(final Calendar cal) {

        final int year = cal.get(Calendar.YEAR);
        final int week = cal.get(Calendar.WEEK_OF_YEAR);
        final int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        if (week == 1 && dayOfMonth > 20) {
            return year + 1;
        }

        if (week >= 52 && dayOfMonth < 10) {
            return year - 1;
        }

        return year;
    }
}

Related

  1. getYear(Calendar cal)
  2. getYear(Calendar calendar)
  3. getYear(Calendar calendar)
  4. getYear(Calendar calendar)
  5. getYear(Date date, Calendar cal)
  6. getYearList(Calendar currDate)
  7. isBeforeDay(Calendar date1, Calendar date2, boolean ignoreYear)
  8. isSameDay(Calendar date1, Calendar date2, boolean ignoreYear)
  9. isSameDayOfYear(Calendar calendar1, Calendar calendar2)