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.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 ww w  .  j a  va2  s .  c  om

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

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

/**
 * ?/*w  w  w . jav a  2  s .  c o m*/
 */
public static int getWeekOfYear(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int week_of_year = c.get(Calendar.WEEK_OF_YEAR);
    return week_of_year - 1;
}

From source file:com.alkacon.opencms.calendar.CmsCalendarDisplay.java

/**
 * Returns all displayed days of the specified week with their corresponding entries as lists.<p>
 * //from ww  w. j  a  v a2  s  .c  om
 * The key of the Map has to be a Date object.<p>
 * 
 * The Map values are always lists of {@link CmsCalendarEntry} objects, if no entries are available for a specific day,
 * an empty List is returned.<p>
 * 
 * @param year the year of the month to display
 * @param week the week of the year to display
 * @return all displayed days of the specified day range with their corresponding entries as lists
 */
public Map getEntriesForWeek(int year, int week) {

    Calendar startDay = new GregorianCalendar(getJsp().getRequestContext().getLocale());
    startDay.set(Calendar.YEAR, year);
    startDay.set(Calendar.WEEK_OF_YEAR, week);
    startDay.set(Calendar.DAY_OF_WEEK, startDay.getFirstDayOfWeek());

    Calendar endDay = (Calendar) startDay.clone();
    endDay.add(Calendar.DAY_OF_YEAR, 6);

    return getEntriesForDays(startDay, endDay);
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static Object dateAdd(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 3) {
        try {/* w  w w .  j  a  v  a2 s  .com*/
            if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                return null;
            } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                return undefinedValue;
            }
            java.util.Date dIn = (java.util.Date) ArgList[0];
            String strType = ((String) ArgList[1]).toLowerCase();
            int iValue = (Integer) ArgList[2];
            Calendar cal = Calendar.getInstance();
            cal.setTime(dIn);
            if (strType.equals("y")) {
                cal.add(Calendar.YEAR, iValue);
            } else if (strType.equals("m")) {
                cal.add(Calendar.MONTH, iValue);
            } else if (strType.equals("d")) {
                cal.add(Calendar.DATE, iValue);
            } else if (strType.equals("w")) {
                cal.add(Calendar.WEEK_OF_YEAR, iValue);
            } else if (strType.equals("wd")) {
                int iOffset = 0;
                while (iOffset < iValue) {
                    int day = cal.get(Calendar.DAY_OF_WEEK);
                    cal.add(Calendar.DATE, 1);
                    if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY)) {
                        iOffset++;
                    }
                }
            } else if (strType.equals("hh")) {
                cal.add(Calendar.HOUR, iValue);
            } else if (strType.equals("mi")) {
                cal.add(Calendar.MINUTE, iValue);
            } else if (strType.equals("ss")) {
                cal.add(Calendar.SECOND, iValue);
            }
            return cal.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call dateAdd requires 3 arguments.");
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object dateAdd(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 3) {
        try {/*ww w .  j  a v a2 s. c  o m*/
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return undefinedValue;
            java.util.Date dIn = (java.util.Date) ArgList[0];
            String strType = (String) ArgList[1];
            int iValue = (int) (Integer) ArgList[2];
            Calendar cal = Calendar.getInstance();
            cal.setTime(dIn);
            if (strType.toLowerCase().equals("y"))
                cal.add(Calendar.YEAR, iValue);
            else if (strType.toLowerCase().equals("m"))
                cal.add(Calendar.MONTH, iValue);
            else if (strType.toLowerCase().equals("d"))
                cal.add(Calendar.DATE, iValue);
            else if (strType.toLowerCase().equals("w"))
                cal.add(Calendar.WEEK_OF_YEAR, iValue);
            else if (strType.toLowerCase().equals("wd")) {
                int iOffset = 0;
                while (iOffset < iValue) {
                    int day = cal.get(Calendar.DAY_OF_WEEK);
                    cal.add(Calendar.DATE, 1);
                    if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
                        iOffset++;
                }
            } else if (strType.toLowerCase().equals("hh"))
                cal.add(Calendar.HOUR, iValue);
            else if (strType.toLowerCase().equals("mi"))
                cal.add(Calendar.MINUTE, iValue);
            else if (strType.toLowerCase().equals("ss"))
                cal.add(Calendar.SECOND, iValue);
            return cal.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call dateAdd requires 3 arguments.");
    }
}

