Java Calendar Year getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale)

Here you can find the source of getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale)

Description

get Start Of Week Calendar

License

Apache License

Declaration

public static Calendar getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale) 

Method Source Code

//package com.java2s;
/**// w w w  .j  a v a 2  s  .co m
 * OLAT - Online Learning and Training<br>
 * http://www.olat.org
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
 * University of Zurich, Switzerland.
 * <p>
 */

import java.util.Calendar;

import java.util.Locale;

public class Main {
    public static Calendar getStartOfWeekCalendar(final int year, final int weekOfYear, final Locale locale) {
        final Calendar cal = createCalendarInstance(locale);
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    /**
     * Create a calendar instance that uses mondays or sundays as the first day of the week depending on the given locale and sets the week number 1 to the first week in
     * the year that has four days of january.
     * 
     * @param local the locale to define if a week starts on sunday or monday
     * @return a calendar instance
     */
    public static Calendar createCalendarInstance(final Locale locale) {
        // use Calendar.getInstance(locale) that sets first day of week
        // according to locale or let user decide in GUI
        final Calendar cal = Calendar.getInstance(locale);
        // manually set min days to 4 as we are used to have it
        cal.setMinimalDaysInFirstWeek(4);
        return cal;
    }
}

Related

  1. createAndValidateCalendar(int month, int day, int year)
  2. getDecimalYear(Calendar calendar)
  3. getFiscalYear(Calendar calendar)
  4. getIntYear(Calendar c)
  5. getLastYear(Calendar cal)
  6. getValidCalendar(int year, int month, int day)
  7. getWeekOfYear(Calendar currentDate)
  8. getWeekOfYear(int startDay, Calendar dt)
  9. getWeekOfYearYear(GregorianCalendar cal)