get End Of Current Week - Java java.util

Java examples for java.util:Week

Description

get End Of Current Week

Demo Code

/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  w w.  j  a v  a 2  s .com
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/
//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getEndOfCurrentWeek());
    }

    private static int startDay = Calendar.MONDAY;

    public static Calendar getEndOfCurrentWeek() {
        Calendar cal = getCalendar();
        return snapEndOfWeek(cal);
    }

    public static Calendar getCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(startDay);
        cal.getTime();
        return cal;
    }

    public static Calendar snapEndOfWeek(Calendar cal) {

        cal.set(Calendar.DAY_OF_WEEK, getLastDayOfWeek(cal));

        snapEndOfDay(cal);
        return cal;
    }

    public static int getLastDayOfWeek(Calendar cal) {
        int last = cal.getFirstDayOfWeek() - 1;

        if (last == 0) {
            last = Calendar.SATURDAY;
        }

        return last;
    }

    public static Calendar snapEndOfDay(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY,
                cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND,
                cal.getActualMaximum(Calendar.MILLISECOND));
        cal.getTime();
        return cal;
    }
}

Related Tutorials