Example usage for java.util Calendar WEEK_OF_YEAR

List of usage examples for java.util Calendar WEEK_OF_YEAR

Introduction

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

Prototype

int WEEK_OF_YEAR

To view the source code for java.util Calendar WEEK_OF_YEAR.

Click Source Link

Document

Field number for get and set indicating the week number within the current year.

Usage

From source file:com.feilong.commons.core.date.DateUtil.java

/**
 * ? {@link Calendar#WEEK_OF_YEAR}(week?,??)<br>
 * ,./*w w w .j a va 2 s.co  m*/
 * 
 * <pre>
 * Example 1:
 * addWeek(2012-06-29 00:45:18,5)
 * return 2012-08-03 00:45:18
 * 
 * Example 2:
 * addWeek(2012-06-29 00:45:18,-5)
 * return 2012-05-25 00:45:18
 * </pre>
 * 
 * @param date
 *            
 * @param week
 *            ?? ?
 * @return ?
 * @see #operateDate(Date, int, int)
 * @see Calendar#WEEK_OF_YEAR
 * @since 1.0
 */
public static final Date addWeek(Date date, int week) {
    return operateDate(date, Calendar.WEEK_OF_YEAR, week);
}

From source file:com.ibm.mil.readyapps.physio.fragments.ProgressFragment.java

private void fetchData(final TimeFilter filter) {
    fetchPainData(filter);//from w  ww  .j  a  va  2 s  . co m

    Calendar calendar = Calendar.getInstance();
    Date endDate = new Date();
    calendar.setTime(endDate);

    TimeUnit unit;
    int interval;
    int bound;
    switch (filter) {
    case DAY:
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        calendar.add(Calendar.HOUR, 1);
        unit = TimeUnit.HOURS;
        interval = 1;
        bound = DAY_BOUND;
        break;
    case WEEK:
        calendar.add(Calendar.WEEK_OF_YEAR, -1);
        unit = TimeUnit.DAYS;
        interval = 1;
        bound = WEEK_BOUND;
        break;
    case MONTH:
        calendar.add(Calendar.MONTH, -1);
        unit = TimeUnit.DAYS;
        interval = 7;
        bound = MONTH_BOUND;
        break;
    case YEAR:
    default:
        calendar.add(Calendar.YEAR, -1);
        calendar.add(Calendar.MONTH, 1);
        unit = TimeUnit.DAYS;
        interval = 30;
        bound = YEAR_BOUND;
        break;
    }

    Date startDate = calendar.getTime();

    HealthDataRetriever.Builder builder = new HealthDataRetriever.Builder().startDate(startDate)
            .endDate(endDate).timeUnit(unit).timeInterval(interval);

    HealthDataRetriever stepsRetriever = builder.dataType(HealthDataRetriever.DataType.STEPS)
            .handler(new HealthDataRetriever.Handler() {
                @Override
                public void handle(List<Integer> data) {
                    switch (filter) {
                    case DAY:
                        stepsDay = data;
                        break;
                    case WEEK:
                        stepsWeek = data;
                        break;
                    case MONTH:
                        stepsMonth = data;
                        break;
                    case YEAR:
                        stepsYear = data;
                        break;
                    }

                    verifyDataIntegrity();
                }
            }).build();
    stepsRetriever.retrieve(mClient);

    HealthDataRetriever heartRetriever = builder.dataType(HealthDataRetriever.DataType.HEART_RATE)
            .handler(new HealthDataRetriever.Handler() {
                @Override
                public void handle(List<Integer> data) {
                    switch (filter) {
                    case DAY:
                        heartDay = data;
                        break;
                    case WEEK:
                        heartWeek = data;
                        break;
                    case MONTH:
                        heartMonth = data;
                        break;
                    case YEAR:
                        heartYear = data;
                        break;
                    }

                    verifyDataIntegrity();
                }
            }).build();
    heartRetriever.retrieve(mClient);

    HealthDataRetriever weightRetriever = builder.dataType(HealthDataRetriever.DataType.WEIGHT)
            .handler(new HealthDataRetriever.Handler() {
                @Override
                public void handle(List<Integer> data) {
                    switch (filter) {
                    case DAY:
                        weightDay = data;
                        break;
                    case WEEK:
                        weightWeek = data;
                        break;
                    case MONTH:
                        weightMonth = data;
                        break;
                    case YEAR:
                        weightYear = data;
                        break;
                    }

                    verifyDataIntegrity();
                }
            }).build();
    weightRetriever.retrieve(mClient);

    calendar.setTime(endDate);
    int caloriesInterval = bound == DAY_BOUND ? Calendar.HOUR : Calendar.DAY_OF_YEAR;

    for (int i = 0; i < bound; i++) {
        endDate = calendar.getTime();
        calendar.add(caloriesInterval, -interval);
        startDate = calendar.getTime();

        HealthDataRetriever caloriesRetriever = builder.startDate(startDate).endDate(endDate)
                .dataType(HealthDataRetriever.DataType.CALORIES).handler(new HealthDataRetriever.Handler() {
                    @Override
                    public void handle(List<Integer> data) {
                        int sum = 0;
                        for (Integer datum : data) {
                            sum += datum;
                        }

                        switch (filter) {
                        case DAY:
                            if (caloriesDay == null) {
                                caloriesDay = new ArrayList<>();
                            }
                            caloriesDay.add(0, sum);
                            break;
                        case WEEK:
                            if (caloriesWeek == null) {
                                caloriesWeek = new ArrayList<>();
                            }
                            caloriesWeek.add(0, sum);
                            break;
                        case MONTH:
                            if (caloriesMonth == null) {
                                caloriesMonth = new ArrayList<>();
                            }
                            caloriesMonth.add(0, sum);
                            break;
                        case YEAR:
                            if (caloriesYear == null) {
                                caloriesYear = new ArrayList<>();
                            }
                            caloriesYear.add(0, sum);
                            break;
                        }

                        verifyDataIntegrity();
                    }
                }).build();
        caloriesRetriever.retrieve(mClient);
    }
}

