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

The following examples show how to use java.util.Calendar#setMinimalDaysInFirstWeek() . 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: TimestampFormatter.java    From crate with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: WeekTest.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * The 53rd week in 2004 in London and Paris should span the range:
 *
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * Europe/London    | 1104105600000 | 1104710399999 | 27-Dec-2004 | 02-Jan-2005
 * Europe/Paris     | 1104102000000 | 1104706799999 | 27-Dec-2004 | 02-Jan-2005
 *
 * The 53rd week in 2005 in New York should span the range:
 *
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * America/New_York | 1135486800000 | 1136091599999 | 25-Dec-2005 | 31-Dec-2005
 *
 * In London and Paris, Monday is the first day of the week, while in the
 * US it is Sunday.
 */
@Test
public void testWeek532005() {
    Week w1 = new Week(53, 2004);
    Calendar c1 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/London"), Locale.UK);
    c1.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104105600000L, w1.getFirstMillisecond(c1));
    assertEquals(1104710399999L, w1.getLastMillisecond(c1));
    Calendar c2 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
    c2.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104102000000L, w1.getFirstMillisecond(c2));
    assertEquals(1104706799999L, w1.getLastMillisecond(c2));
    w1 = new Week(53, 2005);
    Calendar c3 = Calendar.getInstance(
            TimeZone.getTimeZone("America/New_York"), Locale.US);
    assertEquals(1135486800000L, w1.getFirstMillisecond(c3));
    assertEquals(1136091599999L, w1.getLastMillisecond(c3));
}
 
Example 3
Source File: ValueDataUtil.java    From hop with Apache License 2.0 6 votes vote down vote up
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 4
Source File: WeekTest.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The 53rd week in 2004 in London and Paris should span the range:
 *
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * Europe/London    | 1104105600000 | 1104710399999 | 27-Dec-2004 | 02-Jan-2005
 * Europe/Paris     | 1104102000000 | 1104706799999 | 27-Dec-2004 | 02-Jan-2005
 *
 * The 53rd week in 2005 in New York should span the range:
 *
 * TimeZone         | Start Millis  | End Millis    | Start Date  | End Date
 * -----------------+---------------+---------------+-------------+------------
 * America/New_York | 1135486800000 | 1136091599999 | 25-Dec-2005 | 31-Dec-2005
 *
 * In London and Paris, Monday is the first day of the week, while in the
 * US it is Sunday.
 */
@Test
public void testWeek532005() {
    Week w1 = new Week(53, 2004);
    Calendar c1 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/London"), Locale.UK);
    c1.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104105600000L, w1.getFirstMillisecond(c1));
    assertEquals(1104710399999L, w1.getLastMillisecond(c1));
    Calendar c2 = Calendar.getInstance(
            TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
    c2.setMinimalDaysInFirstWeek(4);  // see Java Bug ID 4960215
    assertEquals(1104102000000L, w1.getFirstMillisecond(c2));
    assertEquals(1104706799999L, w1.getLastMillisecond(c2));
    w1 = new Week(53, 2005);
    Calendar c3 = Calendar.getInstance(
            TimeZone.getTimeZone("America/New_York"), Locale.US);
    assertEquals(1135486800000L, w1.getFirstMillisecond(c3));
    assertEquals(1136091599999L, w1.getLastMillisecond(c3));
}
 
Example 5
Source File: PeriodParser.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 6
Source File: WeekPicker.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Calendar createDateFromWeek(int year, int week) {
    Calendar date = Calendar.getInstance();
    date.clear();
    date.setFirstDayOfWeek(Calendar.MONDAY);
    date.setMinimalDaysInFirstWeek(4);
    date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    date.set(Calendar.YEAR, year);
    date.set(Calendar.WEEK_OF_YEAR, week);
    return date;
}
 
Example 7
Source File: WeekPicker.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public WeekPicker(Context context, long minValue, long maxValue) {
    super(context, minValue, maxValue);

    getPositionInYearSpinner().setContentDescription(
            getResources().getString(R.string.accessibility_date_picker_week));

    // initialize to current date
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    cal.setTimeInMillis(System.currentTimeMillis());
    init(getISOWeekYearForDate(cal), getWeekForDate(cal), null);
}
 
Example 8
Source File: CalendarWeekOfMonthTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    Calendar calendar = new GregorianCalendar(
            TimeZone.getTimeZone("America/Los_Angeles"), Locale.US);
    calendar.setFirstDayOfWeek(firstDayOfWeek);
    calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
    calendar.setTimeInMillis(timeInMillis);

    assertEquals(toString(), expectedWeekOfMonth, calendar.get(Calendar.WEEK_OF_MONTH));
}
 
Example 9
Source File: TimestampFormatter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: CalendarUtils.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Create a calendar instance that uses mondays or sundays as the first day of the week depending on the given locale and sets the week number 1 to the first week in
 * the year that has four days of january.
 * 
 * @param local
 *            the locale to define if a week starts on sunday or monday
 * @return a calendar instance
 */
