Java Code Examples for java.util.Calendar.setMinimalDaysInFirstWeek()

The following are Jave code examples for showing how to use setMinimalDaysInFirstWeek() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 8 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) {
        week = 0;
    } else if (weekYear > year) {
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 2
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 8 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
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 8 votes
/**
 * 一周的日期
 * @param date  时间
 * @return  结果
 */
public static List<Date> getWeekDayOfYear(Date date) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int year = calendar.get(Calendar.YEAR);

    List<Date> result = new ArrayList<Date>();
    result.add(getDateOfYearWeek(year, week, Calendar.MONDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.TUESDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.WEDNESDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.THURSDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.FRIDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.SATURDAY));
    result.add(getDateOfYearWeek(year, week, Calendar.SUNDAY));
    return result;
}
 
Example 4
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 7 votes
@Override
public String format(DateTime timestamp) {
    // if week starts in last year, return 00
    // range 00 - 53
    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) {
        week = 0;
    } else if (weekYear > year) {
        week = c.getWeeksInWeekYear();
    }
    return zeroPadded(2, String.valueOf(week));
}
 
Example 5
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 7 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 6
Project: raven   File: DateUtils.java   View Source Code Vote up 7 votes
/**
 * getWeekOfYear
 * 计算当前时间属于一年的第几周,并计算出相对于基准时间 offsetDate的周数<br/>
 * 如果 基准时间 offsetDate 为 null 则仅返回当前时间属于一年中的第几周<br/>
 * 一周从 星期一开始计算  <br/>
 * 遵循 ISO8601 week algorithm, 即 一年的第一周至少有4天及以上
 * @param date
 * @param offsetDate
 * @return
 * @return int  返回类型 
 * @author wangwei
 * @date 2016年6月24日 下午2:39:13 
 * @version  [1.0, 2016年6月24日]
 * @since  version 1.0
 */
public static int getWeekOfYear(Date date, Date offsetDate)
{
	Calendar calendar1 = Calendar.getInstance();
	calendar1.setFirstDayOfWeek(Calendar.MONDAY);
	calendar1.setMinimalDaysInFirstWeek(4);
	calendar1.setTime(date);
	int weekOfYear = calendar1.get(Calendar.WEEK_OF_YEAR);
	
	if(null == offsetDate){
		return weekOfYear;
	}
	
	Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(offsetDate);
calendar2.setFirstDayOfWeek(Calendar.MONDAY);
calendar2.setMinimalDaysInFirstWeek(4);
int weekOfYear2 = calendar2.get(Calendar.WEEK_OF_YEAR);
  
int tmp = weekOfYear - weekOfYear2 + 1;
return tmp < 0 ? tmp+52 : tmp; // 跨年处理
}
 
Example 7
Project: androidtools   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * Get weeks
 *
 * @param mills mills
 * @return weeks
 */
public static int getWeekInMills(long mills) {
    Calendar cal = Calendar.getInstance();//This sentence must be set, otherwise, the United States believes that the first day is Sunday, and our country believes that Monday, the calculation of the current date is the first few weeks there will be errors.
    cal.setFirstDayOfWeek(Calendar.MONDAY); // The first day of the week set for Monday.
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// Starting every Monday.
    cal.setMinimalDaysInFirstWeek(7); // Set for at least 7 days a week.
    cal.setTime(new Date());
    int weeks1 = cal.get(Calendar.WEEK_OF_YEAR);
    return weeks1;
}
 
Example 8
Project: amap   File: MIP_DateUtil.java   View Source Code Vote up 6 votes
/**
 * 取得当前日期是多少周
 * 
 * @param date
 * @return
 */
public static int getWeekOfYear(Date date)
{
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTime(date);
    
    return c.get(Calendar.WEEK_OF_YEAR);
}
 
Example 9
Project: amap   File: MIP_TimeUtils.java   View Source Code Vote up 6 votes
/**
 * 取得当前日期是多少周
 * 
 * @param date
 * @return
 */
public static int getWeekOfYear(Date date)
{
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setMinimalDaysInFirstWeek(7);
    c.setTime(date);
    
    return c.get(Calendar.WEEK_OF_YEAR);
}
 
Example 10
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 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.withZone(DateTimeZone.UTC).getMillis());
    return zeroPadded(4, String.valueOf(c.getWeekYear()));
}
 
Example 11
Project: Elasticsearch   File: TimestampFormatter.java   View Source Code Vote up 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.withZone(DateTimeZone.UTC).getMillis());
    return zeroPadded(4, String.valueOf(c.getWeekYear()));
}
 
Example 12
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 6 votes
/**
  * 获取一年中某周,星期几的日期
  *
  * @param yearNum   年
  * @param weekNum   那一周
  * @param dayOfWeek 周几
  * @return  日期
  */
 private static Date getDateOfYearWeek(int yearNum, int weekNum,
                                       int dayOfWeek) {
     Calendar cal = Calendar.getInstance();
     cal.setFirstDayOfWeek(Calendar.MONDAY);
     cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
     cal.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
     cal.set(Calendar.YEAR, yearNum);
     cal.set(Calendar.WEEK_OF_YEAR, weekNum);
     /*
      * cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0);
* cal.set(Calendar.SECOND, 0);
*/
     return cal.getTime();
 }
 
Example 13
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 6 votes
/**
 * 本周指定星期几的日期
 *
 * @param date  指定的日期
 * @param day   那一天
 * @return  结果
 */
public static String getCurrentWeekDay(Date date, int day) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int year = calendar.get(Calendar.YEAR);

    return formatDate(getDateOfYearWeek(year, week, day));
}
 