From source file:org.nuclos.common2.DateUtils.java

private static Date dateFromQuant(Calendar cal, int quant, int quantizer) {
    switch (quantizer) {
    case Calendar.WEEK_OF_YEAR:
        final Date old = cal.getTime();

        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.WEEK_OF_YEAR, quant);
        // cal.set(Calendar.DAY_OF_WEEK, WEEK_START_DAY);

        if (old.before(cal.getTime())) {
            montoneAdd(cal, Calendar.YEAR, -1);
            montoneAdd(cal, Calendar.WEEK_OF_YEAR, 1);
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            assert cal.getTime().before(old);
        }/*from   ww  w  . j  a v  a2  s  .  c  om*/
        break;
    case Calendar.DAY_OF_WEEK:
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.DAY_OF_WEEK, quant);
        break;
    case Calendar.DAY_OF_MONTH:
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.DAY_OF_MONTH, quant);
        break;
    case Calendar.MONTH:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.MONTH, quant);
        break;
    case QUARTER:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);

        cal.set(Calendar.MONTH, quant * 3);
        break;
    case Calendar.YEAR:
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MONTH, 0);
        break;
    default:
        throw new IllegalArgumentException();
    }
    final Date result = cal.getTime();
    return result;
}

From source file:com.redhat.rhn.frontend.action.monitoring.notification.BaseFilterEditAction.java

private String getRecurringDigits(Long durationIn, int durationTypeIn) {
    long duration = durationIn.longValue();
    if (durationTypeIn == Calendar.MINUTE) {
        // durationType == 12
        // NOOP since minutes is the base type.
    } else if (durationTypeIn == Calendar.HOUR_OF_DAY) {
        // durationType == 11
        duration = duration / 60;//  w  w w  .java  2s  .  c om
    } else if (durationTypeIn == Calendar.DAY_OF_MONTH) {
        // durationType == 5
        duration = duration / 60 / 24;
    } else if (durationTypeIn == Calendar.WEEK_OF_YEAR) {
        // durationType == 3
        duration = duration / 60 / 24 / 7;
    } else if (durationTypeIn == Calendar.YEAR) {
        // durationType == 1
        duration = duration / 60 / 24 / 7 / 365;
    } else {
        throw new IllegalArgumentException("Durration for recurring "
                + "should be either Calendar.MINUTE, HOURS_OF_DAY, " + "DAY_OF_MONTH, WEEK_OF_YEAR, YEAR");
    }
    return new Long(duration).toString();
}

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

/**
 * Returns the last millisecond of the week, evaluated using the supplied
 * calendar (which determines the time zone).
 *
 * @param calendar  the calendar (<code>null</code> not permitted).
 *
 * @return The last millisecond of the week.
 *
 * @throws NullPointerException if <code>calendar</code> is
 *     <code>null</code>.//from  w w w .  j  a  v  a2s . c  o  m
 */
@Override
public long getLastMillisecond(Calendar calendar) {
    Calendar c = (Calendar) calendar.clone();
    c.clear();
    c.set(Calendar.YEAR, this.year);
    c.set(Calendar.WEEK_OF_YEAR, this.week + 1);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTimeInMillis() - 1;
}

From source file:fr.paris.lutece.plugins.form.utils.FormUtils.java

/**
 * Compare two timestamp and return true if  they have the same times unit(Day,week,month)
 * @param  timestamp1 timestamp1/*from  ww w .  ja v  a  2  s . c  o m*/
 * @param  timestamp2 timestamp2
 * @param strTimesUnit (day,week,month)
 * @return Compare two timestamp and return true if  they have the same times unit(Day,week,month)
 */
