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:org.exoplatform.services.jcr.ext.classify.impl.DateTimeClassifyPlugin.java

/**
 * Gets the date time structured.//from   ww w.j  ava2 s  .  c  o  m
 *
 * @param templDateTime the templ date time
 * @param calendar the calendar
 *
 * @return the date time structured
 */
private String getDateTimeStructured(String templDateTime, Calendar calendar) {
    int year = calendar.get(Calendar.YEAR), month = calendar.get(Calendar.MONTH) + 1,
            woy = calendar.get(Calendar.WEEK_OF_YEAR), wom = calendar.get(Calendar.WEEK_OF_MONTH),
            dom = calendar.get(Calendar.DAY_OF_MONTH), dow = calendar.get(Calendar.DAY_OF_WEEK),
            startYear = startDateTime.get(Calendar.YEAR), startMonth = startDateTime.get(Calendar.MONTH);
    switch (incrementType) {
    case 'Y':
        int n = (year - startYear) / (increment + 1);
        year = n * (increment + 1) + startYear;
        break;
    case 'M':
        if (year != startYear)
            startMonth = 1;
        int m = (month - startMonth) / (increment + 1);
        month = m * (increment + 1) + startMonth;
        break;
    default:
        break;
    }
    templDateTime = templDateTime.replace("YYYY", Integer.toString(year)).replace("MM", Integer.toString(month))
            .replace("WW", Integer.toString(woy)).replace("ww", Integer.toString(wom))
            .replace("DD", Integer.toString(dom)).replace("dd", Integer.toString(dow));
    String expr = templDateTime.substring(templDateTime.indexOf("{") + 1, templDateTime.indexOf("}"));
    templDateTime = templDateTime.replace(expr, operateExpression(expr)).replace("#", "").replace("{", "")
            .replace("}", "");
    return templDateTime;
}

From source file:com.glaf.core.util.CalendarUtils.java

/**
 * ??//from  ww w.  j av a  2 s .  c  o  m
 * 
 * @param date
 * @param productionLine
 * @return
 */
public static List<SysCalendar> getNextWeekCalendars(Date date, String productionLine) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR) + 1);
    SysCalendarQuery query = new SysCalendarQuery();
    query.setWeek(cal.get(Calendar.WEEK_OF_YEAR));
    query.setProductionLine(productionLine);
    query.setIsFreeDay(0);// 01?
    query.setWorkDateGreaterThanOrEqual(date);
    query.setOrderBy(" E.WORKDATE_ ");
    return getSysCalendarService().list(query);
}

From source file:de.fhbingen.wbs.wpOverview.tabs.AvailabilityGraph.java

/**
 * Decrease the actual chosen view. (DAY / MONTH / WEEK/ YEAR)
 *///from w  ww.  ja  v a  2s .com
protected final void decrement() {
    switch (actualView) {
    case DAY:
        actualDay.add(Calendar.DAY_OF_YEAR, -1);
        break;
    case WEEK:
        actualDay.add(Calendar.WEEK_OF_YEAR, -1);
        break;
    case MONTH:
        actualDay.add(Calendar.MONTH, -1);
        break;
    case YEAR:
        actualDay.add(Calendar.YEAR, -1);
        break;
    default:
        break;
    }
    changeView(actualView);
}

From source file:org.mot.common.util.DateBuilder.java

/**
 * @param weekNumber - which week to look for
 * @param pattern - define a pattern in which the result string will be formatted
 * @return String value representing the first monday of the week
 *//*from  www.  j a v a 2s  . c om*/
public String getDateForWeekOfYear(int weekNumber, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.WEEK_OF_YEAR, weekNumber);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    //System.out.println(sdf.format(cal.getTime())); 
    return sdf.format(cal.getTime());
}

From source file:uk.ac.ox.oucs.vle.SampleDataLoader.java

