Java Day of Week getFirstDayWeekly(final int year)

Here you can find the source of getFirstDayWeekly(final int year)

Description

Returns an array of Dates starting with the first day of each week of the year.

License

Open Source License

Parameter

Parameter Description
year The year to generate the array for

Return

The array of dates

Declaration

public static Date[] getFirstDayWeekly(final int year) 

Method Source Code

//package com.java2s;
/*/*from www . jav a2 s.  com*/
 * 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.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

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 an array of Dates starting with the first day of each week of the
     * year.
     *
     * @param year The year to generate the array for
     * @return The array of dates
     * @see <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO_8601</a>
     */
    public static Date[] getFirstDayWeekly(final int year) {
        final GregorianCalendar cal = gregorianCalendarThreadLocal.get();
        final GregorianCalendar testCal = new GregorianCalendar();

        List<Date> dates = new ArrayList<>();

        // level the date
        cal.setTime(trimDate(cal.getTime()));

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.WEEK_OF_YEAR, 1);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());

        for (int i = 0; i < 53; i++) {
            testCal.setTime(cal.getTime());
            testCal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);

            if (testCal.get(Calendar.YEAR) == year) {
                dates.add(cal.getTime());
            }

            cal.add(Calendar.DATE, 7); // add 7 days
        }

        return dates.toArray(new Date[dates.size()]);
    }

    /**
     * Trims the date so that only the day, month, and year are significant.
     *
     * @param date date to trim
     * @return leveled date
     */
    public static Date trimDate(final Date date) {
        final GregorianCalendar c = gregorianCalendarThreadLocal.get();

        c.setTime(date);

        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);

        return c.getTime();
    }
}

Related

  1. getEndOfWeek(Date date)
  2. getFirstDayOfLastWeek(Date date)
  3. getFirstDayOfWeek(int year, int week)
  4. getFirstDayOfWeek(int year, int week)
  5. getFirstDayOfWeek(int year, int week)
  6. getFirstMonthWeek(Date month)
  7. getFirstWeekdayBetween(Date fromDate, Date toDate, int dayOfWeek)
  8. getFirstWeekofMonth(Date date1)
  9. getIntervalIncludeWeekend(Date date)