Java Day of Week weekOfYear(Date date)

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

Description

week Of Year

License

Open Source License

Declaration

public static String weekOfYear(Date date) 

Method Source Code

//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String weekOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//from  w w w .  j av  a  2  s  .c  o m

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);

        int a, b, c, s, e, f, g, d, n;
        int week;

        if (month <= 2) {
            a = year - 1;
            b = a / 4 - a / 100 + a / 400;
            c = (a - 1) / 4 - (a - 1) / 100 + (a - 1) / 400;
            s = b - c;
            e = 0;
            f = day - 1 + 31 * (month - 1);
        } else {
            a = year;
            b = a / 4 - a / 100 + a / 400;
            c = (a - 1) / 4 - (a - 1) / 100 + (a - 1) / 400;
            s = b - c;
            e = s + 1;
            f = day + (153 * (month - 3) + 2) / 5 + 58 + s;
        }

        g = (a + b) % 7;
        d = (f + g - e) % 7;
        n = f + 3 - d;

        if (n < 0) {
            week = 53 - (g - s) / 5;
            year = year - 1;
        } else if (n > 364 + s) {
            week = 1;
            year = year + 1;
        } else {
            week = n / 7 + 1;
        }

        if (week < 10) {
            return "0" + week + " " + year;
        } else {
            return week + " " + year;
        }
    }
}

Related

  1. truncDateToIsoWeek(Date d)
  2. weekCount(Date start, Date end)
  3. weekDay(final Date date)
  4. weekDaysBetween(final Date a, final Date b)
  5. WeekOfTheYear(Date dt)
  6. weeksBetween(Date date1, Date date2)
  7. weeksBetween(Date early, Date late)
  8. weekStart(Date date)
  9. weekStart(Date date)