Example usage for java.util Calendar getFirstDayOfWeek

List of usage examples for java.util Calendar getFirstDayOfWeek

Introduction

In this page you can find the example usage for java.util Calendar getFirstDayOfWeek.

Prototype

public int getFirstDayOfWeek() 

Source Link

Document

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

Usage

From source file:org.getobjects.foundation.UDate.java

/**
 * This returns a Calendar object which represents the beginning of the week
 * in the Calendar's locale and timezone.
 * <p>/*from   w w  w. java2 s .  c o m*/
 * Eg in a German locale this could be
 *   'Mon, 2007-06-04 00:00:00'
 * if the Calendar being passed in is
 *   'Wed, 2007-06-06 12:34:37'.
 * In US locales the week would start on Sunday etc.
 *
 * @param _calendar - a Calendar in the week
 * @return a Calendar representing the start of the week
 */
public static Calendar beginningOfWeekAsCalendar(Calendar _calendar) {
    if (_calendar == null) {
        /* not recommended as the timezone is most likely incorrect */
        _calendar = Calendar.getInstance();
    } else
        _calendar = (Calendar) _calendar.clone();

    _calendar.set(Calendar.DAY_OF_WEEK, _calendar.getFirstDayOfWeek());
    _calendar.set(Calendar.HOUR_OF_DAY, 0);
    _calendar.set(Calendar.MINUTE, 0);
    _calendar.set(Calendar.SECOND, 0);
    _calendar.set(Calendar.MILLISECOND, 0);
    return _calendar;
}

From source file:org.nuclos.common2.DateUtils.java

private static Date dateFromQuant(Calendar cal, int quant, int quantizer) {
    switch (quantizer) {
    case Calendar.WEEK_OF_YEAR:
        final Date old = cal.getTime();

        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.WEEK_OF_YEAR, quant);
        // cal.set(Calendar.DAY_OF_WEEK, WEEK_START_DAY);

        if (old.before(cal.getTime())) {
            montoneAdd(cal, Calendar.YEAR, -1);
            montoneAdd(cal, Calendar.WEEK_OF_YEAR, 1);
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            assert cal.getTime().before(old);
        }/*  w ww.ja  v  a 2  s  .  c o m*/
        break;
    case Calendar.DAY_OF_WEEK:
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.DAY_OF_WEEK, quant);
        break;
    case Calendar.DAY_OF_MONTH:
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.DAY_OF_MONTH, quant);
        break;
    case Calendar.MONTH:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.MONTH, quant);
        break;
    case QUARTER:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.MONTH, quant * 3);
        break;
    case Calendar.YEAR:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MONTH, 0);
        break;
    default:
        throw new IllegalArgumentException();
    }
    final Date result = cal.getTime();
    return result;
}

From source file:com.cognizant.trumobi.em.Email.java

public static String getWeekDays() {
    Calendar cal = Calendar.getInstance();
    String string = Email.getPresentDay();
    // Log.e("getPresentDay", ""+string);
    Date date = null;/*from ww  w. ja  va2 s.c o  m*/
    try {
        date = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH).parse(string);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.setTime(date);
    SimpleDateFormat format = new SimpleDateFormat("MMM dd", Locale.US);
    Calendar firstCal = (Calendar) cal.clone();

    firstCal.add(Calendar.DAY_OF_WEEK, firstCal.getFirstDayOfWeek() - firstCal.get(Calendar.DAY_OF_WEEK));
    // SharedPreferences prefs = PreferenceManager
    // .getDefaultSharedPreferences(appContext);
    SharedPreferences prefs = new SharedPreferences(appContext);
    String WeekStartsOn = prefs.getString(appContext.getString(R.string.key_list_preference), "");
    if (WeekStartsOn.equalsIgnoreCase("2")) {
        firstCal.setFirstDayOfWeek(Calendar.SATURDAY);
    } else if (WeekStartsOn.equalsIgnoreCase("3")) {
        firstCal.setFirstDayOfWeek(Calendar.SUNDAY);
    } else if (WeekStartsOn.equalsIgnoreCase("4")) {
        firstCal.setFirstDayOfWeek(Calendar.MONDAY);
    }
    int firstDayOfWeek = firstCal.getFirstDayOfWeek();
    int dayOfWeek = firstCal.get(Calendar.DAY_OF_WEEK);
    if (firstDayOfWeek == 7) {
        firstCal.add(Calendar.DAY_OF_WEEK, -dayOfWeek);
    } else
        firstCal.add(Calendar.DAY_OF_WEEK, firstCal.getFirstDayOfWeek() - firstCal.get(Calendar.DAY_OF_WEEK));
    String[] days = new String[2];
    days[0] = format.format(firstCal.getTime());
    Calendar lastCal = (Calendar) firstCal.clone();
    lastCal.add(Calendar.DAY_OF_YEAR, 6);
    days[1] = format.format(lastCal.getTime());
    String[] weekDays = Arrays.toString(days).split(",");
    // String weekVal2=weekDays[1].substring(weekDays[1].length()-1);
    String weekVal2 = weekDays[1].replace("]", "");
    String weekVal = weekDays[0].substring(1) + " - " + weekVal2;
    return weekVal;
}

From source file:org.sharetask.service.StatisticsServiceImpl.java

