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.fsoft.bn.service.impl.ScheduleLocalServiceImpl.java

/**
 * Build weekly schedule//  w ww .  j  a v a 2s .  c  om
 * @param startDate
 * @param endDate
 * @param year
 * @param locale
 * @param month
 * @return
 */
private List<ScheduleModel> buildWeekSchedule(String startDate, String endDate, int year, Locale locale,
        int month) {
    List<ScheduleModel> retVal = new ArrayList<ScheduleModel>();
    List<ScheduleModel> weekList = new ArrayList<ScheduleModel>();
    ScheduleModel schedule;

    Date weekStartDate = DateUtils.getStartDateOfWeek(startDate, CommonConstants.DATE_FORMET_EN);
    Date weekEndDate = DateUtils.getStartDateOfWeek(endDate, CommonConstants.DATE_FORMET_EN);

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

    while (true) {

        if (cal.getTime().after(weekEndDate)) {
            break;
        }

        schedule = new ScheduleModel();
        schedule.setWeekOfYear(cal.get(Calendar.WEEK_OF_YEAR));
        schedule.setStartDate(DateUtils.date2String(cal.getTime(),
                LanguageUtil.get(locale, CommonConstants.scheduleTitleDateFormat)));
        // User name column header
        schedule.getWeekScheduleHeader().add(StringPool.BLANK);
        // Monday column header
        schedule.getWeekScheduleHeader()
                .add(LanguageUtil.get(locale, CommonConstants.scheduleListHeaderKeys[1])
                        + CommonConstants.HTML_NEW_LINE + DateUtils.date2String(cal.getTime(),
                                LanguageUtil.get(locale, CommonConstants.scheduleHeaderDateFormat)));
        // Schedule for: TUE, WED, THU, FRI
        for (int i = 0; i < CommonConstants.NUM_4; i++) {
            cal.add(Calendar.DATE, CommonConstants.NUM_1);
            schedule.getWeekScheduleHeader()
                    .add(LanguageUtil.get(locale, CommonConstants.scheduleListHeaderKeys[i + 2])
                            + CommonConstants.HTML_NEW_LINE + DateUtils.date2String(cal.getTime(),
                                    LanguageUtil.get(locale, CommonConstants.scheduleHeaderDateFormat)));
        }
        schedule.setEndDate(DateUtils.date2String(cal.getTime(),
                LanguageUtil.get(locale, CommonConstants.scheduleTitleDateFormat)));
        schedule.setSchedule(new ArrayList<WeekScheduleModel>());
        // Set weekly schedule title
        StringBuffer title = new StringBuffer();
        title.append(LanguageUtil.get(locale, "portlet.schedule.weektitle") + CommonConstants.SPACE);
        title.append(schedule.getWeekOfYear());
        title.append(LanguageUtil.get(locale, "portlet.schedule.weeksuffix") + CommonConstants.SPACE);
        title.append("(" + LanguageUtil.get(locale, "portlet.schedule.weekstart") + CommonConstants.SPACE);
        title.append(schedule.getStartDate() + " - ");
        title.append(schedule.getEndDate() + "):");
        schedule.setWeekScheduleTitle(title.toString());

        weekList.add(schedule);
        //Add more for SAT, SUN
        cal.add(Calendar.DATE, CommonConstants.NUM_3);
    }

    Calendar startCal = Calendar.getInstance();
    Calendar endCal = Calendar.getInstance();
    Date startDateOfWeek = new Date();
    Date endDateOfWeek = new Date();

    startCal.setTime(weekStartDate);
    endCal.setTime(weekEndDate);
    // Sort EST and remove week that does not in month
    for (int i = weekList.size(); i > 0; i--) {
        startDateOfWeek = DateUtils.string2Date(weekList.get(i - 1).getStartDate(),
                LanguageUtil.get(locale, CommonConstants.scheduleTitleDateFormat), locale);
        endDateOfWeek = DateUtils.string2Date(weekList.get(i - 1).getEndDate(),
                LanguageUtil.get(locale, CommonConstants.scheduleTitleDateFormat), locale);
        startCal.setTime(startDateOfWeek);
        endCal.setTime(endDateOfWeek);
        if (startCal.get(Calendar.MONTH) != month && endCal.get(Calendar.MONTH) != month) {
            continue;
        }
        retVal.add(weekList.get(i - 1));
    }

    return retVal;
}

From source file:TimeSpan.java

/**
 * Adds the duration of this time span to the provided {@code Calendar}
 * instance, moving it forward in time./*  www .ja va 2 s  .co  m*/
 *
 * @param c the calendar whose date will be moved forward by the duration of
 *        this time span
 */
public void addTo(Calendar c) {
    c.add(Calendar.YEAR, years);
    c.add(Calendar.MONTH, months);
    c.add(Calendar.WEEK_OF_YEAR, weeks);
    c.add(Calendar.DAY_OF_YEAR, days);
    c.add(Calendar.HOUR_OF_DAY, hours);
}

