Java Week Calculate startOfWeek(Date datum)

Here you can find the source of startOfWeek(Date datum)

Description

start Of Week

License

Open Source License

Declaration

public static Date startOfWeek(Date datum) 

Method Source Code

//package com.java2s;
/*//w  ww  .  j  ava 2  s.  c om
 * #%L
 * Webstar Newsletter
 * %%
 * Copyright (C) 2013 Webstar Csoport Kft.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.*;

public class Main {

    public static Date startOfWeek(Date datum) {
        if (datum == null) {
            return null;
        }
        Calendar c = new GregorianCalendar();
        c.setTime(datum);
        while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            c.setTime(addDays(c.getTime(), -1));
        }
        return startOfDay(c.getTime());
    }

    public static Date addDays(Date eredeti, int mennyit) {
        return add(eredeti, Calendar.DAY_OF_YEAR, mennyit);
    }

    public static Date startOfDay(Date datum) {
        if (datum == null) {
            return null;
        }
        Calendar cal = new GregorianCalendar();
        cal.setTime(datum);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    public static Date add(Date eredeti, int mihez, int mennyit) {
        if (eredeti == null) {
            return null;
        }
        Calendar cal = new GregorianCalendar();
        cal.setTime(eredeti);
        cal.add(mihez, mennyit);

        return cal.getTime();
    }
}

Related

  1. getWeeksInYear(int year)
  2. isSameWeekDates(Date date1, Date date2)
  3. nextWeek(Date date, int week)
  4. nextWeek(long date)
  5. plusHour(Date date, int plusWeek)
  6. weekCal(int weekNumber)
  7. weekCount(Date start, Date end)
  8. weekNumber()
  9. weeksInYear(int year)