get Week Start - Java java.util

Java examples for java.util:Week

Description

get Week Start

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(getWeekStart(cal));
    }/*  www  . j  a  v a 2  s  . c o  m*/

    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