Java Code Examples for java.util.Calendar#getFirstDayOfWeek()

The following examples show how to use java.util.Calendar#getFirstDayOfWeek() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonUtils.java    From SmartChart with Apache License 2.0 6 votes vote down vote up
public static ArrayList<Calendar> getCurrentMonthCalendar(Calendar c, int offset) {
    ArrayList<Calendar> calendars = new ArrayList<>();
    Calendar currentMonth = (Calendar) c.clone();
    currentMonth.add(Calendar.MONTH, offset);


    Calendar cal = (Calendar) c.clone();
    cal.add(Calendar.MONTH, offset);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int off = cal.getFirstDayOfWeek() - firstDayOfWeek;
    if (off > 0) {
        off -= 7;
    }
    cal.add(Calendar.DATE, off);

    while ((cal.get(MONTH) < currentMonth.get(Calendar.MONTH) + 1 || cal.get(YEAR) < currentMonth.get(Calendar.YEAR)) //
            && cal.get(YEAR) <= currentMonth.get(Calendar.YEAR)) {
        for (int i = 0; i < 7; i++) {
            calendars.add((Calendar) cal.clone());
            cal.add(Calendar.DATE, 1);
            Log.i("aaa", cal.get(Calendar.MONTH) + "--" + cal.get(Calendar.DAY_OF_MONTH));
        }
    }
    return calendars;
}
 
Example 2
Source File: DayOfWeekStatisticDaoImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
public StatisticResult generateStatisticResult(final Locale locale, final ICourse course, final long courseRepositoryEntryKey) {
    final DBQuery dbQuery = DBFactory.getInstance().createQuery(
            "select businessPath,day,value from org.olat.data.course.statistic.dayofweek.DayOfWeekStat sv " + "where sv.resId=:resId");
    dbQuery.setLong("resId", courseRepositoryEntryKey);

    final StatisticResult result = new StatisticResult(course, dbQuery.list());

    // now sort by user's preferred firstDayOfWeek
    final Calendar c = Calendar.getInstance(locale);
    final int firstDayOfWeek = c.getFirstDayOfWeek();

    final List<String> columnHeaders = new ArrayList<String>(7);
    for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
        int mod = i % 7;
        if (mod == 0) {
            // jdk calendar calculations don't start at 0 - they start at 1
            mod = 7;
        }
        columnHeaders.add(String.valueOf(mod));
    }

    result.setColumnHeaders(columnHeaders);
    return result;
}
 
Example 3
Source File: DayOfWeekStatisticDaoImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
@Override
public StatisticResult generateStatisticResult(final Locale locale, final ICourse course, final long courseRepositoryEntryKey) {
    final DBQuery dbQuery = DBFactory.getInstance().createQuery(
            "select businessPath,day,value from org.olat.data.course.statistic.dayofweek.DayOfWeekStat sv " + "where sv.resId=:resId");
    dbQuery.setLong("resId", courseRepositoryEntryKey);

    final StatisticResult result = new StatisticResult(course, dbQuery.list());

    // now sort by user's preferred firstDayOfWeek
    final Calendar c = Calendar.getInstance(locale);
    final int firstDayOfWeek = c.getFirstDayOfWeek();

    final List<String> columnHeaders = new ArrayList<String>(7);
    for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
        int mod = i % 7;
        if (mod == 0) {
            // jdk calendar calculations don't start at 0 - they start at 1
            mod = 7;
        }
        columnHeaders.add(String.valueOf(mod));
    }

    result.setColumnHeaders(columnHeaders);
    return result;
}
 