From source file:org.exoplatform.addon.pulse.service.ws.RestActivitiesStatistic.java

private ChartData buildPlfDownloadData(String maxColumn, String filter, Date fromDate) {
    int totalDataCoulumn = 5;
    try {/*from w w w .  j a  v a  2s.  c om*/
        totalDataCoulumn = Integer.parseInt(maxColumn);
    } catch (Exception e) {
        //do nothing
    }
    if (filter.equalsIgnoreCase(FILTER_BY_DAY)) {
        List<String> listTitle = new ArrayList<String>();
        List<Long> plfDownloadsData = new ArrayList<Long>();

        for (int i = 0; i < totalDataCoulumn; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTime(fromDate);
            calendar.add(Calendar.DATE, i);
            Date toDate = calendar.getTime();
            String toDateStr = partString(toDate, "yyyy-MM-dd");

            listTitle.add(partString(toDate, "dd-MM-yyyy"));
            String url = "http://sourceforge.net/projects/exo/files/stats/json?start_date=" + toDateStr
                    + "&end_date=" + toDateStr;
            try {
                String returnValue = httpGet(url);
                plfDownloadsData.add(getTotalPlfDownloadDataFromJson(returnValue, toDate));
            } catch (Exception e) {
                LOG.error(e.getMessage());
                plfDownloadsData.add(0l);
            }

        }
        ChartData chartData = new ChartData();
        chartData.setListTitle(listTitle);
        chartData.setPlfDownloadsData(plfDownloadsData);
        return chartData;
    }

    if (filter.equalsIgnoreCase(FILTER_BY_WEEK)) {
        List<String> listTitle = new ArrayList<String>();
        List<Long> plfDownloadsData = new ArrayList<Long>();

        String fromDateStr = partString(fromDate, "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(fromDate);
        int fromWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        int fromMonth = calendar.get(Calendar.MONTH);
        if (fromMonth == Calendar.DECEMBER && fromWeek == 1)
            fromWeek = 53;
        int fromYear = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.WEEK_OF_YEAR, fromWeek);
        calendar.set(Calendar.YEAR, fromYear);
        Date beginWeekDate = calendar.getTime();
        calendar.clear();
        calendar.setTime(beginWeekDate);
        calendar.add(Calendar.DATE, 6);
        Date endWeekDate = calendar.getTime();
        calendar.clear();
        String toDateStr = partString(endWeekDate, "yyyy-MM-dd");
        for (int i = 0; i < totalDataCoulumn; i++) {
            calendar.setTime(endWeekDate);
            listTitle.add("W" + calendar.get(Calendar.WEEK_OF_YEAR) + "-" + calendar.get(Calendar.YEAR));
            String url = "http://sourceforge.net/projects/exo/files/stats/json?start_date=" + fromDateStr
                    + "&end_date=" + toDateStr;
            try {
                String returnValue = httpGet(url);
                plfDownloadsData.add(
                        getTotalPlfDownloadDataFromJson(returnValue, parseDate(fromDateStr, "yyyy-MM-dd")));
            } catch (Exception e) {
                LOG.error(e.getMessage());
                plfDownloadsData.add(0l);
            }

            calendar.clear();

            fromDate = endWeekDate;
            calendar.setTime(fromDate);
            calendar.add(Calendar.DATE, 1);
            fromDate = calendar.getTime();
            fromDateStr = partString(fromDate, "yyyy-MM-dd");

            calendar.add(Calendar.DATE, 6);
            endWeekDate = calendar.getTime();
            toDateStr = partString(endWeekDate, "yyyy-MM-dd");
            calendar.clear();
        }
        ChartData chartData = new ChartData();
        chartData.setListTitle(listTitle);
        chartData.setPlfDownloadsData(plfDownloadsData);
        return chartData;
    }

    if (filter.equalsIgnoreCase(FILTER_BY_MONTH)) {
        List<String> listTitle = new ArrayList<String>();
        List<Long> plfDownloadsData = new ArrayList<Long>();

        String fromDateStr = partString(fromDate, "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(fromDate);

        int fromMonth = calendar.get(Calendar.MONTH);
        int fromYear = calendar.get(Calendar.YEAR);
        //calendar.add(Calendar.MONTH, totalDataCoulumn);
        //int toYear = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.MONTH, fromMonth);
        calendar.set(Calendar.YEAR, fromYear);
        Date beginMonthDate = calendar.getTime();
        calendar.clear();
        calendar.setTime(beginMonthDate);
        int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, dayOfMonth - 1);
        Date endMonthDate = calendar.getTime();
        calendar.clear();
        String toDateStr = partString(endMonthDate, "yyyy-MM-dd");
        for (int i = 0; i < totalDataCoulumn; i++) {
            calendar.setTime(endMonthDate);
            listTitle.add(partString(calendar.getTime(), "MMM") + "-" + calendar.get(Calendar.YEAR));
            String url = "http://sourceforge.net/projects/exo/files/stats/json?start_date=" + fromDateStr
                    + "&end_date=" + toDateStr;
            try {
                String returnValue = httpGet(url);
                plfDownloadsData.add(
                        getTotalPlfDownloadDataFromJson(returnValue, parseDate(fromDateStr, "yyyy-MM-dd")));
            } catch (Exception e) {
                plfDownloadsData.add(0l);
            }

            calendar.clear();

            fromDate = endMonthDate;
            calendar.setTime(fromDate);
            calendar.add(Calendar.DATE, 1);
            fromDate = calendar.getTime();
            fromDateStr = partString(fromDate, "yyyy-MM-dd");

            dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            calendar.add(Calendar.DATE, dayOfMonth - 1);
            endMonthDate = calendar.getTime();
            toDateStr = partString(endMonthDate, "yyyy-MM-dd");
            calendar.clear();
        }
        ChartData chartData = new ChartData();
        chartData.setListTitle(listTitle);
        chartData.setPlfDownloadsData(plfDownloadsData);
        return chartData;
    }

    return null;
}