public static Date addWeeks(Calendar cal, int i) {
    Calendar opens = (Calendar) cal.clone();
    opens.add(Calendar.WEEK_OF_YEAR, i);
    return opens.getTime();
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.group.BookingRoomController.java

@Override
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object comm,
        BindException bindException) throws Exception {
    try {/*from   w  w  w.  j a va2s  . c o  m*/
        BookRoomCommand command = (BookRoomCommand) comm;
        status = messageSource.getMessage("bookRoom.controllerMessages.status.fail", null,
                RequestContextUtils.getLocale(request));
        comment = messageSource.getMessage("bookRoom.controllerMessages.comment.error.unknown", null,
                RequestContextUtils.getLocale(request));

        Person user = personDao.getLoggedPerson();
        int group = command.getSelectedGroup();
        int repType = command.getRepType();
        int repCount = command.getRepCount();
        String startStr = BookingRoomUtils.getHoursAndMinutes(command.getStartTimeCal());
        String endStr = BookingRoomUtils.getHoursAndMinutes(command.getEndTimeCal());

        Reservation res = new Reservation();

        Timestamp createTime = new Timestamp(new GregorianCalendar().getTimeInMillis());
        res.setCreationTime(createTime);
        res.setStartTime(command.getStartTimeTimestamp());
        res.setEndTime(command.getEndTimeTimestamp());

        res.setPerson(user);

        //searching for ResearchGroup
        ResearchGroup grp = getResearchGroup(group);
        res.setResearchGroup(grp);

        log.debug("Reservation has been created: " + ((res == null) ? "false" : "true"));
        reservationDao.create(res);

        if (repCount > 0) {
            comment = messageSource.getMessage("bookRoom.controllerMessages.comment.booked.multiple.part1",
                    null, RequestContextUtils.getLocale(request));
            comment += command.getDate() + ", from " + startStr + " to " + endStr + "<br>\n";

            GregorianCalendar nextS = command.getStartTimeCal();
            GregorianCalendar nextE = command.getEndTimeCal();

            for (int i = 0; i < repCount; i++) {
                //shift of dates
                int add = BookingRoomUtils.getWeeksAddCount(repType, i);
                nextS.add(Calendar.WEEK_OF_YEAR, add);
                nextE.add(Calendar.WEEK_OF_YEAR, add);
                Reservation newReservation = new Reservation();
                newReservation.setCreationTime(createTime);
                newReservation.setStartTime(new Timestamp(nextS.getTimeInMillis()));
                newReservation.setEndTime(new Timestamp(nextE.getTimeInMillis()));
                newReservation.setPerson(user);
                newReservation.setResearchGroup(grp);
                reservationDao.create(newReservation);

                comment += BookingRoomUtils.getDate(nextS) + ", from "
                        + BookingRoomUtils.getHoursAndMinutes(nextS) + " to "
                        + BookingRoomUtils.getHoursAndMinutes(nextE) + "<br>\n";
            }

            comment += String.format(
                    messageSource.getMessage("bookRoom.controllerMessages.comment.booked.multiple.part2", null,
                            RequestContextUtils.getLocale(request)),
                    repCount + 1);//+1 because we need count "original" reservation!
        } else {
            comment = String
                    .format(messageSource.getMessage("bookRoom.controllerMessages.comment.booked.single", null,
                            RequestContextUtils.getLocale(request)), command.getDate(), startStr, endStr);
        }

        status = messageSource.getMessage("bookRoom.controllerMessages.status.ok", null,
                RequestContextUtils.getLocale(request));
    } catch (Exception e) {
        log.error("Exception: " + e.getMessage() + "\n" + e.getStackTrace()[0].getFileName() + " at line "
                + e.getStackTrace()[0].getLineNumber(), e);

        status = messageSource.getMessage("bookRoom.controllerMessages.status.fail", null,
                RequestContextUtils.getLocale(request));
        comment = messageSource.getMessage("bookRoom.controllerMessages.comment.error.exception", null,
                RequestContextUtils.getLocale(request)) + " " + e.getMessage();
    }

    log.debug("Returning MAV" + " with status=" + status + "&comment=" + comment);
    ModelAndView mav = new ModelAndView(getSuccessView()/* + "?status=" + status + "&comment=" + comment*/);

    return mav;
}