public static Calendar createCalendarInstance(final Locale locale) {
    // use Calendar.getInstance(locale) that sets first day of week
    // according to locale or let user decide in GUI
    final Calendar cal = Calendar.getInstance(locale);
    // manually set min days to 4 as we are used to have it
    cal.setMinimalDaysInFirstWeek(4);
    return cal;
}
 
Example 11
Source File: StringFormatTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Date getDate( int year, int month, int day )
{
    final Calendar calendar = Calendar.getInstance();

    // override locale settings for weeks
    calendar.setFirstDayOfWeek( Calendar.MONDAY );
    calendar.setMinimalDaysInFirstWeek( 4 );

    calendar.clear();
    calendar.set( year, month - 1, day );

    return calendar.getTime();
}
 
Example 12
Source File: WeekPicker.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public WeekPicker(Context context, double minValue, double maxValue) {
    super(context, minValue, maxValue);

    getPositionInYearSpinner().setContentDescription(
            getResources().getString(R.string.accessibility_date_picker_week));

    // initialize to current date
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    cal.setTimeInMillis(System.currentTimeMillis());
    init(getISOWeekYearForDate(cal), getWeekForDate(cal), null);
}
 
Example 13
Source File: ValueDataUtil.java    From hop with Apache License 2.0 5 votes vote down vote up
public static Object weekOfYearISO8601( 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 ) );
  return new Long( calendar.get( Calendar.WEEK_OF_YEAR ) );

}
 
Example 14
Source File: PatternProcessor.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the next potential rollover time.
 * @param currentMillis The current time.
 * @param increment The increment to the next time.
 * @param modulus If true the time will be rounded to occur on a boundary aligned with the increment.
 * @return the next potential rollover time and the timestamp for the target file.
 */
public long getNextTime(final long currentMillis, final int increment, final boolean modulus) {
    //
    // https://issues.apache.org/jira/browse/LOG4J2-1232
    // Call setMinimalDaysInFirstWeek(7);
    //
    prevFileTime = nextFileTime;
    long nextTime;

    if (frequency == null) {
        throw new IllegalStateException("Pattern does not contain a date");
    }
    final Calendar currentCal = Calendar.getInstance();
    currentCal.setTimeInMillis(currentMillis);
    final Calendar cal = Calendar.getInstance();
    currentCal.setMinimalDaysInFirstWeek(7);
    cal.setMinimalDaysInFirstWeek(7);
    cal.set(currentCal.get(Calendar.YEAR), 0, 1, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    if (frequency == RolloverFrequency.ANNUALLY) {
        increment(cal, Calendar.YEAR, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.MONTH, currentCal.get(Calendar.MONTH));
    if (frequency == RolloverFrequency.MONTHLY) {
        increment(cal, Calendar.MONTH, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.MONTH, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    if (frequency == RolloverFrequency.WEEKLY) {
        cal.set(Calendar.WEEK_OF_YEAR, currentCal.get(Calendar.WEEK_OF_YEAR));
        increment(cal, Calendar.WEEK_OF_YEAR, increment, modulus);
        cal.set(Calendar.DAY_OF_WEEK, currentCal.getFirstDayOfWeek());
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.DAY_OF_YEAR, currentCal.get(Calendar.DAY_OF_YEAR));
    if (frequency == RolloverFrequency.DAILY) {
        increment(cal, Calendar.DAY_OF_YEAR, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.HOUR_OF_DAY, currentCal.get(Calendar.HOUR_OF_DAY));
    if (frequency == RolloverFrequency.HOURLY) {
        increment(cal, Calendar.HOUR_OF_DAY, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.HOUR_OF_DAY, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.MINUTE, currentCal.get(Calendar.MINUTE));
    if (frequency == RolloverFrequency.EVERY_MINUTE) {
        increment(cal, Calendar.MINUTE, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.MINUTE, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.SECOND, currentCal.get(Calendar.SECOND));
    if (frequency == RolloverFrequency.EVERY_SECOND) {
        increment(cal, Calendar.SECOND, increment, modulus);
        nextTime = cal.getTimeInMillis();
        cal.add(Calendar.SECOND, -1);
        nextFileTime = cal.getTimeInMillis();
        return debugGetNextTime(nextTime);
    }
    cal.set(Calendar.MILLISECOND, currentCal.get(Calendar.MILLISECOND));
    increment(cal, Calendar.MILLISECOND, increment, modulus);
    nextTime = cal.getTimeInMillis();
    cal.add(Calendar.MILLISECOND, -1);
    nextFileTime = cal.getTimeInMillis();
    return debugGetNextTime(nextTime);
}
 
Example 15
Source File: WeekTest.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * 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
 */
@Test
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 16
Source File: CalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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 17
Source File: WeekTest.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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
 */
@Test
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 18
Source File: WeekTests.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 19
Source File: WeekTest.java    From openstock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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
 */
@Test
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 20
Source File: DateUtil.java    From lorne_core with Apache License 2.0 2 votes vote down vote up
/**
 * 获取指定日期是一周的第几天,星期日是第一天
 *
 * @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;
}