From source file:net.chaosserver.timelord.data.ExcelDataReaderWriter.java

/**
 * Converts a given date to the date that marks the start of a week.
 * This is 12:00am on Monday./*from   ww  w .ja v  a 2s.c  o m*/
 *
 * @param inputDate the date to find the start of week for
 * @return 12:00am on Monday the week of the input
 */
protected Date convertToWeekStart(Date inputDate) {
    Calendar weekStartCalendar = Calendar.getInstance();
    weekStartCalendar.setTime(inputDate);
    weekStartCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    weekStartCalendar.set(Calendar.HOUR_OF_DAY, 0);
    weekStartCalendar.set(Calendar.MINUTE, 0);
    weekStartCalendar.set(Calendar.SECOND, 0);
    weekStartCalendar.set(Calendar.MILLISECOND, 0);

    // Since the day of week has changed from Monday it no
    // longer matches Java's natural interpretation.  So if
    // the date is a Monday, than it's important to roll back
    // the week start by a week
    Calendar inputCalendar = Calendar.getInstance();
    inputCalendar.setTime(inputDate);
    if (inputCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        weekStartCalendar.add(Calendar.WEEK_OF_YEAR, -1);
    }

    return weekStartCalendar.getTime();
}

From source file:DateUtils.java

/**
 * Get expiration timestamp from start date, period type and duration. For 
 * example if the start date is now, period type is hour and duration is 2
 * then the result will be timestamp representing now + 2 hours.  
 * // w w  w.j a va  2s  .c o  m
 * @param tsStartDate - start date of period counting
 * @param iPeriodType - one of the period type constant TIMING_XXX 
 * @param iPeriodDuration - period duration, number of time units specified 
 *                          by period type
 * @return Timestamp - date of period expiration or null if any problem
 */