From source file:edu.lternet.pasta.portal.statistics.GrowthStats.java

private String getLabel(int scale, GregorianCalendar date) {

    String label = null;/* w w  w .  ja va 2  s.com*/

    SimpleDateFormat formatter;

    switch (scale) {
    case Calendar.HOUR:
        formatter = new SimpleDateFormat("kk00 yyyy-MMM-d");
        label = formatter.format(date.getTime());
        break;
    case Calendar.DAY_OF_MONTH:
        formatter = new SimpleDateFormat("yyyy-MMM-d");
        label = formatter.format(date.getTime());
        break;
    case Calendar.WEEK_OF_YEAR:
        formatter = new SimpleDateFormat("w yyyy");
        label = formatter.format(date.getTime());
        break;
    case Calendar.MONTH:
        formatter = new SimpleDateFormat("MMM yyyy");
        label = formatter.format(date.getTime());
        break;
    case Calendar.YEAR:
        formatter = new SimpleDateFormat("yyyy");
        label = formatter.format(date.getTime());
        break;
    default:
        label = date.getTime().toString();
    }

    return label;
}

From source file:com.frameworkset.platform.sanylog.action.CounterController.java

/**???
*///from   w  w w.  j a  v a2 s  .co  m
public String showOperCounterRankByWeek(
        @PagerParam(name = PagerParam.SORT, defaultvalue = "vcount") String sortKey,
        @PagerParam(name = PagerParam.DESC, defaultvalue = "false") boolean desc,
        @PagerParam(name = PagerParam.OFFSET) long offset,
        @PagerParam(name = PagerParam.PAGE_SIZE, defaultvalue = "10") int pagesize, String appId, String vtime,
        ModelMap model) throws Exception {
    if (null == vtime || "".equals(vtime)) {
        Calendar calendar = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        String todayTime = DateUtils.format(today.getTime(), DateUtils.ISO8601_DATE_PATTERN);
        String year = todayTime.substring(0, 4);
        int weeks = calendar.get(Calendar.WEEK_OF_YEAR);
        vtime = year + "-" + String.valueOf(weeks);
    }
    /*else{
       String year = vtime.substring(0, 4);
       String week = vtime.substring(5,7);
       vtime = year+"-"+week;
               
    }*/
    ListInfo datas = counterManager.getOperCounterRankByWeek(appId, vtime, (int) offset, pagesize);
    model.addAttribute("datas", datas);
    return "path:operCounterRankData";
}

From source file:org.mule.el.context.ServerContextTestCase.java

@Test
public void dateTimeAddWeeks() {
    Assert.assertEquals((Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) + 1) % 52,
            evaluate("(int) server.dateTime.plusWeeks(1).weekOfYear"));
}

From source file:org.infoglue.calendar.actions.ViewEventSearchAction.java

public String doExternalBindingSearch() throws Exception {
    setExportResult(false);/*from  ww  w.  j ava2s.  com*/
    setSortAscending(false);
    setStateId(Event.STATE_PUBLISHED);
    initialize();

    List<Long> eventIds = getEventIds();
    if (startDateTime == null && eventIds != null) {
        currentExternalEvents = EventController.getController().getEventList(eventIds, getSession());
    }

    if (startDateTime != null) {
        execute();
        if (this.events != null) {
            log.info("Filtering event results. We only want one post even if there are more versions...");
            // Remove duplicates
            HashSet<Long> currentIds = new HashSet<Long>();
            @SuppressWarnings("unchecked")
            Iterator<Event> eventIterator = this.events.iterator();
            while (eventIterator.hasNext()) {
                Event event = (Event) eventIterator.next();
                if (!currentIds.add(event.getId())) // if the id already exists
                {
                    log.debug("Found duplicate event: " + event.getId());
                    eventIterator.remove();
                }
            }
        }
    } else {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        startDateTime = dateFormatter.format(calendar.getTime());
        calendar.roll(Calendar.WEEK_OF_YEAR, 1);
        endDateTime = dateFormatter.format(calendar.getTime());

        startTime = "00:00";
        endTime = "23:59";
    }
    return "successExternalBinding";
}

From source file:com.gargoylesoftware.htmlunit.ExternalTest.java

/**
 * Returns if now we are in different week than the last finished build one.
 *//*from ww w  .  j  a v a 2s  . co m*/
