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.frameworkset.platform.sanylog.action.CounterController.java

/**??
*//*from   w ww. j  a  v a2  s  .co  m*/

public @ResponseBody(datatype = "json") String statisticOperCounterImmediately() {
    Calendar today = Calendar.getInstance();
    String todayTime = DateUtils.format(today.getTime(), DateUtils.ISO8601_DATE_PATTERN);
    String week = String.valueOf(today.get(Calendar.WEEK_OF_YEAR));
    week = todayTime.substring(0, 4) + "-" + week;
    Calendar startDate = Calendar.getInstance();
    int offset = startDate.get(Calendar.DAY_OF_WEEK);
    startDate.add(Calendar.DAY_OF_MONTH, offset - (offset * 2 - 1));
    String startTime = DateUtils.format(startDate.getTime(), DateUtils.ISO8601_DATE_PATTERN);
    TransactionManager tm = new TransactionManager();
    try {
        tm.begin(TransactionManager.RW_TRANSACTION);
        //
        counterManager.deleteOperCounterByWeek(week);
        counterManager.staticOperCounterByWeek(startTime, todayTime, week);

        //
        counterManager.deleteOperCounterByDay(todayTime);
        counterManager.staticOperCounterByDay(todayTime);

        //
        counterManager.deleteOperCounterByMonth(todayTime.substring(0, 7));
        counterManager.staticOperCounterByMonth(todayTime.substring(0, 7));

        //

        counterManager.deleteOperCounterByYear(todayTime.substring(0, 4));
        counterManager.staticOperCounterByYear(todayTime.substring(0, 4));

        tm.commit();

        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return e.getLocalizedMessage();
    } finally {
        tm.release();
    }
}

From source file:com.haulmont.timesheets.service.StatisticServiceBean.java

@Override
public Map<Integer, Map<String, Object>> getStatisticsByProjects(Date start, Date end) {
    LoadContext<TimeEntry> loadContext = new LoadContext<>(TimeEntry.class)
            .setQuery(new LoadContext.Query(
                    "select t from ts$TimeEntry t where t.date >= :start and t.date <= :end order by t.date")
                            .setParameter("start", start).setParameter("end", end))
            .setView(new View(TimeEntry.class)
                    .addProperty("task",
                            new View(Task.class).addProperty("name").addProperty("project",
                                    viewRepository.getView(Project.class, View.MINIMAL)))
                    .addProperty("timeInMinutes").addProperty("date"));
    List<TimeEntry> timeEntries = dataManager.loadList(loadContext);

    Map<WeekAndProject, BigDecimal> statistic = new LinkedHashMap<>();
    Calendar calendar = Calendar.getInstance();
    for (TimeEntry timeEntry : timeEntries) {
        calendar.setTime(timeEntry.getDate());
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        WeekAndProject key = new WeekAndProject(weekOfYear, timeEntry.getTask().getProject());

        BigDecimal sum = statistic.get(key);
        if (sum == null) {
            sum = BigDecimal.ZERO;
        }// w  w w.ja  va  2 s  .c o  m

        sum = sum.add(HoursAndMinutes.fromTimeEntry(timeEntry).toBigDecimal());
        statistic.put(key, sum);
    }

    Map<Integer, Map<String, Object>> result = new LinkedHashMap<>();
    for (Map.Entry<WeekAndProject, BigDecimal> entry : statistic.entrySet()) {
        Integer week = entry.getKey().week;
        Map<String, Object> projectsByWeek = result.get(week);
        if (projectsByWeek == null) {
            projectsByWeek = new HashMap<>();
            calendar.set(Calendar.WEEK_OF_YEAR, week);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            projectsByWeek.put("week", DateTimeUtils.getDateFormat().format(calendar.getTime()));

        }
        projectsByWeek.put(entry.getKey().project.getName(), entry.getValue());
        result.put(week, projectsByWeek);
    }

    return result;
}

From source file:org.encuestame.rest.api.test.json.TweetPollJsonControllerTestCase.java

/**
 * Init.//from   w  ww  . j  av a  2  s  .  co  m
 */