public static Timestamp getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration) {
    Timestamp tsReturn = null;
    Calendar calHelp;
    if (tsStartDate != null && iPeriodDuration > 0 && iPeriodType > TIMING_NEVER && iPeriodType < TIMING_NONE) {
        calHelp = Calendar.getInstance();
        calHelp.setTime(tsStartDate);

        switch (iPeriodType) {
        case (TIMING_MINUTES): {
            calHelp.add(Calendar.MINUTE, iPeriodDuration);
            break;
        }
        case (TIMING_HOURS): {
            calHelp.add(Calendar.HOUR, iPeriodDuration);
            break;
        }
        case (TIMING_DAYS): {
            calHelp.add(Calendar.DATE, iPeriodDuration);
            break;
        }
        case (TIMING_WEEKS): {
            calHelp.add(Calendar.WEEK_OF_YEAR, iPeriodDuration);
            break;
        }
        case (TIMING_MONTHS): {
            calHelp.add(Calendar.MONTH, iPeriodDuration);
            break;
        }
        case (TIMING_YEARS): {
            calHelp.add(Calendar.YEAR, iPeriodDuration);
            break;
        }
        default: {
            assert false : "Not supported Timing type " + iPeriodType;
        }
        }
        tsReturn = new Timestamp(calHelp.getTimeInMillis());
    }

    return tsReturn;
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static Object dateAdd(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    if (ArgList.length == 3) {
        try {/*from   w w w.j av a 2 s.  co  m*/
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return Context.getUndefinedValue();
            java.util.Date dIn = (java.util.Date) Context.jsToJava(ArgList[0], java.util.Date.class);
            String strType = Context.toString(ArgList[1]);
            int iValue = (int) Context.toNumber(ArgList[2]);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dIn);
            if (strType.toLowerCase().equals("y"))
                cal.add(Calendar.YEAR, iValue);
            else if (strType.toLowerCase().equals("m"))
                cal.add(Calendar.MONTH, iValue);
            else if (strType.toLowerCase().equals("d"))
                cal.add(Calendar.DATE, iValue);
            else if (strType.toLowerCase().equals("w"))
                cal.add(Calendar.WEEK_OF_YEAR, iValue);
            else if (strType.toLowerCase().equals("wd")) {
                int iOffset = 0;
                while (iOffset < iValue) {
                    int day = cal.get(Calendar.DAY_OF_WEEK);
                    cal.add(Calendar.DATE, 1);
                    if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
                        iOffset++;
                }
            } else if (strType.toLowerCase().equals("hh"))
                cal.add(Calendar.HOUR, iValue);
            else if (strType.toLowerCase().equals("mi"))
                cal.add(Calendar.MINUTE, iValue);
            else if (strType.toLowerCase().equals("ss"))
                cal.add(Calendar.SECOND, iValue);
            return cal.getTime();
        } catch (Exception e) {
            throw Context.reportRuntimeError(e.toString());
        }
    } else {
        throw Context.reportRuntimeError("The function call dateAdd requires 3 arguments.");
    }
}

From source file:com.aurel.track.report.dashboard.AverageTimeToCloseItem.java

private String getDateBasedOnReportInterval(Date date, int reportingInterval) {
    String formattedDate = null;/* www . j  ava  2 s .c  o m*/
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week = cal.get(Calendar.WEEK_OF_YEAR);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    if (reportingInterval == REPORTING_INTERVAL.DAILY) {
        SimpleDateFormat dtf = new SimpleDateFormat(ISO_FORMATTED_DATE);
        return dtf.format(date).toString();
    } else if (reportingInterval == REPORTING_INTERVAL.MONTHLY) {
        return year + "-" + month;
    } else if (reportingInterval == REPORTING_INTERVAL.WEEKLY) {
        return week + "/" + year;
    }
    return formattedDate;
}