Java Week Calculate weekCal(int weekNumber)

Here you can find the source of weekCal(int weekNumber)

Description

week Cal

License

LGPL

Return

the week start-date that is weekNumber offset from INITIAL_YEAR

Declaration

public static GregorianCalendar weekCal(int weekNumber) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    public static final int INITIAL_YEAR = 2000;
    /** First day of work within a week */
    public static final int FIRST_DAY_OF_WORK = Calendar.MONDAY;

    /**//w  w  w. ja v  a 2  s  .c  om
       @return the week start-date that is weekNumber offset from INITIAL_YEAR
     */
    public static GregorianCalendar weekCal(int weekNumber) {
        int yearNum = INITIAL_YEAR + (weekNumber / 52);
        int weekNum = weekNumber % 52;
        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum);
        return weekStart(cal);
    }

    /**
       @return the starting moment of the current week
     */
    public static GregorianCalendar weekStart() {
        return weekStart(new GregorianCalendar());
    }

    /**
       @return the starting moment of the week containing the given date
       Note that it is set to the FIRST_DAY_OF_WORK.
     */
    public static GregorianCalendar weekStart(GregorianCalendar cal) {
        GregorianCalendar newCal = (GregorianCalendar) cal.clone();
        newCal.set(Calendar.DAY_OF_WEEK, FIRST_DAY_OF_WORK);
        newCal.set(Calendar.HOUR_OF_DAY, 0);
        newCal.set(Calendar.MINUTE, 0);
        newCal.set(Calendar.SECOND, 0);
        newCal.set(Calendar.MILLISECOND, 0);
        return newCal;
    }
}

Related

  1. isSameWeekDates(Date date1, Date date2)
  2. nextWeek(Date date, int week)
  3. nextWeek(long date)
  4. plusHour(Date date, int plusWeek)
  5. startOfWeek(Date datum)
  6. weekCount(Date start, Date end)
  7. weekNumber()
  8. weeksInYear(int year)
  9. weekStart()