@Before
public void initJsonService() {
    this.userAccount = getSpringSecurityLoggedUserAccount();
    this.initQuestion = createQuestion("Bayern  VS Borussia?", userAccount.getAccount());
    final Question question = createQuestion("Real Madrid VS Barcelona?", userAccount.getAccount());
    final Question question1 = createQuestion("Real Madrid or Barcelona?", userAccount.getAccount());
    final Question question2 = createQuestion("Real Madrid or Barcelona?", userAccount.getAccount());
    final Question question3 = createQuestion("Real Madrid or Barcelona?", userAccount.getAccount());
    this.tp1 = createTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, new Date(), userAccount, question);
    createTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, new Date(), userAccount, question1);
    createTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, new Date(), userAccount, question2);
    createTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, new Date(), userAccount, question3);
    DateTime dt = creationDate.minusDays(3);

    createDefaultTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, this.userAccount, question3,
            dt.toDate());

    dt = creationDate.minusDays(2);
    createDefaultTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, this.userAccount, question3,
            dt.toDate());

    dt = creationDate.minusDays(9);
    createDefaultTweetPollPublicated(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, this.userAccount, question3,
            dt.toDate());

    this.tp1.setFavourites(true);
    final Calendar ca = Calendar.getInstance();
    ca.add(Calendar.WEEK_OF_YEAR, -1);
    this.tp1.setCreateDate(ca.getTime());
    this.tp1.setScheduleTweetPoll(true);
    getAccountDao().saveOrUpdate(this.tp1);
}

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

/**
 * Increase the actual chosen view. (DAY / MONTH / WEEK/ YEAR)
 *///from w ww . j  a  v  a  2s  .  c  o  m
protected final void increment() {
    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:com.ocs.dynamo.utils.DateUtils.java

/**
 * Converts a date to its corresponding week code
 * /*  ww w  .j a  v  a  2 s.co  m*/
 * @param date
 * @return
 */
public static String toWeekCode(Date date) {
    if (date != null) {
        Calendar calendar = new GregorianCalendar(DynamoConstants.DEFAULT_LOCALE);
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        int month = calendar.get(Calendar.MONTH);

        // if the week number is reported as 1, but we are in December,
        // then we have an "overflow"
        if (week == FIRST_WEEK_NUMBER && month == Calendar.DECEMBER) {
            year++;
        }

        return year + "-" + StringUtils.leftPad(Integer.toString(week), 2, "0");
    }
    return null;
}

From source file:org.openvpms.web.echo.date.RelativeDateParser.java

/**
 * Parses a date relative to the specified date.
 *
 * @param source the relative date string
 * @param date   the date/*  w w w  .  j  av  a  2s  .co  m*/
 * @return the relative date, or <code>null</code> if the source is invalid
 */
public Date parse(String source, Date date) {
    if (StringUtils.isEmpty(source)) {
        return null;
    }
    Matcher matcher = pattern.matcher(source.toLowerCase());
    Calendar calendar;
    calendar = new GregorianCalendar();
    calendar.setTime(date);
    int start = 0;
    boolean neg = false;
    boolean first = true;
    while (start < source.length() && matcher.find(start)) {
        if (start != matcher.start()) {
            return null;
        }
        String valueGroup = matcher.group(1);
        int value = Integer.parseInt(valueGroup);
        if (first) {
            if (value < 0) {
                neg = true;
            }
            first = false;
        } else if (value >= 0 && valueGroup.charAt(0) != '+' && neg) {
            // if there is a leading sign, and the current value has no explicit +, it propagates to all
            // other patterns where no sign is specified
            value = -value;
        }
        String field = matcher.group(2);
        String se = matcher.group(3); // get any s=start or e=end
        if (field.equals("d")) {
            calendar.add(Calendar.DAY_OF_MONTH, value);
            // se ignored if day
        } else if (field.equals("m")) {
            calendar.add(Calendar.MONTH, value);
            if (se.equals("s")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
            } else if (se.equals("e")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
                calendar.add(Calendar.MONTH, 1); // go to next month
                calendar.add(Calendar.DAY_OF_MONTH, -1); // back one to to get end of month
            }
        } else if (field.equals("w")) {
            calendar.add(Calendar.WEEK_OF_YEAR, value);
            if (se.equals("s")) {
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //set Monday
            } else if (se.equals("e")) {
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); //set Friday
            }
        } else if (field.equals("q")) { // quarter
            calendar.add(Calendar.MONTH, 3 * value); // move by 3 month blocks
            if (se.equals("s")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
                int k = calendar.get(Calendar.MONTH); // get month (0 = Jan)
                k = (k / 3) * 3; // get month number at start of quarter (0,3,6,9)
                calendar.set(Calendar.MONTH, k); // set that month
            } else if (se.equals("e")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
                int k = calendar.get(Calendar.MONTH); // get month (0 = Jan)
                k = (k / 3) * 3; // get month number at start of quarter (0,3,6,9)
                calendar.set(Calendar.MONTH, k); // set that month
                calendar.add(Calendar.MONTH, 3); // go to next quarter
                calendar.add(Calendar.DAY_OF_MONTH, -1); // back one day to to get end of quarter
            }
        } else {
            calendar.add(Calendar.YEAR, value);
            if (se.equals("s")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
                calendar.set(Calendar.MONTH, Calendar.JANUARY); // set January
            } else if (se.equals("e")) {
                calendar.set(Calendar.DAY_OF_MONTH, 1); // set 1st of month
                calendar.set(Calendar.MONTH, Calendar.JANUARY); // set January
                calendar.add(Calendar.YEAR, 1); // go to next year
                calendar.add(Calendar.DAY_OF_MONTH, -1); // back one to to get 31 Dec
            }
        }

        start = matcher.end();
    }
    return (start == source.length()) ? calendar.getTime() : null;
}

