Java Code Examples for java.util.Calendar#setFirstDayOfWeek()
The following examples show how to use
java.util.Calendar#setFirstDayOfWeek() .
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: PeriodParser.java From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * returns a date based on a week number * * @param year The year of the date * @param week The week of the date * @param calendar The calendar used to calculate the date * @param firstDayOfWeek The first day of the week * @return The Date of the week */ private Date getDateTimeFromWeek(int year, int week, Calendar calendar, Integer firstDayOfWeek) throws IllegalArgumentException { if (week < 1 || week > 53) { throw new IllegalArgumentException("The week number is outside the year week range."); } calendar.setFirstDayOfWeek(firstDayOfWeek); calendar.setMinimalDaysInFirstWeek(4); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.WEEK_OF_YEAR, week); calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek); calendar.set(Calendar.HOUR_OF_DAY, 10); return calendar.getTime(); }
Example 2
Source File: TimestampFormatter.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public String format(DateTime timestamp) { Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE); c.setFirstDayOfWeek(Calendar.MONDAY); c.setMinimalDaysInFirstWeek(4); c.setTimeInMillis(timestamp.getMillis()); int week = c.get(Calendar.WEEK_OF_YEAR); int weekYear = c.getWeekYear(); int year = c.get(Calendar.YEAR); if (weekYear < year) { // get weeks from last year c.add(Calendar.DAY_OF_MONTH, -7); week = c.getWeeksInWeekYear(); } return zeroPadded(2, String.valueOf(week)); }
Example 3
Source File: DateUtil.java From AndroidBase with Apache License 2.0 | 6 votes |
/** * 获取当前日期当前一周的开始日期 (周日) */ public static String getCurrentWeekFirstDayByDate(String date) { int year = Integer.parseInt(date.substring(0, 4)); int month = Integer.parseInt(date.substring(4, 6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("所在周星期日的日期:"+imptimeBegin); return imptimeBegin; }
Example 4
Source File: TimeUtils.java From ClassSchedule with Apache License 2.0 | 6 votes |
public static Date getThisWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); }
Example 5
Source File: ValueDataUtil.java From hop with Apache License 2.0 | 6 votes |
public static Object yearOfDateISO8601( IValueMeta metaA, Object dataA ) throws HopValueException { if ( dataA == null ) { return null; } Calendar calendar = Calendar.getInstance( Locale.ENGLISH ); calendar.setMinimalDaysInFirstWeek( 4 ); calendar.setFirstDayOfWeek( Calendar.MONDAY ); calendar.setTime( metaA.getDate( dataA ) ); int week = calendar.get( Calendar.WEEK_OF_YEAR ); int month = calendar.get( Calendar.MONTH ); int year = calendar.get( Calendar.YEAR ); // fix up for the year taking into account ISO8601 weeks if ( week >= 52 && month == 0 ) { year--; } if ( week <= 2 && month == 11 ) { year++; } return new Long( year ); }
Example 6
Source File: Dates.java From howsun-javaee-framework with Apache License 2.0 | 6 votes |
public static boolean isThisWeek(Calendar date){ Calendar today = Calendar.getInstance(); date.setFirstDayOfWeek(Calendar.MONDAY); today.setFirstDayOfWeek(Calendar.MONDAY); return date.get(Calendar.YEAR) == today.get(Calendar.YEAR) && date.get(Calendar.WEEK_OF_YEAR) == today.get(Calendar.WEEK_OF_YEAR); }
Example 7
Source File: TimestampFormatter.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public String format(DateTime timestamp) { Calendar c = Calendar.getInstance(timestamp.getZone().toTimeZone(), LOCALE); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setMinimalDaysInFirstWeek(7); c.setTimeInMillis(timestamp.getMillis()); int week = c.get(Calendar.WEEK_OF_YEAR); int weekYear = c.getWeekYear(); int year = c.get(Calendar.YEAR); if (weekYear < year) { // get weeks from last year c.add(Calendar.DAY_OF_MONTH, -7); week = c.getWeeksInWeekYear(); } return zeroPadded(2, String.valueOf(week)); }
Example 8
Source File: WeeklyPeriodGeneratorShould.java From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void setFirstDayOfWeekAndMinimalDaysInFirstWeek(Calendar startCalendar, int weekStartDay) { startCalendar.getTime(); startCalendar.setFirstDayOfWeek(weekStartDay); startCalendar.setMinimalDaysInFirstWeek(4); startCalendar.set(Calendar.DAY_OF_WEEK, weekStartDay); }
Example 9
Source File: DateOperation.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public String getEndOfWeek(String dateString) throws Exception{ Date date = getDateFromString(getStartOfWeek(dateString), "yyyy-MM-dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.add(Calendar.DATE, 6); return getDateStringFromDate(cal.getTime(), "yyyy-MM-dd") + " 23:59:59"; }
Example 10
Source File: DateOperation.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
/** * 获取日期在一年中的周数 * 结果从1开始 * @param dateString yyyy-mm-dd * @return */ public int getWeekNumOfYear( Date date ){ try { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.setFirstDayOfWeek(2);//设置每周的第一天是星期一 return cal.get( Calendar.WEEK_OF_YEAR ); } catch (Exception e) { logger.warn( "系统getWeekNumOfYear发生异常"); logger.error(e); } return -1; }
Example 11
Source File: Times.java From symphonyx with Apache License 2.0 | 5 votes |
/** * Gets the week start time with the specified time. * * @param time the specified time * @return week start time */ public static long getWeekStartTime(final long time) { final Calendar start = Calendar.getInstance(); start.setFirstDayOfWeek(Calendar.MONDAY); start.setTimeInMillis(time); start.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); start.set(Calendar.HOUR, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); start.set(Calendar.MILLISECOND, 0); return start.getTime().getTime(); }
Example 12
Source File: MaterialCalendarView.java From monthweekmaterialcalendarview with Apache License 2.0 | 5 votes |
private int getWeekCountBasedOnMode() { int weekCount = calendarMode.visibleWeeksCount; boolean isInMonthsMode = calendarMode.equals(CalendarMode.MONTHS); if (isInMonthsMode && mDynamicHeightEnabled && adapter != null && pager != null) { Calendar cal = (Calendar) adapter.getItem(pager.getCurrentItem()).getCalendar().clone(); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); //noinspection ResourceType cal.setFirstDayOfWeek(getFirstDayOfWeek()); weekCount = cal.get(Calendar.WEEK_OF_MONTH); } return weekCount + DAY_NAMES_ROW; }
Example 13
Source File: DateOperation.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public String getStartOfWeek(String dateString) throws Exception{ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.setFirstDayOfWeek(Calendar.MONDAY); int tmp = cal.get(Calendar.DAY_OF_WEEK) - 1; if (0 == tmp) { tmp = 7; } cal.add(Calendar.DATE, -(tmp-1)); return getDateStringFromDate(cal.getTime(), "yyyy-MM-dd") + " 00:00:00"; }
Example 14
Source File: DateHelper.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
/** * 取得指定日期所在周的最后一天 */ public static Date getLastDayOfWeek(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday return c.getTime(); }
Example 15
Source File: DateUtils.java From MyTv with Apache License 2.0 | 5 votes |
/** * 获取指定時間一周的日期列表 * * @param date * @return */ public static Date[] getWeek(Date date) { Date[] dates = new Date[7]; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); for (int i = 0; i < 7; i++) { dates[i] = calendar.getTime(); calendar.add(Calendar.DAY_OF_YEAR, 1); } return dates; }
Example 16
Source File: DateUtil.java From xiaoV with GNU General Public License v3.0 | 5 votes |
public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); }
Example 17
Source File: DateOperation.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
/** * 获取日期在一年中的周数 * 结果从1开始 * @param dateString yyyy-mm-dd * @return * @throws Exception */ public int getWeekNumOfYear( String dateString ) throws Exception{ Date date = getDateFromString( dateString , "yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.setFirstDayOfWeek(2); //设置每周的第一天是星期一 return cal.get(Calendar.WEEK_OF_YEAR); }
Example 18
Source File: CalendarRegression.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * 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 19
Source File: IndexNameHelper.java From phoebus with Eclipse Public License 1.0 | 4 votes |
private void setDateSpanStartAndEnd(Instant time) { Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(time.toEpochMilli()); calendar.setFirstDayOfWeek(Calendar.SUNDAY); Integer dateSpanField = -1; if (dateSpanUnit.equals(YEAR)) { // Roll the year back to the beginning (midnight of the first of the year). dateSpanField = Calendar.YEAR; calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.WEEK_OF_MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); } if (dateSpanUnit.equals(MONTH)) { // Roll the month back to the beginning (midnight of the first of the month). dateSpanField = Calendar.MONTH; calendar.set(Calendar.WEEK_OF_MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); } if (dateSpanUnit.equals(WEEK)) { // Roll the week back to the beginning (midnight Sunday). dateSpanField = Calendar.WEEK_OF_YEAR; calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); } if (dateSpanUnit.equals(DAY)) dateSpanField = Calendar.DATE; // Roll the day back to midnight calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); spanStart = calendar.toInstant(); calendar.add(dateSpanField, dateSpanValue); spanEnd = calendar.toInstant(); }
Example 20
Source File: DatesUtils.java From mvvm-template with GNU General Public License v3.0 | 3 votes |
/** * Return if the date given is a first day of week * * @param year * The year of the date. * @param month * The month of the date. * @param day * The day of the date. * @return true or false */ @SuppressLint("WrongConstant") public static boolean isFirstDayOfWeek(int year, int month, int day) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day); calendar.add(Calendar.SECOND, 0); calendar.setFirstDayOfWeek(Calendar.SUNDAY); return calendar.get(Calendar.DAY_OF_WEEK) == 1; }