get Week End - Java java.util

Java examples for java.util:Week

Description

get Week End

Demo Code


//package com.java2s;

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

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar cal = Calendar.getInstance();
        System.out.println(getWeekEnd(cal));
    }/*from  w w w.j av a 2  s.  com*/

    public static Calendar getWeekEnd(Calendar cal) {
        Calendar c = getWeekStart(cal);
        c.add(Calendar.DAY_OF_WEEK, 6);
        return c;
    }

    public static Calendar getWeekEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getWeekEnd(cal);
    }

    public static Calendar getWeekEnd(Integer weekOfYear) {
        return getWeekEnd(weekOfYear.intValue());
    }

    public static Calendar getWeekEnd(Integer weekOfYear, Integer year) {
        return getWeekEnd(weekOfYear.intValue(), year.intValue());
    }

    public static Calendar getWeekEnd(int weekOfYear) {
        return getWeekEnd(weekOfYear,
                Calendar.getInstance().get(Calendar.YEAR));
    }

    public static Calendar getWeekEnd(int weekOfYear, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimalDaysInFirstWeek());
        cal.set(Calendar.YEAR, year);
        cal = getWeekEnd(cal);
        cal.add(Calendar.WEEK_OF_YEAR, weekOfYear - 1);
        return cal;
    }

    public static Calendar getWeekStart(Calendar cal) {
        Calendar c = (Calendar) cal.clone();
        c.add(Calendar.DAY_OF_WEEK,
                c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK));
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c;
    }

    public static Calendar getWeekStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getWeekStart(cal);
    }

    public static Calendar getWeekStart(Integer weekOfYear) {
        return getWeekStart(weekOfYear.intValue());
    }

    public static Calendar getWeekStart(Integer weekOfYear, Integer year) {
        return getWeekStart(weekOfYear.intValue(), year.intValue());
    }

    public static Calendar getWeekStart(int weekOfYear) {
        return getWeekStart(weekOfYear,
                Calendar.getInstance().get(Calendar.YEAR));
    }

    public static Calendar getWeekStart(int weekOfYear, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimalDaysInFirstWeek());
        cal.set(Calendar.YEAR, year);
        cal = getWeekStart(cal);
        cal.add(Calendar.WEEK_OF_YEAR, weekOfYear - 1);
        return cal;
    }
}

Related Tutorials