From source file:com.glaf.core.web.springmvc.MxSystemCalendarController.java

@ResponseBody
@RequestMapping("/save")
public byte[] save(HttpServletRequest request) {
    User user = RequestUtils.getUser(request);
    String actorId = user.getActorId();
    int year = RequestUtils.getInt(request, "year");
    int month = RequestUtils.getInt(request, "month");
    int groupA = RequestUtils.getInt(request, "groupA");
    int groupB = RequestUtils.getInt(request, "groupB");
    int days = DateUtils.getYearMonthDays(year, month);
    String productionLine = request.getParameter("productionLine");
    Calendar cal = Calendar.getInstance();
    try {/*  w ww. j a  v  a 2s. com*/
        for (int i = 1; i <= days; i++) {
            String dayStr = "checkDay_" + i;
            int checkValue = RequestUtils.getInt(request, dayStr);

            SysCalendar sysCalendar = this.sysCalendarService.getSysCalendar(productionLine, year, month, i);
            boolean isExist = true;
            if (null == sysCalendar) {
                sysCalendar = new SysCalendar();
                isExist = false;
            }
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
            cal.set(Calendar.DATE, i);
            Date workDate = cal.getTime();
            sysCalendar.setWorkDate(workDate);
            sysCalendar.setYear(year);
            sysCalendar.setMonth(month);
            sysCalendar.setDay(i);
            sysCalendar.setWeek(cal.get(Calendar.WEEK_OF_YEAR));

            if (checkValue == 1) {
                if (!isExist) {
                    sysCalendar.setIsFreeDay(1);
                }
                if (-1 != groupA) {
                    sysCalendar.setIsFreeDay(0);
                    sysCalendar.setGroupA("A");
                    sysCalendar.setDutyA(groupA + "");
                }
                if (-1 != groupB) {
                    sysCalendar.setIsFreeDay(0);
                    sysCalendar.setGroupB("B");
                    sysCalendar.setDutyB(groupB + "");
                }
                if (groupA == 0 && groupB == 0) {
                    sysCalendar.setIsFreeDay(1);
                }
            } else {
                if (!isExist) {
                    sysCalendar.setIsFreeDay(1);
                }
            }

            sysCalendar.setProductionLine(productionLine);
            sysCalendar.setCreateBy(actorId);
            sysCalendar.setCreateDate(new Date());

            if (checkValue == 1 || !isExist) {
                this.sysCalendarService.save(sysCalendar);
            }
        }
        return ResponseUtils.responseJsonResult(true);
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.error(ex);
    }
    return ResponseUtils.responseJsonResult(true);
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Formats a date for display in the application.
 *
 * @param applicationContext The application context.
 * @param date               The date to format.
 * @param hideTime           Omits the time in the formatted date if true.
 * @return the formatted date.//from w  w  w  .  ja  v  a  2 s  . co m
 */
public static String getFormattedDate(Context applicationContext, Date date, boolean hideTime) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    Calendar oneMinuteAgo = Calendar.getInstance();
    oneMinuteAgo.add(Calendar.MINUTE, -1);
    if (oneMinuteAgo.getTime().before(date)) {
        // Last minute: X seconds ago
        long seconds = (Calendar.getInstance().getTime().getTime() - calendar.getTime().getTime()) / 1000;
        if (seconds < 10) {
            return applicationContext.getString(R.string.utils_date_just_now);
        } else {
            return seconds + " " + applicationContext.getString(R.string.utils_date_seconds_ago);
        }
    }

    Calendar oneHourAgo = Calendar.getInstance();
    oneHourAgo.add(Calendar.HOUR_OF_DAY, -1);
    if (oneHourAgo.getTime().before(date)) {
        // Last hour: X minutes ago
        long minutes = (Calendar.getInstance().getTime().getTime() - calendar.getTime().getTime())
                / (1000 * 60);
        if (minutes == 1) {
            return applicationContext.getString(R.string.utils_date_one_minute_ago);
        } else {
            return minutes + " " + applicationContext.getString(R.string.utils_date_minutes_ago);
        }
    }

    if (compareDateWithoutTime(Calendar.getInstance(), calendar) == 0) {
        // Today: h:mm a
        DateFormat format = new SimpleDateFormat("h:mm a", Locale.getDefault());
        return format.format(date);
    }

    if (Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) == calendar.get(Calendar.WEEK_OF_YEAR)
            && Calendar.getInstance().get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
        if (hideTime) {
            // This week: EEE
            DateFormat format = new SimpleDateFormat("EEE", Locale.getDefault());
            return format.format(date);
        } else {
            // This week: EEE h:mm a
            DateFormat format = new SimpleDateFormat("EEE h:mm a", Locale.getDefault());
            return format.format(date);
        }
    }

    if (Calendar.getInstance().get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {
        if (hideTime) {
            // This year: MMM d
            DateFormat format = new SimpleDateFormat("MMM d", Locale.getDefault());
            return format.format(date);
        } else {
            // This year: MMM d h:mm a
            DateFormat format = new SimpleDateFormat("MMM d, h:mm a", Locale.getDefault());
            return format.format(date);
        }
    }

    if (hideTime) {
        // Any: MMM d, yyyy
        DateFormat format = new SimpleDateFormat("MMM d, yyyy", Locale.getDefault());
        return format.format(date);
    } else {
        // Any: MMM d, yyyy h:mm a
        DateFormat format = new SimpleDateFormat("MMM d, yyyy, h:mm a", Locale.getDefault());
        return format.format(date);
    }
}

