Java Utililty Methods Year Week

List of utility methods to do Year Week

Description

The list of methods to do Year Week are organized into topic(s).

Method

StringgetEndDayByWeekOfYear(int year, int weekNo)
get the end day of given week no of a year.,the first day of week is Sunday
Calendar cal = getCalendarFormYear(year);
cal.set(Calendar.WEEK_OF_YEAR, weekNo);
cal.add(Calendar.DAY_OF_WEEK, 6);
int monthInt = cal.get(Calendar.MONTH) + 1;
String monthString = monthInt < 10 ? "0" + monthInt : monthInt + "";
int dayOfMontyInt = cal.get(Calendar.DAY_OF_MONTH);
String dayOfMonth = dayOfMontyInt < 10 ? "0" + dayOfMontyInt : dayOfMontyInt + "";
return cal.get(Calendar.YEAR) + "-" + monthString + "-" + dayOfMonth;
...
DategetLastDayOfWeek(int year, int week)
get Last Day Of Week
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
Calendar c = (Calendar) cal.clone();
c.add(Calendar.DATE, week * 7);
return getEndTimeOfWeek(c.getTime());
StringgetLastWeekOfYear()
get Last Week Of Year
int num = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
if (num > 1) {
    num--;
return (num + "").length() == 1 ? "0" + num : "" + num;
intgetMaxWeekNumOfYear(int year)
get Max Week Num Of Year
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
DategetNextWeekMonday(int year, int weekOfYear)
get Next Week Monday
Date date = getMondayOfWeek(year, weekOfYear);
return getNextWeekDate(date);
intgetNumberOfWeeksInYear(int year)
Computes the number of weeks in a given year See: http://stackoverflow.com/questions/18438332/how-to-get-total-number-of-week-in-the-current-year
GregorianCalendar cal = new GregorianCalendar(year, 12, 31);
cal.setFirstDayOfWeek(Calendar.MONDAY); 
cal.setMinimalDaysInFirstWeek(4); 
return cal.getMaximum(Calendar.WEEK_OF_YEAR);
intgetSeasonStartWeekOffset(int year)
get Season Start Week Offset
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
cal.set(Calendar.DAY_OF_MONTH, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY:
    return 34;
case Calendar.MONDAY:
...
StringgetStartDayOfWeekNo(int year, int weekNo)
get start date of given week no of a year
Calendar cal = getCalendarFormYear(year);
cal.set(Calendar.WEEK_OF_YEAR, weekNo);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
intgetTotalWeekOfYear(int year)
get Total Week Of Year
Calendar c = Calendar.getInstance();
return c.getActualMaximum(Calendar.WEEK_OF_YEAR);
StringgetYmd(int year, int week, int days)
get Ymd
Calendar calendar = Calendar.getInstance();
if (year > 0) {
    calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.DAY_OF_WEEK, days);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
...