Java Month of Year getMonth(int week, int year)

Here you can find the source of getMonth(int week, int year)

Description

Gets the month.

License

Open Source License

Parameter

Parameter Description
week the week
year the year

Return

the month

Declaration

public static int getMonth(int week, int year) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    /**//from  w  ww.j  av a2 s .  c  o m
     * Gets the month.
     * 
     * @param week the week
     * @param year the year
     * 
     * @return the month
     */
    public static int getMonth(int week, int year) {
        Calendar cal = getFirstSundayDateInYear(year);
        cal.add(Calendar.DAY_OF_YEAR, (week - 1) * 7);
        return cal.get(Calendar.MONTH);
    }

    /**
     * Gets the first sunday date in year.
     * 
     * @param year the year
     * 
     * @return the first sunday date in year
     */
    public static Calendar getFirstSundayDateInYear(int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.DAY_OF_YEAR, 8);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            cal.set(Calendar.DAY_OF_YEAR, 7);
        int daydiff = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
        cal.add(Calendar.DATE, daydiff * -1);
        return cal;
    }
}

Related

  1. getLastMonthDate()
  2. getLastMonthDate(int date)
  3. getLastMonthOfQuater(Date date)
  4. getLastMonthOfYear()
  5. getMaxDayOfMonth(int year, int month)
  6. getMonthDays(Object year, Object month)
  7. getMonthHaveDays(int year, int month)
  8. getMonthOfYear(int m)
  9. getMonthOfYear(long time)