Java SQL Date getWeekNumber()

Here you can find the source of getWeekNumber()

Description

Gets the week number.

License

Open Source License

Return

the week number

Declaration

public static int getWeekNumber() 

Method Source Code

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

import java.sql.Date;

import java.util.Calendar;

public class Main {
    /**/*w ww.  j  av  a 2s  .  c o  m*/
     * Gets the week number.
     * 
     * @return the week number
     */
    public static int getWeekNumber() {
        return (getLastSundayDate().get(Calendar.DAY_OF_YEAR) - getFirstSundayDateInYear(
                2009).get(Calendar.DAY_OF_YEAR)) / 7 + 1;
    }

    /**
     * Gets the last sunday date.
     * 
     * @return the last sunday date
     */
    public static Calendar getLastSundayDate() {
        Calendar cal = Calendar.getInstance();
        int daydiff = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
        cal.add(Calendar.DATE, daydiff * -1);
        return cal;
    }

    /**
     * Gets the last sunday date.
     * 
     * @param d the d
     * 
     * @return the last sunday date
     */
    public static Calendar getLastSundayDate(Date d) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int daydiff = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
        cal.add(Calendar.DATE, daydiff * -1);
        return cal;
    }

    /**
     * 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. getNow()
  2. getNow()
  3. getNowHour()
  4. getProtobufClass(Object value, Class protobufClass)
  5. getTodayAndTomorrow()
  6. getWeekOfMonth(String year, String month, String day)
  7. getYearLater()
  8. hexToBin(String sHexString)
  9. isLastDayOfMonth(String theDataStr)