From source file:com.qdum.llhb.common.utils.DateUtils.java

/**
 * ??/* w ww  .ja v  a2 s.  c  o m*/
 *
 * @param date1
 * @param date2
 * @return
 */
public static Boolean isSameWeek(Date date1, Date date2) {
    Calendar c = Calendar.getInstance();
    c.setTime(date1);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    int week1 = c.get(Calendar.WEEK_OF_YEAR);
    int week2 = c2.get(Calendar.WEEK_OF_YEAR);
    if (week1 == week2) {
        return Boolean.TRUE;
    } else {
        return Boolean.FALSE;
    }
}

From source file:jdchub.module.commands.handlers.BanCommand.java

@Override
public String execute(String cmd, String args, AbstractClient commandOwner) {
    banExpiresDate = new Date();

    this.commandOwner = commandOwner;

    this.nick = null;

    this.reason = null;

    LongOpt[] longOpts = new LongOpt[5];

    longOpts[0] = new LongOpt("nick", LongOpt.REQUIRED_ARGUMENT, null, 'n');
    longOpts[1] = new LongOpt("reason", LongOpt.REQUIRED_ARGUMENT, null, 'r');
    longOpts[2] = new LongOpt("time", LongOpt.REQUIRED_ARGUMENT, null, 't');
    longOpts[3] = new LongOpt("ip", LongOpt.REQUIRED_ARGUMENT, null, 'i');
    longOpts[4] = new LongOpt("mask", LongOpt.REQUIRED_ARGUMENT, null, 'm');

    String[] argArray = CommandUtils.strArgToArray(args);

    Getopt getopt = new Getopt(cmd, argArray, "n:r:t:i:m:", longOpts);

    if (argArray.length < 1) {
        showHelp();//from w w  w. j a  v  a  2  s.  co  m
        return null;
    }

    int c;

    while ((c = getopt.getopt()) != -1) {
        switch (c) {
        case 'n':
            this.nick = getopt.getOptarg();
            break;

        case 'r':
            this.reason = getopt.getOptarg();
            break;

        case 'i':
            this.ip = getopt.getOptarg();
            break;

        case 'm':
            this.mask = getopt.getOptarg();
            break;

        case 't':
            // Valid entries for <time> are Ns, Nm, Nh, Nd, Nw, NM, Ny
            String timeString = getopt.getOptarg();
            int timeType = timeString.charAt(timeString.length() - 1);
            int time;
            try {
                time = Integer.parseInt(timeString.substring(0, timeString.length() - 1));
                if (time <= 0) {
                    throw new NumberFormatException("Invalid time format : time must be greater than 0");
                }
            } catch (NumberFormatException ex) {
                showError("Invalid time format.");
                return "Invalid expires time.";
            }

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(banExpiresDate);

            switch (timeType) {
            case 's':
                timeType = Calendar.SECOND;
                break;

            case 'm':
                timeType = Calendar.MINUTE;
                break;

            case 'h':
                timeType = Calendar.HOUR;
                break;

            case 'd':
                timeType = Calendar.DAY_OF_YEAR;
                break;

            case 'w':
                timeType = Calendar.WEEK_OF_YEAR;
                break;

            case 'M':
                timeType = Calendar.MONTH;
                break;

            case 'Y':
                timeType = Calendar.YEAR;
                break;

            default:
                showError("Invalid time format.");
            }

            calendar.add(timeType, time);
            this.banExpiresDate = calendar.getTime();
            this.banType = Constants.BAN_TEMPORARY;

            break;

        case '?':
            showHelp();
            break;

        default:
            showHelp();
            break;
        }
    }

    return ban();
}

