Java Day of Week getYearOfWeek(Date date)

Here you can find the source of getYearOfWeek(Date date)

Description

get Year Of Week

License

Apache License

Declaration

public static Integer getYearOfWeek(Date date) 

Method Source Code

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

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

public class Main {
    public static int FIRST_DAY_OF_WEEK = Calendar.MONDAY;

    public static Integer getYearOfWeek(Date date) {
        Date sunday = getSundayOfWeek(date);
        return getYear(sunday);
    }//ww w  .  j  av  a2s. c  o  m

    public static Date getSundayOfWeek(Date date) {
        Calendar sunday = getCalendar();
        sunday.setTime(date);
        sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sunday.getTime();
    }

    public static Date getSundayOfWeek(int year, int weekOfYear) {
        Calendar sunday = getCalendar();
        sunday.set(Calendar.YEAR, year);
        sunday.set(Calendar.WEEK_OF_YEAR, weekOfYear + 1);
        sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sunday.getTime();
    }

    public static Integer getYear(Date date) {
        Calendar c = getCalendar();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    private static Calendar getCalendar() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        return c;
    }
}

Related

  1. getWeeksBetweenDate(Date begin, Date end)
  2. getWeekStart(Date date)
  3. getWeekStart(Date date)
  4. getWeekStartDateBeforeCurrent(int weekNum, Date current)
  5. getWeekth(String sDate)
  6. incWeek(java.util.Date date, int amount, Locale locale)
  7. isMatchWeek(Date date, int week)
  8. isOnWeekend(Date date)
  9. isSameWeekDates(Date date1, Date date2)