Example 4
Source File: Bug4527203.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    Calendar huCalendar = Calendar.getInstance(new Locale("hu","HU"));
    int hufirstDayOfWeek = huCalendar.getFirstDayOfWeek();
    if (hufirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

    Calendar ukCalendar = Calendar.getInstance(new Locale("uk","UA"));
    int ukfirstDayOfWeek = ukCalendar.getFirstDayOfWeek();
    if (ukfirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

}
 
Example 5
Source File: Bug4527203.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    Calendar huCalendar = Calendar.getInstance(new Locale("hu","HU"));
    int hufirstDayOfWeek = huCalendar.getFirstDayOfWeek();
    if (hufirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

    Calendar ukCalendar = Calendar.getInstance(new Locale("uk","UA"));
    int ukfirstDayOfWeek = ukCalendar.getFirstDayOfWeek();
    if (ukfirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

}
 
Example 6
Source File: CalendarComposite.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private void drawTitleDays(GC gc) {
	Calendar temp = Calendar.getInstance(mSettings.getLocale());
	// fetch the first day of the week, and draw starting on that day
	int fdow = temp.getFirstDayOfWeek();

	Rectangle bounds = super.getBounds();
	int xStart = mSettings.getDatesLeftMargin() + 5;
	int yStart = bounds.y + mSettings.getHeaderTopMargin() + mSettings.getHeaderHeight() + 1;

	int spacer = 0;
	int letterHeight = 0;

	for (int i = 0; i < 7; i++) {
		Point strWidth = gc.stringExtent(mDayTitles[fdow]);

		int x = xStart + mSettings.getOneDateBoxSize() + spacer - strWidth.x;
		// don't add the string width, as our string width later when
		// drawing days will differ
		mDayXs[i] = xStart + mSettings.getOneDateBoxSize() + spacer;

		gc.drawString(mDayTitles[fdow], x, yStart, true);

		letterHeight = strWidth.y;
		spacer += mSettings.getOneDateBoxSize() + mSettings.getBoxSpacer();

		fdow++;
		if (fdow > 7) {
			fdow = 1;
		}
	}

	int lineStart = yStart + 1 + letterHeight;
	gc.setForeground(mColorManager.getLineColor());
	gc.drawLine(mSettings.getDatesLeftMargin() + 1, lineStart, bounds.width - mSettings.getDatesRightMargin() - 3, lineStart);

	mDatesTopY = lineStart + 3;
}
 
Example 7
Source File: LoadNotesListTask.java    From nextcloud-notes with GNU General Public License v3.0 5 votes vote down vote up
Timeslotter() {
    Calendar now = Calendar.getInstance();
    int month = now.get(Calendar.MONTH);
    int day = now.get(Calendar.DAY_OF_MONTH);
    int offsetWeekStart = (now.get(Calendar.DAY_OF_WEEK) - now.getFirstDayOfWeek() + 7) % 7;
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_today), month, day));
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_yesterday), month, day - 1));
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_this_week), month, day - offsetWeekStart));
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_last_week), month, day - offsetWeekStart - 7));
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_this_month), month, 1));
    timeslots.add(new Timeslot(context.getResources().getString(R.string.listview_updated_last_month), month - 1, 1));
    lastYear = Calendar.getInstance();
    lastYear.set(now.get(Calendar.YEAR) - 1, 0, 1, 0, 0, 0);
}
 
Example 8
Source File: CalendarUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static int getLastDayOfWeek(Calendar cal) {
    int dow = cal.getFirstDayOfWeek() - 1;

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

    return dow;
}
 
Example 9
Source File: LocaleTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4143951
 * Russian first day of week should be Monday. Confirmed.
 */
public void Test4143951() {
    Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
    if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
        errln("Fail: First day of week in Russia should be Monday");
    }
}
 
Example 10
Source File: CalendarGridPanel.java    From microba with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int getFocusedIndex() {
	Calendar bc = getCalendar(baseDate);
	Calendar fc = getCalendar(focusDate);
	bc.set(Calendar.DAY_OF_MONTH, 1);
	int skipBefore = bc.get(Calendar.DAY_OF_WEEK) - bc.getFirstDayOfWeek();
	if (skipBefore < 0)
		skipBefore = 7 + skipBefore;
	int selDay = fc.get(Calendar.DAY_OF_MONTH);
	int maxDay = bc.getActualMaximum(Calendar.DAY_OF_MONTH);
	if (selDay > maxDay)
		selDay = maxDay;
	return skipBefore + selDay - 1;
}
 
Example 11
Source File: LocaleTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4143951
 * Russian first day of week should be Monday. Confirmed.
 */
public void Test4143951() {
    Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
    if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
        errln("Fail: First day of week in Russia should be Monday");
    }
}
 
Example 12
Source File: Bug4527203.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    Calendar huCalendar = Calendar.getInstance(new Locale("hu","HU"));
    int hufirstDayOfWeek = huCalendar.getFirstDayOfWeek();
    if (hufirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

    Calendar ukCalendar = Calendar.getInstance(new Locale("uk","UA"));
    int ukfirstDayOfWeek = ukCalendar.getFirstDayOfWeek();
    if (ukfirstDayOfWeek != Calendar.MONDAY) {
        throw new RuntimeException();
    }

}
 
