Java Calendar Week setTimeAsFirstDayOfWeek(Calendar calendar)

Here you can find the source of setTimeAsFirstDayOfWeek(Calendar calendar)

Description

set Time As First Day Of Week

License

Open Source License

Declaration

public static void setTimeAsFirstDayOfWeek(Calendar calendar) 

Method Source Code


//package com.java2s;
/*//from ww w  .  j  a v  a2s.  co  m
 * Copyright 2009 Yodlee, Inc.  All Rights Reserved.  Your use of this code 
 * requires a license from Yodlee.  Any such license to this code is 
 * restricted to evaluation/illustrative purposes only. It is not intended 
 * for use in a production environment, and Yodlee disclaims all warranties 
 * and/or support obligations concerning this code, regardless of the terms 
 * of any other agreements between Yodlee and you."
 */

import java.util.Calendar;

public class Main {
    public static void setTimeAsFirstDayOfWeek(Calendar calendar) {

        int monthBeforeSet = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        int monthAfterSet = calendar.get(Calendar.MONTH);

        // if year changes setMinimalDaysInFirstWeek for New Year
        if (monthBeforeSet == Calendar.JANUARY && monthAfterSet == Calendar.DECEMBER) {
            calendar.setMinimalDaysInFirstWeek(calculateMinimalDaysInFirstWeek(calendar));
        }
        normalise(calendar);
    }

    public static int calculateMinimalDaysInFirstWeek(Calendar calendar) {

        Calendar temp = (Calendar) calendar.clone();
        temp.set(Calendar.DAY_OF_YEAR, 1);
        int firstDayOfJan = temp.get(Calendar.DAY_OF_WEEK);
        return 8 - firstDayOfJan;
    }

    public static void normalise(Calendar calendar) {

        Calendar tempCalendar = (Calendar) calendar.clone();
        calendar.clear();
        calendar.set(Calendar.YEAR, tempCalendar.get(Calendar.YEAR));
        calendar.set(Calendar.DAY_OF_YEAR, tempCalendar.get(Calendar.DAY_OF_YEAR));
    }
}

Related

  1. isWeekend(Calendar cal)
  2. isWeekend(GregorianCalendar date)
  3. lastDayOfWeek(Calendar cal)
  4. lastDayOfWeek(Calendar calendar)
  5. setStartOfWeek(Calendar date)
  6. truncateToWeek(GregorianCalendar date)
  7. weekDay(Calendar date)
  8. weekFloor(Calendar calendar)
  9. weekFromBinStart(Calendar date)