Java Day of Week getWeekOfTheYear(final Date dateOfYear)

Here you can find the source of getWeekOfTheYear(final Date dateOfYear)

Description

Returns the numerical week of the year given a date.

License

Open Source License

Parameter

Parameter Description
dateOfYear the base date to work from

Return

the week of the year

Declaration

public static int getWeekOfTheYear(final Date dateOfYear) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w.  j av  a  2 s  . co m*/
 * jGnash, a personal finance application
 * Copyright (C) 2001-2015 Craig Cavanaugh
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**
     * ThreadLocal for a {@code GregorianCalendar}
     */
    private static final ThreadLocal<GregorianCalendar> gregorianCalendarThreadLocal = new ThreadLocal<GregorianCalendar>() {
        @Override
        protected GregorianCalendar initialValue() {
            return new GregorianCalendar();
        }
    };

    /**
     * Returns the numerical week of the year given a date.
     * <p/>
     * Minimal days of week is set to 4 to comply with ISO 8601
     *
     * @param dateOfYear the base date to work from
     * @return the week of the year
     */
    public static int getWeekOfTheYear(final Date dateOfYear) {
        final GregorianCalendar c = gregorianCalendarThreadLocal.get();

        int minimal = c.getMinimalDaysInFirstWeek();

        c.setTime(dateOfYear);
        c.setMinimalDaysInFirstWeek(4);

        int returnVal = c.get(Calendar.WEEK_OF_YEAR);

        c.setMinimalDaysInFirstWeek(minimal); // restore original minimal

        return returnVal;
    }
}

Related

  1. getWeekOfDay(Date date)
  2. getWeekOfMonth(Date date)
  3. getWeekOfMonth(final Date date)
  4. getWeekOfMonthFirstDay(Date dt)
  5. getWeekOfTheYear(Date aDate)
  6. getWeekOfYear(Date d, Locale locale)
  7. getWeekOfYear(Date date)
  8. getWeekOfYear(Date date)
  9. getWeekOfYear(Date date)