Example 13
Source File: WeekFramerImpl.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Date adjustRight(Date baseDate) {
  Calendar c = myCalendarFactory.newCalendar();
  do {
    baseDate = myDayFramer.adjustRight(baseDate);
    c.setTime(baseDate);
  } while (c.get(Calendar.DAY_OF_WEEK) != c.getFirstDayOfWeek());
  return c.getTime();
}
 
Example 14
Source File: LocaleTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4143951
 * Russian first day of week should be Monday. Confirmed.
 */
public void Test4143951() {
    Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
    if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
        errln("Fail: First day of week in Russia should be Monday");
    }
}
 
Example 15
Source File: LocaleTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 4143951
 * Russian first day of week should be Monday. Confirmed.
 */
public void Test4143951() {
    Calendar cal = Calendar.getInstance(new Locale("ru", "", ""));
    if (cal.getFirstDayOfWeek() != Calendar.MONDAY) {
        errln("Fail: First day of week in Russia should be Monday");
    }
}
 
Example 16
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 4080631: Calendar.hashCode is amazingly bad
 */
public void Test4080631() {
    Calendar cal = Calendar.getInstance();
    int h1 = cal.hashCode();
    cal.add(SECOND, +1);
    int h2 = cal.hashCode();
    Calendar cal2 = (Calendar) cal.clone();
    cal.add(MILLISECOND, +1);
    int h3 = cal.hashCode();
    logln("hash code: h1=" + h1 + ", h2=" + h2 + ", h3=" + h3);
    if (h1 == h2 || h1 == h3 || h2 == h3) {
        errln("hash code is poor: hashCode=" + h1);
    }
    h2 = cal2.hashCode();
    cal.add(MILLISECOND, -1);
    int h4 = cal.hashCode();
    logln("hash code: h2=" + h2 + ", h4=" + h4);
    if (cal.equals(cal2) && h2 != h4) {
        errln("broken hash code: h2=" + h2 + ", h4=" + h4);
    }
    int x = cal.getFirstDayOfWeek() + 3;
    if (x > SATURDAY) {
        x -= 7;
    }
    cal.setFirstDayOfWeek(x);
    int h5 = cal.hashCode();
    logln("hash code: h4=" + h4 + ", h5=" + h5);
    if (h4 == h5) {
        errln("has code is poor with first day of week param: hashCode=" + h4);
    }
}
 
Example 17
Source File: CalendarBuilder.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 18
Source File: CalendarBuilder.java    From j2objc with Apache License 2.0 4 votes vote down vote up
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 19
Source File: CalendarBuilder.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
Calendar establish(Calendar cal) {
    boolean weekDate = isSet(WEEK_YEAR)
                        && field[WEEK_YEAR] > field[YEAR];
    if (weekDate && !cal.isWeekDateSupported()) {
        // Use YEAR instead
        if (!isSet(YEAR)) {
            set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
        }
        weekDate = false;
    }

    cal.clear();
    // Set the fields from the min stamp to the max stamp so that
    // the field resolution works in the Calendar.
    for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
        for (int index = 0; index <= maxFieldIndex; index++) {
            if (field[index] == stamp) {
                cal.set(index, field[MAX_FIELD + index]);
                break;
            }
        }
    }

    if (weekDate) {
        int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
        int dayOfWeek = isSet(DAY_OF_WEEK) ?
                            field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
        if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
            if (dayOfWeek >= 8) {
                dayOfWeek--;
                weekOfYear += dayOfWeek / 7;
                dayOfWeek = (dayOfWeek % 7) + 1;
            } else {
                while (dayOfWeek <= 0) {
                    dayOfWeek += 7;
                    weekOfYear--;
                }
            }
            dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
        }
        cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
    }
    return cal;
}
 
Example 20
Source File: DateRangeBuilder.java    From jasperreports with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * <p>
 *     Configuration method which set start of a week
 *     which will be used during DateRange.getStart and DateRange.getEnd calculations
 *     from passed locale instance
 * </p>
 * <p>
 *     NOTE: This method and {@link #set(Integer)} will overwrite each other
 *     so configuration which was set later will be used.
 * </p>
 * <p>
 *     NOTE: this method only will take effect if expression is in fact date range expression like "WEEK".
 * </p>
 * @param locale locale to calculate DateRange.getStart and DateRange.getEnd for expressions like "WEEK"
 * @return this instance of DateRangeBuilder
 */
public DateRangeBuilder set(Locale locale) {
	Calendar cal = Calendar.getInstance(locale);
	this.weekStartDay = cal.getFirstDayOfWeek();

	return this;
}