private StatisticsDataDTO getStatisticsPerLastWeek() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    Date date = DateUtils.truncate(calendar.getTime(), Calendar.DATE);
    return new StatisticsDataDTO.Builder().usersCount(userInformationRepository.getCountCreatedAfter(date))
            .workspacesCount(workspaceRepository.getCountCreatedAfter(date))
            .tasksCount(taskRepository.getCountCreatedAfter(date)).build();
}

From source file:com.stratelia.webactiv.util.DateUtil.java

/**
 * Gets the first day of weeks of the calendar with 1 meaning for sunday, 2 meaning for monday,
 * and so on. The first day of weeks depends on the locale; the first day of weeks is monday for
 * french whereas it is for sunday for US.
 * @param language the locale to take into account (fr for the french locale (fr_FR) for
 * example)./*from   w  w  w. j a  v a  2 s.c  o  m*/
 * @return the first day of week.
 */
public static int getFirstDayOfWeek(String language) {
    Calendar calendar;
    Locale locale = getLocale(language);
    if (locale == null) {
        calendar = Calendar.getInstance();
    } else {
        calendar = Calendar.getInstance(locale);
    }
    return calendar.getFirstDayOfWeek();
}

From source file:org.apache.lens.cube.metadata.TimePartition.java

private Date truncate(Date date, UpdatePeriod updatePeriod) {
    if (updatePeriod.equals(UpdatePeriod.WEEKLY)) {
        Date truncDate = DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
        Calendar cal = Calendar.getInstance();
        cal.setTime(truncDate);//from   w ww . j  a  v  a2  s  .c o m
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        return cal.getTime();
    } else if (updatePeriod.equals(UpdatePeriod.QUARTERLY)) {
        Date dt = DateUtils.truncate(date, updatePeriod.calendarField());
        dt.setMonth(dt.getMonth() - dt.getMonth() % 3);
        return dt;
    } else {
        return DateUtils.truncate(date, updatePeriod.calendarField());
    }
}

From source file:org.apache.ranger.audit.utils.RollingTimeUtil.java

private Date computeTopOfWeekDate(int weeks) {
    Date ret = null;//from  w w  w .j  a  v a2  s  . co  m

    Calendar calendarWeek = Calendar.getInstance();
    calendarWeek.set(Calendar.DAY_OF_WEEK, calendarWeek.getFirstDayOfWeek());
    calendarWeek.add(Calendar.WEEK_OF_YEAR, weeks);
    calendarWeek.set(Calendar.HOUR_OF_DAY, 0);
    calendarWeek.clear(Calendar.MINUTE);
    calendarWeek.clear(Calendar.SECOND);
    calendarWeek.clear(Calendar.MILLISECOND);

    ret = calendarWeek.getTime();

    return ret;
}

From source file:org.shredzone.cilla.site.renderer.CalendarRenderStrategyImpl.java

/**
 * Writes the calendar table itself./*ww  w .ja  v  a 2s  . co  m*/
 */
protected void writeCalendar() throws IOException {
    Calendar cal = generator.getDisplayCalendar();

    int firstDay = cal.getFirstDayOfWeek() - 1;
    int firstWeekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
    int numberOfDays = cal.getActualMaximum(Calendar.DATE);
    int skipDays = (firstWeekday + 7 - firstDay) % 7;

    out.append("<tr>");

    int column = 0;
    for (int ix = -skipDays; ix < numberOfDays; ix++) {
        if (ix >= 0) {
            String url = generator.getDayUrl(ix + 1);
            if (url != null) {
                writeLinkedDay(ix + 1, url);
            } else {
                writeDay(ix + 1);
            }
        } else {
            writeEmptyDay();
        }

        column++;
        if (column == 7) {
            column = 0;
            out.append("</tr>");
            out.append("<tr>");
        }

    }

    for (; column < 7; column++) {
        writeEmptyDay();
    }

    out.append("</tr>");
}

From source file:org.shredzone.cilla.site.renderer.CalendarRenderStrategyImpl.java

/**
 * Writes the headline of the calendar table. It consists of the weekdays.
 */// w w  w. ja v  a  2  s  .c  o m
protected void writeHeadline() throws IOException {
    Calendar calendar = Calendar.getInstance(generator.getLocale());
    int firstDay = calendar.getFirstDayOfWeek();

    DateFormatSymbols symbols = DateFormatSymbols.getInstance(generator.getLocale());
    String[] weekdays = symbols.getShortWeekdays();

    out.append("<tr>");
    for (int wd = firstDay; wd < firstDay + 7; wd++) {
        int weekdayIndex = (wd - 1) % 7;

        String wdname = weekdays[weekdayIndex + 1];
        if (wdname.length() > 2) {
            wdname = wdname.substring(0, 2);
        }

        out.append("<th>");
        out.append(HtmlUtils.htmlEscape(wdname));
        out.append("</th>");
    }
    out.append("</tr>");
}

From source file:TripServlet.java

public Date getLastYear() {
    Calendar c = Calendar.getInstance();
    c = (Calendar) c.clone();//from w ww  .  ja  v a 2  s.co  m
    c.add(Calendar.YEAR, -1);// last month
    c.set(Calendar.DAY_OF_YEAR, c.getFirstDayOfWeek());
    Date date = c.getTime();
    return date;
}