public static boolean sameDate(Timestamp timestamp1, Timestamp timestamp2, String strTimesUnit) {
    Calendar caldate1 = new GregorianCalendar();
    caldate1.setTime(timestamp1);

    Calendar caldate2 = new GregorianCalendar();
    caldate2.setTime(timestamp2);

    if (strTimesUnit.equals(CONSTANT_GROUP_BY_DAY)
            && (caldate1.get(Calendar.YEAR) == caldate2.get(Calendar.YEAR))
            && (caldate1.get(Calendar.DAY_OF_YEAR) == caldate2.get(Calendar.DAY_OF_YEAR))) {
        return true;
    } else if (strTimesUnit.equals(CONSTANT_GROUP_BY_WEEK)
            && (caldate1.get(Calendar.YEAR) == caldate2.get(Calendar.YEAR))
            && (caldate1.get(Calendar.WEEK_OF_YEAR) == caldate2.get(Calendar.WEEK_OF_YEAR))) {
        return true;
    } else if (strTimesUnit.equals(CONSTANT_GROUP_BY_MONTH)
            && (caldate1.get(Calendar.YEAR) == caldate2.get(Calendar.YEAR))
            && (caldate1.get(Calendar.MONTH) == caldate2.get(Calendar.MONTH))) {
        return true;
    }

    return false;
}

From source file:com.px100systems.util.SpringELCtx.java

/**
 * Date arithmetic./* w w  w. j a  v  a  2  s. com*/
 * @param date time
 * @param arg add/subtract value: "+/-" + "m/s/h/d/w/M/y" e.g. "+5m" or "-3d"
 * @return the adjusted time
 */
public static Date dateArithmetic(Date date, String arg) {
    if (date == null)
        return null;

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int amount = Integer.parseInt(arg.substring(1, arg.length() - 1));
    if (arg.startsWith("-"))
        amount = -amount;

    int field;
    if (arg.endsWith("h"))
        field = Calendar.HOUR_OF_DAY;
    else if (arg.endsWith("m"))
        field = Calendar.MINUTE;
    else if (arg.endsWith("s"))
        field = Calendar.SECOND;
    else if (arg.endsWith("d"))
        field = Calendar.DAY_OF_MONTH;
    else if (arg.endsWith("M"))
        field = Calendar.MONTH;
    else if (arg.endsWith("y"))
        field = Calendar.YEAR;
    else if (arg.endsWith("w"))
        field = Calendar.WEEK_OF_YEAR;
    else
        return date;

    cal.add(field, amount);
    return cal.getTime();
}

From source file:org.onebusaway.nyc.vehicle_tracking.webapp.controllers.VehicleLocationSimulationController.java

private Date getTime(HttpSession session, Date time) {

    if (time == null)
        time = new Date();

    String calendarOffset = (String) session.getAttribute(CALENDAR_OFFSET_KEY);

    if (calendarOffset != null) {

        Matcher m = CALENDAR_OFFSET_PATTERN.matcher(calendarOffset);
        if (m.matches()) {

            int amount = Integer.parseInt(m.group(1));
            String type = m.group(2);

            Calendar c = Calendar.getInstance();
            c.setTime(time);//from w  ww  . j  a va2s . c  o  m

            if (type.equals("M"))
                c.add(Calendar.MONTH, amount);
            else if (type.equals("w"))
                c.add(Calendar.WEEK_OF_YEAR, amount);
            else if (type.equals("d"))
                c.add(Calendar.DAY_OF_YEAR, amount);
            else if (type.equals("h"))
                c.add(Calendar.HOUR, amount);
            time = c.getTime();
        }
    }

    return time;
}

From source file:org.apps8os.motivator.ui.MoodHistoryActivity.java

/**
 * Used to set the selected day and week of the sprint in the activity.
 * @param dayInMillis/*from   www.  j a v  a  2  s.  c om*/
 */
public void setSelectedDay(long dayInMillis) {
    // Calculate the selected day position in the viewpager
    mSelectedDay = (int) TimeUnit.DAYS.convert(dayInMillis - mSprintStartDateInMillis, TimeUnit.MILLISECONDS);
    Calendar selectedDayAsCalendar = Calendar.getInstance();
    selectedDayAsCalendar.setFirstDayOfWeek(Calendar.MONDAY);
    selectedDayAsCalendar.setTimeInMillis(dayInMillis);
    // Calculate the selected day position as week in the viewpager
    mSelectedWeek = selectedDayAsCalendar.get(Calendar.WEEK_OF_YEAR) - mStartDate.get(Calendar.WEEK_OF_YEAR);
    // Take into account change of year.
    if (mSelectedWeek < 0) {
        mSelectedWeek = 52 - mStartDate.get(Calendar.WEEK_OF_YEAR)
                + selectedDayAsCalendar.get(Calendar.WEEK_OF_YEAR);
    } else {
    }
    // Set the day or week depending on orientation.
    if (mRes.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        mViewPager.setCurrentItem(mSelectedDay);
    } else if (mRes.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        mViewPager.setCurrentItem(mSelectedWeek);
    }
}

From source file:com.frey.repo.DateUtil.java

/*****************************************
 * @return interger//from w w w .  ja v  a2s  .  c  o  m
 * @ ?
 ****************************************/
public static int getWeekNumOfYearDay(String strDate) throws ParseException {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date curDate = format.parse(strDate);
    int iWeekNum;
    calendar.setTime(curDate);
    if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR) - 1;
    } else {
        iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
    }
    return iWeekNum;
}