From source file:com.baidu.rigel.biplatform.ac.util.TimeDimensionUtils.java

/**
 * ?Member/*from w ww .  j  a v  a  2 s  .  c  o  m*/
 * 
 * @param calendar 
 * @param timeType ??Member
 * @param timeFormat ?
 * @return Member
 */
public static MiniCubeMember createTimeMember(Calendar calendar, TimeType timeType, String timeFormat) {
    // ??
    String resultTimeFormat = StringUtils.isBlank(timeFormat) ? timeType.getFormat() : timeFormat;
    int quarter = calendar.get(Calendar.MONTH) / 3 + 1;
    String name = "";
    String caption = "";
    if (resultTimeFormat.toUpperCase().endsWith("QN")) {
        name = timeFormat.toUpperCase().replace(TimeType.TimeYear.getFormat(),
                calendar.get(Calendar.YEAR) + "");
        name = name.toUpperCase().replace("N", quarter + "");
    } else if (resultTimeFormat.toUpperCase().endsWith("WN")) {
        name = timeFormat.toUpperCase().replace(TimeType.TimeYear.getFormat(),
                calendar.get(Calendar.YEAR) + "");
        name = name.toUpperCase().replace("N", calendar.get(Calendar.WEEK_OF_YEAR) + "");
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat(resultTimeFormat);
        name = sdf.format(calendar.getTime());
    }
    MiniCubeLevel level = new MiniCubeLevel("level_" + timeType);

    if (timeType.equals(TimeType.TimeYear)) {
        caption = calendar.get(Calendar.YEAR) + "";
        level.setType(LevelType.TIME_YEARS);
    } else if (timeType.equals(TimeType.TimeQuarter)) {
        caption = calendar.get(Calendar.YEAR) + "_Q" + quarter;
        level.setType(LevelType.TIME_QUARTERS);
    } else if (timeType.equals(TimeType.TimeMonth)) {
        caption = calendar.get(Calendar.YEAR) + "_" + (calendar.get(Calendar.MONTH) + 1);
        level.setType(LevelType.TIME_MONTHS);
    } else if (timeType.equals(TimeType.TimeWeekly)) {
        caption = calendar.get(Calendar.YEAR) + "_W" + calendar.get(Calendar.WEEK_OF_YEAR);
        level.setType(LevelType.TIME_WEEKS);
    } else if (timeType.equals(TimeType.TimeDay)) {
        caption = DEFAULT_SIMPLE_DATEFORMAT.format(calendar.getTime());
        level.setType(LevelType.TIME_DAYS);
    }

    MiniCubeMember member = new MiniCubeMember(name);
    member.setCaption(caption);
    member.setVisible(true);
    member.setLevel(level);
    return member;
}

From source file:com.zxy.commons.lang.utils.DatesUtils.java

/**
 * ?/*from  www  .  j  av  a  2  s .c om*/
 * 
 * @param date date
 * @return 
 */
public static String getWeekOfYear(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    int year = calendar.get(Calendar.YEAR);
    return new StringBuilder("").append(year).append(week).toString();
}