Example 14
Project: parabuild-ci   File: WeekTests.java   View Source Code Vote up 5 votes
/**
 * The first week in 2005 should span the range:
 * 
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * Europe/London    | 1104710400000 | 1105315199999 |  3-Jan-2005 | 9-Jan-2005
 * Europe/Paris     | 1104706800000 | 1105311599999 |  3-Jan-2005 | 2-Jan-2005
 * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
 * 
 * In London and Paris, Monday is the first day of the week, while in the 
 * US it is Sunday.
 * 
 * Previously, we were using these values, but see Java Bug ID 4960215:
 * 
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * Europe/London    | 1104105600000 | 1104710399999 | 27-Dec-2004 | 2-Jan-2005
 * Europe/Paris     | 1104102000000 | 1104706799999 | 27-Dec-2004 | 2-Jan-2005
 * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
 */
public void testWeek12005() {
    Week w1 = new Week(1, 2005);
    Calendar c1 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/London"), Locale.UK);
    c1.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104710400000L, w1.getFirstMillisecond(c1));
    assertEquals(1105315199999L, w1.getLastMillisecond(c1));
    Calendar c2 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
    c2.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104706800000L, w1.getFirstMillisecond(c2));
    assertEquals(1105311599999L, w1.getLastMillisecond(c2));
    Calendar c3 = Calendar.getInstance(
            TimeZone.getTimeZone("America/New_York"), Locale.US);
    assertEquals(1104037200000L, w1.getFirstMillisecond(c3));
    assertEquals(1104641999999L, w1.getLastMillisecond(c3));   
}
 
Example 15
Project: openjdk-jdk10   File: CalendarTest.java   View Source Code Vote up 5 votes
public void TestActualMinMax() {
    Calendar cal = new GregorianCalendar(1967, MARCH, 10);
    cal.setFirstDayOfWeek(SUNDAY);
    cal.setMinimalDaysInFirstWeek(3);

    if (cal.getActualMinimum(DAY_OF_MONTH) != 1) {
        errln("Actual minimum date for 3/10/1967 should have been 1; got "
                + cal.getActualMinimum(DAY_OF_MONTH));
    }
    if (cal.getActualMaximum(DAY_OF_MONTH) != 31) {
        errln("Actual maximum date for 3/10/1967 should have been 31; got "
                + cal.getActualMaximum(DAY_OF_MONTH));
    }

    cal.set(MONTH, FEBRUARY);
    if (cal.getActualMaximum(DAY_OF_MONTH) != 28) {
        errln("Actual maximum date for 2/10/1967 should have been 28; got "
                + cal.getActualMaximum(DAY_OF_MONTH));
    }
    if (cal.getActualMaximum(DAY_OF_YEAR) != 365) {
        errln("Number of days in 1967 should have been 365; got "
                + cal.getActualMaximum(DAY_OF_YEAR));
    }

    cal.set(YEAR, 1968);
    if (cal.getActualMaximum(DAY_OF_MONTH) != 29) {
        errln("Actual maximum date for 2/10/1968 should have been 29; got "
                + cal.getActualMaximum(DAY_OF_MONTH));
    }
    if (cal.getActualMaximum(DAY_OF_YEAR) != 366) {
        errln("Number of days in 1968 should have been 366; got "
                + cal.getActualMaximum(DAY_OF_YEAR));
    }
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(WEEK_OF_YEAR) != 52) {
        errln("Number of weeks in 1968 should have been 52; got "
                + cal.getActualMaximum(WEEK_OF_YEAR));
    }

    cal.set(YEAR, 1976);
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(WEEK_OF_YEAR) != 53) {
        errln("Number of weeks in 1976 should have been 53; got "
                + cal.getActualMaximum(WEEK_OF_YEAR));
    }
}
 
Example 16
Project: openjdk-jdk10   File: CalendarRegression.java   View Source Code Vote up 5 votes
/**
 * Calendar and GregorianCalendar hashCode() methods need improvement.
 * Calendar needs a good implementation that subclasses can override,
 * and GregorianCalendar should use that implementation.
 */
public void Test4136399() {
    /* Note: This test is actually more strict than it has to be.
    * Technically, there is no requirement that unequal objects have
    * unequal hashes.  We only require equal objects to have equal hashes.
    * It is desirable for unequal objects to have distributed hashes, but
    * there is no hard requirement here.
    *
    * In this test we make assumptions about certain attributes of calendar
    * objects getting represented in the hash, which need not always be the
    * case (although it does work currently with the given test). */
    Calendar a = Calendar.getInstance();
    Calendar b = (Calendar) a.clone();
    if (a.hashCode() != b.hashCode()) {
        errln("Calendar hash code unequal for cloned objects");
    }

    b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores minimal days in first week");
    }
    b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());

    b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores first day of week");
    }
    b.setFirstDayOfWeek(a.getFirstDayOfWeek());

    b.setLenient(!a.isLenient());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores lenient setting");
    }
    b.setLenient(a.isLenient());

    // Assume getTimeZone() returns a reference, not a clone
    // of a reference -- this is true as of this writing
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset() + 60 * 60 * 1000);
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores zone");
    }
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());

    GregorianCalendar c = new GregorianCalendar();
    GregorianCalendar d = (GregorianCalendar) c.clone();
    if (c.hashCode() != d.hashCode()) {
        errln("GregorianCalendar hash code unequal for clones objects");
    }
    Date cutover = c.getGregorianChange();
    d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000));
    if (c.hashCode() == d.hashCode()) {
        errln("GregorianCalendar hash code ignores cutover");
    }
}
 
Example 17
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 3 votes
/**
 * 获取指定日期是一周的第几天,星期日是第一天
 *
 * @param date 日期
 * @return  结果
 */
public static int getDayOfWeek(Date date) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.setMinimalDaysInFirstWeek(DAYS_OF_A_WEEK);
    return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}