Example usage for java.util Calendar setMinimalDaysInFirstWeek

List of usage examples for java.util Calendar setMinimalDaysInFirstWeek

Introduction

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

Prototype

public void setMinimalDaysInFirstWeek(int value) 

Source Link

Document

Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1.

Usage

From source file:Main.java

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    // get what is the minimal days required in the first week
    int min = cal.getMinimalDaysInFirstWeek();
    System.out.println("Minimal Days in Week: " + min);

    // set the minimal days required in the first week
    cal.setMinimalDaysInFirstWeek(2);

    // print the result
    min = cal.getMinimalDaysInFirstWeek();
    System.out.println("Minimal Days in Week: " + min);
}

From source file:jp.classmethod.aws.brian.job.BrianQuartzJobBean.java

static SimpleDateFormat createFormat() {
    SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan")); // TODO localize
    calendar.setMinimalDaysInFirstWeek(4);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTimeInMillis(0);/*w  w  w .  ja  v a 2 s  .  c  om*/
    format.setCalendar(calendar);
    return format;
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Object weekOfYearISO8601(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null)
        return null;

    if (metaA.isDate()) {
        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));
    }/* ww w  .  j av  a 2 s  .  co m*/

    throw new KettleValueException("The 'weekOfYearISO8601' function only works with dates");
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Object yearOfDateISO8601(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null)
        return null;

    if (metaA.isDate()) {
        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--;//  www .  j  a  va  2  s  .c o m
        if (week <= 2 && month == 11)
            year++;

        return new Long(year);
    }

    throw new KettleValueException("The 'yearOfDateISO8601' function only works with dates");
}

From source file:org.jfree.data.time.WeekTest.java

/**
 * 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./*from  w  w  w  . ja  va2 s.c om*/
 *
 * 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));
}

From source file:org.jfree.data.time.WeekTest.java

/**
 * 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.//from   ww w  .  ja  v  a2 s  .  co m
 */
@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));
}

From source file:com.rogchen.common.xml.UtilDateTime.java

public static int weekNumber(Timestamp input, int startOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(startOfWeek);

    if (startOfWeek == Calendar.MONDAY) {
        calendar.setMinimalDaysInFirstWeek(4);
    } else if (startOfWeek == Calendar.SUNDAY) {
        calendar.setMinimalDaysInFirstWeek(3);
    }/*from   w w  w  . j  a va2s  . c o m*/

    calendar.setTime(new Date(input.getTime()));
    return calendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:org.cs.basic.test.util.DateUtils.java

/**?
 * @param date/*from  www.  ja v a  2 s. c  om*/
 * @return
 */
public static int weekOfDay(Date date) {
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY); //1   
    c.setMinimalDaysInFirstWeek(4); //?4??1   

    c.setTime(date);
    int weeknum = c.get(Calendar.WEEK_OF_YEAR);
    int vyear = c.get(Calendar.YEAR);
    int vmonth = c.get(Calendar.MONTH) + 1;

    //1????1   
    if (vmonth == 1 && weeknum > 6) {
        vyear--;
    }

    //????1   
    if (vmonth == 12 && weeknum == 1) {
        vyear++;
    }
    /*DateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
    System.out.println(format.format(date)+",weeknum:"+weeknum+",year:"+vyear);*/
    return weeknum;
}

From source file:org.openossad.util.core.row.ValueDataUtil.java

public static Object weekOfYearISO8601(ValueMetaInterface metaA, Object dataA)
        throws OpenDESIGNERValueException {
    if (dataA == null)
        return null;

    if (metaA.isDate()) {
        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));
    }/*from w  ww  .  ja  v  a2s .  c o m*/

    throw new OpenDESIGNERValueException("The 'weekOfYearISO8601' function only works with dates");
}

From source file:org.openossad.util.core.row.ValueDataUtil.java

public static Object yearOfDateISO8601(ValueMetaInterface metaA, Object dataA)
        throws OpenDESIGNERValueException {
    if (dataA == null)
        return null;

    if (metaA.isDate()) {
        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--;// ww  w .j  a  va2  s . co m
        if (week <= 2 && month == 11)
            year++;

        return new Long(year);
    }

    throw new OpenDESIGNERValueException("The 'yearOfDateISO8601' function only works with dates");
}