Java Week Calculate alignWeek(Calendar timestamp)

Here you can find the source of alignWeek(Calendar timestamp)

Description

Aligns the time fields to the start of the week.

License

Apache License

Parameter

Parameter Description
timestamp The calendar to align.

Return

The parameter.

Declaration

public static Calendar alignWeek(Calendar timestamp) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//from ww  w.  j a  va  2 s.  c om
     * Aligns the time fields to the start of the week.
     * @param timestamp The calendar to align.
     * @return The parameter.
     */
    public static Calendar alignWeek(Calendar timestamp) {
        timestamp = alignDay(timestamp);
        int dayOfWeek = timestamp.get(Calendar.DAY_OF_WEEK);
        int offset = 1 - dayOfWeek;
        if (offset == 0) {
            return timestamp;
        }
        return addDays(offset, timestamp);
    }

    /**
     * Aligns the time fields to the start of the day.
     * @param timestamp The calendar to align.
     * @return The parameter.
     */
    public static Calendar alignDay(Calendar timestamp) {
        timestamp.set(Calendar.HOUR_OF_DAY, 0);
        timestamp.set(Calendar.MINUTE, 0);
        timestamp.set(Calendar.SECOND, 0);
        timestamp.set(Calendar.MILLISECOND, 0);
        return timestamp;
    }

    /**
     * Adds or subtracts the corresponding time field, does not
     * perform any alignment.
     * @param count The quantity to change, can be negative.
     * @param timestamp The calendar to modify.
     * @return The timestamp parameter.
     */
    public static Calendar addDays(int count, Calendar timestamp) {
        timestamp.add(Calendar.DATE, count);
        return timestamp;
    }
}

Related

  1. diffWeeks(Date start, Date end)
  2. getBounceDatesOfWeek(Date date)
  3. getCurrentWeekNum(Date startDate, Date endDate)
  4. getCurrWeek()