From source file:org.openmrs.module.registration.web.controller.ajax.RegistrationAjaxController.java

private String getEstimatedBirthdate(String text) throws Exception {
    text = text.toLowerCase();/*from   w  ww  .java 2 s .  c  o m*/
    String ageStr = text.substring(0, text.length() - 1);
    String type = text.substring(text.length() - 1);
    int age = Integer.parseInt(ageStr);
    if (age < 0) {
        throw new Exception("Age must not be negative number!");
    }
    Calendar date = Calendar.getInstance();
    if (type.equalsIgnoreCase("y")) {
        date.add(Calendar.YEAR, -age);
    } else if (type.equalsIgnoreCase("m")) {
        date.add(Calendar.MONTH, -age);
    } else if (type.equalsIgnoreCase("w")) {
        date.add(Calendar.WEEK_OF_YEAR, -age);
    } else if (type.equalsIgnoreCase("d")) {
        date.add(Calendar.DATE, -age);
    }
    return RegistrationUtils.formatDate(date.getTime());
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.models.spinner.SpinnerModelPropertyEditor.java

/**
 * @return the the name of step from {@link Calendar} fields.
 *//*from  w ww .  j a  va  2 s  . com*/
private static String getDateStep(int calendarField) {
    switch (calendarField) {
    case Calendar.ERA:
        return "ERA";
    case Calendar.YEAR:
        return "YEAR";
    case Calendar.MONTH:
        return "MONTH";
    case Calendar.WEEK_OF_YEAR:
        return "WEEK_OF_YEAR";
    case Calendar.WEEK_OF_MONTH:
        return "WEEK_OF_MONTH";
    case Calendar.DAY_OF_MONTH:
        return "DAY_OF_MONTH";
    case Calendar.DAY_OF_YEAR:
        return "DAY_OF_YEAR";
    case Calendar.DAY_OF_WEEK:
        return "DAY_OF_WEEK";
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return "DAY_OF_WEEK_IN_MONTH";
    case Calendar.AM_PM:
        return "AM_PM";
    case Calendar.HOUR:
        return "HOUR";
    case Calendar.HOUR_OF_DAY:
        return "HOUR_OF_DAY";
    case Calendar.MINUTE:
        return "MINUTE";
    case Calendar.SECOND:
        return "SECOND";
    case Calendar.MILLISECOND:
        return "MILLISECOND";
    default:
        return null;
    }
}