private static boolean isDifferentWeek() throws Exception {
    try (final WebClient webClient = getWebClient()) {
        HtmlPage page = webClient.getPage("https://ci.canoo.com/teamcity/viewLog.html"
                + "?buildTypeId=HtmlUnit_FastBuild&buildId=lastSuccessful");
        page = page.getAnchorByText("Log in as guest").click();
        webClient.waitForBackgroundJavaScript(1000);
        final HtmlTable table = page.getFirstByXPath("//table[@class='statusTable']");
        assumeNotNull(page.asXml(), table);
        final HtmlTableCell cell = table.getRow(1).getCell(3);
        final String triggerText = cell.asText();

        final String marker = " on ";
        final int start = triggerText.indexOf(marker);
        final String triggerDate = triggerText.substring(start + marker.length());

        final Calendar buildCalendar = Calendar.getInstance(Locale.ROOT);
        buildCalendar.setTime(TEAM_CITY_FORMAT_.parse(triggerDate));
        return Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) != buildCalendar.get(Calendar.WEEK_OF_YEAR);
    }
}

From source file:net.groupbuy.service.impl.ProductServiceImpl.java

/**
 * //from  w w  w . java  2 s .  c  o  m
 */
@SuppressWarnings("unchecked")
private void updateHits() {
    Ehcache cache = cacheManager.getEhcache(Product.HITS_CACHE_NAME);
    List<Long> ids = cache.getKeys();
    for (Long id : ids) {
        Product product = productDao.find(id);
        if (product != null) {
            productDao.lock(product, LockModeType.PESSIMISTIC_WRITE);
            Element element = cache.get(id);
            long hits = (Long) element.getObjectValue();
            long increment = hits - product.getHits();
            Calendar nowCalendar = Calendar.getInstance();
            Calendar weekHitsCalendar = DateUtils.toCalendar(product.getWeekHitsDate());
            Calendar monthHitsCalendar = DateUtils.toCalendar(product.getMonthHitsDate());
            if (nowCalendar.get(Calendar.YEAR) != weekHitsCalendar.get(Calendar.YEAR)
                    || nowCalendar.get(Calendar.WEEK_OF_YEAR) > weekHitsCalendar.get(Calendar.WEEK_OF_YEAR)) {
                product.setWeekHits(increment);
            } else {
                product.setWeekHits(product.getWeekHits() + increment);
            }
            if (nowCalendar.get(Calendar.YEAR) != monthHitsCalendar.get(Calendar.YEAR)
                    || nowCalendar.get(Calendar.MONTH) > monthHitsCalendar.get(Calendar.MONTH)) {
                product.setMonthHits(increment);
            } else {
                product.setMonthHits(product.getMonthHits() + increment);
            }
            product.setHits(hits);
            product.setWeekHitsDate(new Date());
            product.setMonthHitsDate(new Date());
            productDao.merge(product);
        }
    }
}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static int getWeekNumberOfDate(Date date) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date);//ww  w .  j  a  v a2 s  .co m

    return cal1.get(Calendar.WEEK_OF_YEAR);
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public TreeMap<String, SynchronizedDescriptiveStatistics> getWeeklySummaryStats(Date start, Date end,
        Long hospitalser, boolean imrtOnly, boolean includeWeekends) {
    Calendar cal = new GregorianCalendar();
    TreeMap<String, SynchronizedDescriptiveStatistics> retval = new TreeMap<>();

    List<Object[]> events;

    if (hospitalser < 0) {
        events = getDailyCounts(start, end, imrtOnly, includeWeekends);
    } else {// w w w.  j a v  a  2s  .c  o m
        events = getDailyCounts(start, end, hospitalser, imrtOnly, includeWeekends);
    }
    cal.setTime(start);
    int wk = cal.get(Calendar.WEEK_OF_YEAR);
    int mo = cal.get(Calendar.MONTH);
    int yr = cal.get(Calendar.YEAR);
    if (mo == Calendar.DECEMBER && wk == 1) {
        yr = yr + 1;
    } else if (mo == Calendar.JANUARY && wk == 52) {
        yr = yr - 1;
    }
    String currYrWk = yr + "-" + String.format("%02d", wk);
    String prevYrWk = "";
    SynchronizedDescriptiveStatistics currStats = new SynchronizedDescriptiveStatistics();
    int i = 0;
    while (cal.getTime().before(end) && i < events.size()) {

        Object[] event = events.get(i);
        Date d = (Date) event[0];
        Long count = (Long) event[1];

        prevYrWk = currYrWk;
        cal.setTime(d);
        wk = cal.get(Calendar.WEEK_OF_YEAR);
        mo = cal.get(Calendar.MONTH);
        yr = cal.get(Calendar.YEAR);
        if (mo == Calendar.DECEMBER && wk == 1) {
            yr = yr + 1;
        } else if (mo == Calendar.JANUARY && wk == 52) {
            yr = yr - 1;
        }
        currYrWk = yr + "-" + String.format("%02d", wk);

        if (!(prevYrWk.equals(currYrWk))) {
            retval.put(prevYrWk, currStats);
            currStats = new SynchronizedDescriptiveStatistics();
        }

        currStats.addValue(count);
        i++;
    }
    retval.put(prevYrWk, currStats);

    return retval;
}