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:Main.java

/**
 * Method to check if a date is this week. Note that it checks if it is this
 * week and not 7 days ahead!//w  w  w  .  j a v  a2s .  c o m
 * 
 * @param theDate
 *            the date to see if it is this week
 * @return true if it is this week
 */
public static boolean isThisWeek(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time
    Calendar tmpDate = Calendar.getInstance();

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.WEEK_OF_YEAR) == theCalendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:Main.java

public static void moveToCalendarWeekOfYear(Calendar cal, int weekOfYear) {
    assertArgumentNotMinusInteger("weekOfYear", weekOfYear);
    cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
}

From source file:Main.java

public static Date startOfNextWeek() {
    Calendar cal = Calendar.getInstance();

    cal.setTime(startOfThisWeek());/* w w w . ja  v a  2s  .  c om*/
    cal.add(Calendar.WEEK_OF_YEAR, 1);

    return cal.getTime();
}

From source file:CalendarDemo.java

public void format() {

    // Tell the calendar what date/time to format
    calendar.setTime(timeNow);/*from  ww w.j a  va  2 s.co  m*/

    // print out most of the known fields
    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:org.kalypso.commons.time.PeriodUtils.java

public static Period getPeriod(final int calendarField, final int amount) {
    switch (calendarField) {
    case Calendar.YEAR:
        return Period.years(amount);

    case Calendar.MONTH:
        return Period.months(amount);

    case Calendar.WEEK_OF_YEAR:
    case Calendar.WEEK_OF_MONTH:
        return Period.weeks(amount);

    case Calendar.DAY_OF_MONTH:
    case Calendar.DAY_OF_YEAR:
    case Calendar.DAY_OF_WEEK:
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return Period.days(amount);

    case Calendar.HOUR:
    case Calendar.HOUR_OF_DAY:
        return Period.hours(amount);

    case Calendar.MINUTE:
        return Period.minutes(amount);

    case Calendar.SECOND:
        return Period.seconds(amount);

    case Calendar.MILLISECOND:
        return Period.millis(amount);

    case Calendar.AM_PM:
    case Calendar.ERA:
    default://www.ja  va2s  .c om
        throw new UnsupportedOperationException();
    }
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsEraser.java

/**
 * Each day at 0:00 remove old out dated entries.
 *//*from  www .  ja va2 s  .c  o m*/
@Scheduled(cron = "0 0 0 * * *")
public void erase() {
    Calendar gc = new GregorianCalendar();
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    gc.add(Calendar.WEEK_OF_YEAR, -1 * Math.abs(this.numberOfWeeksToKeepLogs));
    this.dao.eraseSystemUserLog(gc.getTime());
    this.dao.eraseApplicationUserLog(gc.getTime());
    this.dao.eraseFlowLogMessage(gc.getTime());
}

From source file:Main.java

/**
 * Adds a number of weeks to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date  the date, not null/*from ww w  .j ava  2s.co m*/
 * @param amount  the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
public static Date addWeeks(Date date, int amount) {
    return add(date, Calendar.WEEK_OF_YEAR, amount);
}

From source file:edu.jhuapl.graphs.jfreechart.TimeSeriesEffectsTest.java

public static JFreeChartGraphSource getSource() throws GraphException {
    Calendar base = Calendar.getInstance();
    base.set(Calendar.WEEK_OF_YEAR, 6);

    Random r = new Random();

    List<TimePoint> ps1 = new LinkedList<TimePoint>();
    List<TimePoint> ps2 = new LinkedList<TimePoint>();
    List<TimePoint> ps3 = new LinkedList<TimePoint>();

    for (int count = 0; count < 4; count += 1) {
        base.set(Calendar.DAY_OF_WEEK, 2);
        addPoint(base.getTime(), ps2, r);

        // now generate five points for ps1 and p23, the daily sets
        addPoint(base.getTime(), ps1, r);
        addPoint(base.getTime(), ps3, r, "showPoint?week=" + count + "&day=0", null);

        for (int i = 1; i < 5; i += 1) {
            base.add(Calendar.DAY_OF_WEEK, 1);
            addPoint(base.getTime(), ps1, r);
            addPoint(base.getTime(), ps3, r, "showPoint?week=" + count + "&day=" + i, null);
        }//from w ww  .  ja  v a 2  s.c om

        base.set(Calendar.DAY_OF_WEEK, 1);
        base.add(Calendar.WEEK_OF_YEAR, 1);
    }

    List<DefaultTimeSeries> series = new LinkedList<DefaultTimeSeries>();
    Map<String, Object> s1Md = new HashMap<String, Object>();
    s1Md.put(GraphSource.SERIES_TIME_RESOLUTION, TimeResolution.DAILY);
    s1Md.put(GraphSource.SERIES_SHAPE, new Ellipse2D.Float(-5, -5, 10, 10));
    s1Md.put(GraphSource.SERIES_TITLE, "Frederick County Counts");
    series.add(new DefaultTimeSeries(ps1, s1Md));

    Map<String, Object> s2Md = new HashMap<String, Object>();
    s2Md.put(GraphSource.SERIES_TIME_RESOLUTION, TimeResolution.WEEKLY);
    s2Md.put(GraphSource.SERIES_SHAPE, new Ellipse2D.Float(-7, -7, 14, 14));
    s2Md.put(GraphSource.SERIES_TITLE, "NCR Counts");
    series.add(new DefaultTimeSeries(ps2, s2Md));

    Map<String, Object> s3Md = new HashMap<String, Object>();
    s3Md.put(GraphSource.SERIES_TIME_RESOLUTION, TimeResolution.DAILY);
    s3Md.put(GraphSource.SERIES_SHAPE, new Rectangle2D.Float(-3, -3, 6, 6));
    s3Md.put(GraphSource.SERIES_TITLE, "Carroll County Counts");
    BasicStroke bs = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10,
            new float[] { 3F, 2F, 8F }, 1F);
    s3Md.put(GraphSource.SERIES_STROKE, bs);
    series.add(new DefaultTimeSeries(ps3, s3Md));

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(GraphSource.BACKGROUND_COLOR, Color.BLUE);
    params.put(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Color.WHITE);
    params.put(GraphSource.GRAPH_TITLE, "Counts");
    params.put(GraphSource.GRAPH_X_LABEL, "Time");
    params.put(GraphSource.GRAPH_Y_LABEL, "Total Counts");
    DateAxis customAxis = new RotatedTickDateAxis(60.);
    params.put(JFreeChartTimeSeriesGraphSource.DATE_AXIS, customAxis);

    JFreeChartGraphSource source = new JFreeChartGraphSource();
    source.setData(series);
    source.setParams(params);
    source.initialize();

    return source;
}

From source file:com.ocs.dynamo.utils.DateUtils.java

/**
 * Gets the number of the last week of a year
 * //from w  w  w  . j ava2 s  .  c  o  m
 * @param year
 *            the year
 * @return
 */
public static int getLastWeekOfYear(int year) {
    Date date = createDate("3112" + year);
    Calendar calendar = Calendar.getInstance(DynamoConstants.DEFAULT_LOCALE);
    calendar.setTime(date);

    // it is possible for the last day of a year to actually be part of the
    // first week of next year. We have to compensate for this
    int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
    while (weekNumber == 1) {
        calendar.add(Calendar.DATE, -1);
        weekNumber = calendar.get(Calendar.WEEK_OF_YEAR);
    }
    return weekNumber;
}

From source file:srvmonitor.thGetAgendas.java

@Override
public void run() {
    /*//  w  ww.jav a  2 s.  com
    Recupera Parametros Fecha Actual
    */
    logger.info("Buscando Agendas Activas");

    String[] ids = TimeZone.getAvailableIDs(-4 * 60 * 60 * 1000);
    String clt = ids[0];
    SimpleTimeZone tz = new SimpleTimeZone(-4 * 60 * 60 * 1000, clt);
    tz.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    tz.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    Calendar calendar = new GregorianCalendar(tz);

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
    int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);

    int hour = calendar.get(Calendar.HOUR); // 12 hour clock
    int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    int millisecond = calendar.get(Calendar.MILLISECOND);

    int findHour = 12;
    int findMinutes = 5;

    /*
    calendar.add(Calendar.HOUR_OF_DAY, -1);
    int hourBefore = calendar.get(Calendar.HOUR_OF_DAY);
            
    calendar.add(Calendar.HOUR_OF_DAY, 2);
    int hourAfter = calendar.get(Calendar.HOUR_OF_DAY);
    */

    String posmonth = String.valueOf(month + 1);
    String posdayOfMonth = String.valueOf(dayOfMonth);
    String posdayOfWeek = String.valueOf(dayOfWeek);
    String posweekOfYear = String.valueOf(weekOfYear);
    String posweekOfMonth = String.valueOf(weekOfMonth);
    String poshourOfDay = String.valueOf(hourOfDay);
    String posminute = String.valueOf(minute);
    String possecond = String.valueOf(second);
    String posmillisecond = String.valueOf(millisecond);

    Calendar iteratorCalendar;
    String vSQL;
    String iteratorHour;
    String iteratorMinute;
    Statement stm;
    JSONObject jData;
    JSONObject jDataMinute;
    JSONArray jArray = new JSONArray();
    JSONArray jArrayMinute = new JSONArray();
    String posIteratorHour;
    String posIteratorMinute;

    /*
    Inicializa Lista de Agendas
    */
    gDatos.getLstShowAgendas().clear();
    gDatos.getLstActiveAgendas().clear();

    for (int i = -findHour; i <= findHour; i++) {
        iteratorCalendar = new GregorianCalendar(tz);
        iteratorCalendar.add(Calendar.HOUR_OF_DAY, i);
        iteratorHour = String.valueOf(iteratorCalendar.get(Calendar.HOUR_OF_DAY));
        posIteratorHour = String.valueOf(Integer.valueOf(iteratorHour) + 1);

        vSQL = "select " + iteratorHour
                + " horaAgenda,ageID, month, dayOfMonth, dayOfWeek, weekOfYear, weekOfMonth, hourOfDay from process.tb_agenda where "
                + "     ageEnable=1 " + "     and substr(month," + posmonth + ",1) = '1'"
                + "     and substr(dayOfMonth," + posdayOfMonth + ",1) = '1'" + "     and substr(dayOfWeek,"
                + posdayOfWeek + ",1) = '1'" + "     and substr(weekOfYear," + posweekOfYear + ",1) = '1'"
                + "     and substr(weekOfMonth," + posweekOfMonth + ",1) = '1'" + "     and substr(hourOfDay,"
                + posIteratorHour + ",1) = '1'";
        logger.debug("i: " + i + " vSQL: " + vSQL);
        try {
            stm = gDatos.getServerStatus().getMetadataConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY);
            jData = new JSONObject();

            ResultSet rs = stm.executeQuery(vSQL);
            if (rs != null) {
                while (rs.next()) {
                    jData = new JSONObject();
                    jData.put("horaAgenda", rs.getString("horaAgenda"));
                    jData.put("ageID", rs.getString("ageID"));
                    jData.put("month", rs.getString("month"));
                    jData.put("dayOfMonth", rs.getString("dayOfMonth"));
                    jData.put("weekOfYear", rs.getString("weekOfYear"));
                    jData.put("weekOfMonth", rs.getString("weekOfMonth"));
                    jData.put("hourOfDay", rs.getString("hourOfDay"));
                    jArray.put(jData);
                    gDatos.getLstShowAgendas().add(jData);
                }
            } else {
                jData.put("horaAgenda", iteratorHour);
                jData.put("ageID", "");
                jData.put("month", "");
                jData.put("dayOfMonth", "");
                jData.put("weekOfYear", "");
                jData.put("weekOfMonth", "");
                jData.put("hourOfDay", "");
                jArray.put(jData);
                gDatos.getLstShowAgendas().add(jData);
                System.out.println("No hay registros");
            }
            stm.close();
        } catch (SQLException | JSONException e) {
            logger.error(e.getMessage());
        }
    }

    iteratorCalendar = new GregorianCalendar(tz);
    iteratorHour = String.valueOf(iteratorCalendar.get(Calendar.HOUR_OF_DAY));
    posIteratorHour = String.valueOf(Integer.valueOf(iteratorHour) + 1);

    for (int i = -findMinutes; i <= 0; i++) {
        iteratorCalendar = new GregorianCalendar(tz);
        iteratorCalendar.add(Calendar.MINUTE, i);
        iteratorMinute = String.valueOf(iteratorCalendar.get(Calendar.MINUTE));
        posIteratorMinute = String.valueOf(Integer.valueOf(iteratorMinute) + 1);

        vSQL = "select " + iteratorMinute
                + " horaAgenda,ageID, month, dayOfMonth, dayOfWeek, weekOfYear, weekOfMonth, hourOfDay from process.tb_agenda where "
                + "     ageEnable=1 " + "     and substr(month," + posmonth + ",1) = '1'"
                + "     and substr(dayOfMonth," + posdayOfMonth + ",1) = '1'" + "     and substr(dayOfWeek,"
                + posdayOfWeek + ",1) = '1'" + "     and substr(weekOfYear," + posweekOfYear + ",1) = '1'"
                + "     and substr(weekOfMonth," + posweekOfMonth + ",1) = '1'" + "     and substr(hourOfDay,"
                + posIteratorHour + ",1) = '1'" + "     and substr(minute," + posIteratorMinute + ",1) = '1'";
        logger.debug("i: " + i + " vSQL: " + vSQL);
        try {
            stm = gDatos.getServerStatus().getMetadataConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY);

            ResultSet rs = stm.executeQuery(vSQL);
            if (rs != null) {
                while (rs.next()) {
                    jDataMinute = new JSONObject();
                    jDataMinute.put("horaAgenda", rs.getString("horaAgenda"));
                    jDataMinute.put("ageID", rs.getString("ageID"));
                    jDataMinute.put("month", rs.getString("month"));
                    jDataMinute.put("dayOfMonth", rs.getString("dayOfMonth"));
                    jDataMinute.put("weekOfYear", rs.getString("weekOfYear"));
                    jDataMinute.put("weekOfMonth", rs.getString("weekOfMonth"));
                    jDataMinute.put("hourOfDay", rs.getString("hourOfDay"));
                    jArrayMinute.put(jDataMinute);
                    gDatos.getLstActiveAgendas().add(jDataMinute);
                }
            }
            stm.close();
        } catch (SQLException | JSONException e) {
            logger.error(e.getMessage());
        }
    }

    for (int i = 0; i < gDatos.getLstShowAgendas().size(); i++) {
        logger.debug(gDatos.getLstShowAgendas().get(i).toString());
    }

    for (int i = 0; i < gDatos.getLstActiveAgendas().size(); i++) {
        logger.debug(gDatos.getLstActiveAgendas().get(i).toString());
    }

    logger.info("Finaliza busquenda agendas activas...");
}