Example usage for java.util Calendar DAY_OF_WEEK

List of usage examples for java.util Calendar DAY_OF_WEEK

Introduction

In this page you can find the example usage for java.util Calendar DAY_OF_WEEK.

Prototype

int DAY_OF_WEEK

To view the source code for java.util Calendar DAY_OF_WEEK.

Click Source Link

Document

Field number for get and set indicating the day of the week.

Usage

From source file:fr.paris.lutece.plugins.calendar.service.Utils.java

/**
 * Returns the Week as a formatted string corresponding to the date code
 * @return The week label/*from   w  w w .ja v  a 2 s .  c om*/
 * @param locale The locale used for display settings
 * @param strDate The date code
 */
public static String getWeekLabel(String strDate, Locale locale) {
    Calendar calendar = new GregorianCalendar();
    Calendar calendarFirstDay = new GregorianCalendar();
    Calendar calendarLastDay = new GregorianCalendar();
    calendar.set(Utils.getYear(strDate), Utils.getMonth(strDate), Utils.getDay(strDate));

    int nDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    if (nDayOfWeek == 1) {
        nDayOfWeek = 8;
    }

    calendarFirstDay = calendar;
    calendarFirstDay.add(Calendar.DATE, Calendar.MONDAY - nDayOfWeek);
    calendarLastDay = (GregorianCalendar) calendarFirstDay.clone();
    calendarLastDay.add(Calendar.DATE, 6);

    String strFormat = AppPropertiesService.getProperty(Constants.PROPERTY_LABEL_FORMAT_DATE_OF_DAY);
    DateFormat formatDate = new SimpleDateFormat(strFormat, locale);
    String strLabelFirstDay = formatDate.format(calendarFirstDay.getTime());
    String strLabelLastDay = formatDate.format(calendarLastDay.getTime());
    calendarFirstDay.clear();
    calendarLastDay.clear();

    return strLabelFirstDay + "-" + strLabelLastDay;
}

From source file:com.feilong.commons.core.date.DateUtil.java

/**
 * date  (<b></b>) <code>00:00:00.000</code> <br>
 * ?:,,?<br>// www.  j  ava 2  s.  com
 * ?:,?
 * 
 * <pre>
 *   2012-10-11 17:10:30.701(),
 * return 2012-10-07 00:00:00.000
 * 
 * 
 * getFirstDateOfThisWeek(2014-01-01 05:00:00)
 * return 2013-12-29 00:00:00.000
 * </pre>
 * 
 * @param date
 *            the date
 * @return date  <code>00:00:00.000</code> 
 * @see #toCalendar(Date)
 * @see Calendar#set(int, int)
 * @see #dayBegin(Calendar)
 * @see Calendar#getTime()
 * @since 1.0
 */
public static final Date getFirstDateOfThisWeek(Date date) {
    Calendar calendar = toCalendar(date);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    dayBegin(calendar);
    return calendar.getTime();
}

From source file:com.carapp.login.splashActivity.java

private void getClientBranch() {

    MultipartEntity entity = new MultipartEntity();
    try {/*from  www.  j  a  v  a2 s.  c o m*/
        entity.addPart("action", new StringBody("init_config"));

        new AsyncWebServiceProcessingTask(context, entity, messagecheck, new AsynckCallback() {

            @Override
            public void run(String result) {
                if (UIUtils.checkJson(result, context)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.optString("satus").equals("success")) {
                            PdfInfo.client = jsonObject.optString("client");
                            PdfInfo.branch = jsonObject.optString("branch");
                            Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces,
                                    new Callback2() {
                                        @Override
                                        public void ok(final Dialog dialog) {
                                            final Handler handler = new Handler();
                                            final Runnable runnable = new Runnable() {
                                                @Override
                                                public void run() {
                                                    dialog.dismiss();
                                                    new AsyncWebServiceProcessingTask(context, null,
                                                            "Please wait while checking date",
                                                            new AsynckCallback() {

                                                                @Override
                                                                public void run(String result) {
                                                                    Log.e("date", "" + result);
                                                                    String sysdate = android.text.format.DateFormat
                                                                            .format("dd/MM/yyyy",
                                                                                    new java.util.Date())
                                                                            .toString();
                                                                    if (result.equals(sysdate)) {
                                                                        Toast.makeText(context, "date match",
                                                                                Toast.LENGTH_SHORT).show();
                                                                        new AsyncWebServiceProcessingTask(
                                                                                context, null, messagecheckday,
                                                                                new AsynckCallback() {
                                                                                    @Override
                                                                                    public void run(
                                                                                            String result) {
                                                                                        int day = Integer
                                                                                                .parseInt(
                                                                                                        result);
                                                                                        Calendar calendar = Calendar
                                                                                                .getInstance();
                                                                                        int Today = calendar
                                                                                                .get(Calendar.DAY_OF_WEEK);
                                                                                        day++;
                                                                                        if (day == Today) {
                                                                                            Toast.makeText(
                                                                                                    context,
                                                                                                    "day is correct",
                                                                                                    Toast.LENGTH_SHORT)
                                                                                                    .show();
                                                                                            String ma_a = null;

                                                                                            try {
                                                                                                WifiManager wiman = (WifiManager) getSystemService(
                                                                                                        Context.WIFI_SERVICE);
                                                                                                ma_a = wiman
                                                                                                        .getConnectionInfo()
                                                                                                        .getMacAddress();
                                                                                                Log.i(t, ""
                                                                                                        + ma_a);
                                                                                            } catch (Exception e1) {

                                                                                                e1.printStackTrace();
                                                                                                Log.i(t, " "
                                                                                                        + e1);
                                                                                            }
                                                                                            MultipartEntity entity = new MultipartEntity();
                                                                                            try {
                                                                                                entity.addPart(
                                                                                                        "action",
                                                                                                        new StringBody(
                                                                                                                "device_authentication"));
                                                                                                entity.addPart(
                                                                                                        "mac_address",
                                                                                                        new StringBody(
                                                                                                                ma_a));
                                                                                                new AsyncWebServiceProcessingTask(
                                                                                                        context,
                                                                                                        entity,
                                                                                                        "Checking License",
                                                                                                        new AsynckCallback() {

                                                                                                            @Override
                                                                                                            public void run(
                                                                                                                    String result) {

                                                                                                                if (UIUtils
                                                                                                                        .checkJson(
                                                                                                                                result,
                                                                                                                                context)) {
                                                                                                                    try {
                                                                                                                        JSONObject jsonObject = new JSONObject(
                                                                                                                                result);
                                                                                                                        if (jsonObject
                                                                                                                                .optString(
                                                                                                                                        "satus")
                                                                                                                                .equals("success")) {
                                                                                                                            Toast.makeText(
                                                                                                                                    context,
                                                                                                                                    "LicenseCheck sucesses",
                                                                                                                                    Toast.LENGTH_SHORT)
                                                                                                                                    .show();
                                                                                                                            startActivity(
                                                                                                                                    new Intent(
                                                                                                                                            context,
                                                                                                                                            LoginActivity.class));
                                                                                                                            finish();

                                                                                                                        } else if (jsonObject
                                                                                                                                .optString(
                                                                                                                                        "satus")
                                                                                                                                .equals("error")) {
                                                                                                                            Util.showCustomDialog(
                                                                                                                                    context,
                                                                                                                                    "Error",
                                                                                                                                    jsonObject
                                                                                                                                            .optString(
                                                                                                                                                    "msg"));

                                                                                                                        }

                                                                                                                    } catch (Exception e) {

                                                                                                                        e.printStackTrace();

                                                                                                                    }

                                                                                                                }
                                                                                                            }
                                                                                                        }).execute(
                                                                                                                PdfInfo.newjobcard);

                                                                                            } catch (Exception e) {

                                                                                                e.printStackTrace();
                                                                                            }

                                                                                        } else {
                                                                                            Util.showCustomDialogWithoutButton(
                                                                                                    context,
                                                                                                    "Error",
                                                                                                    messagecheckdayerror
                                                                                                            + " server day is "
                                                                                                            + day
                                                                                                            + " but your device is"
                                                                                                            + Today,
                                                                                                    new Callback2() {

                                                                                                        @Override
                                                                                                        public void ok(
                                                                                                                final Dialog dialog) {

                                                                                                            final Handler handler = new Handler();
                                                                                                            final Runnable runnable = new Runnable() {
                                                                                                                @Override
                                                                                                                public void run() {
                                                                                                                    dialog.dismiss();
                                                                                                                    finish();
                                                                                                                }
                                                                                                            };
                                                                                                            handler.postDelayed(
                                                                                                                    runnable,
                                                                                                                    5000);

                                                                                                        }
                                                                                                    });
                                                                                        }

                                                                                    }
                                                                                }).execute(PdfInfo.dayaddress);
                                                                    } else {

                                                                        Util.showCustomDialogWithoutButton(
                                                                                context, "Error",
                                                                                messagecheckdateerror
                                                                                        + " server date is "
                                                                                        + result
                                                                                        + " but device date is "
                                                                                        + sysdate,
                                                                                new Callback2() {

                                                                                    @Override
                                                                                    public void ok(
                                                                                            final Dialog dialog) {

                                                                                        new Timer().schedule(
                                                                                                new TimerTask() {

                                                                                                    @Override
                                                                                                    public void run() {
                                                                                                        dialog.dismiss();
                                                                                                        finish();

                                                                                                    }
                                                                                                }, 5000);
                                                                                    }
                                                                                });

                                                                    }
                                                                }
                                                            }).execute(PdfInfo.dateaddress);

                                                }
                                            };
                                            handler.postDelayed(runnable, 5000);

                                        }
                                    });

                        } else if (jsonObject.optString("satus").equals("error")) {
                            Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces,
                                    new Callback2() {

                                        @Override
                                        public void ok(final Dialog dialog) {

                                            new Timer().schedule(new TimerTask() {

                                                @Override
                                                public void run() {
                                                    dialog.dismiss();
                                                    finish();

                                                }
                                            }, 5000);
                                        }
                                    });

                        }

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }
            }
        }).execute(PdfInfo.newjobcard);

    } catch (Exception e) {

        e.printStackTrace();
    }

}

From source file:com.scottjjohnson.finance.analysis.YahooFinanceClient.java

/**
 * Builds a url for extracting historical quotes data from Yahoo Finance.
 * /*from  www . j av a  2  s .c o  m*/
 * The date range ends on the Friday prior to the current date and begins DateRangeInDays days earlier.
 * 
 * @param ticker
 *            stock ticker symbol to be queried
 * @param dateRangeInDays
 *            number of calendar days of history to be retrieved
 * @return url containing a YQL query string
 */
private String buildHistoricalDataURL(String ticker, int dateRangeInDays) {

    Calendar cal = DateUtils.getStockExchangeCalendar(); // today

    // ************* HOKEY WORKAROUND FOR YAHOO ISSUE -- Ask for extra day into future to bust cache
    cal.add(Calendar.DAY_OF_WEEK, 1);
    dateRangeInDays++;
    // *************

    Date endDate = cal.getTime();
    cal.add(Calendar.DAY_OF_WEEK, -dateRangeInDays);
    Date startDate = cal.getTime();

    // SimpleDateFormat is not thread-safe so creating one as needed.
    SimpleDateFormat yqlDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    String query = String.format(HISTORICAL_DATA_QUERY_PATTERN, ticker, yqlDateFormat.format(startDate),
            yqlDateFormat.format(endDate));

    List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
    queryParams.add(new BasicNameValuePair("q", query));
    queryParams.add(new BasicNameValuePair("format", RESPONSE_FORMAT));
    queryParams.add(new BasicNameValuePair("env", DATA_SOURCE_ENV));

    StringBuilder sb = new StringBuilder(BASE_URL);
    sb.append(URLEncodedUtils.format(queryParams, "UTF-8"));

    LOGGER.debug("YQL query = {}.", sb.toString());

    return sb.toString();
}

From source file:TripServlet.java

public Date getLastWeek() {
    Calendar c = Calendar.getInstance();
    c = (Calendar) c.clone();// w w w .  jav  a2s  .  c o m
    c.add(Calendar.WEEK_OF_YEAR, -1);// last week
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    Date date = c.getTime();
    return date;
}

From source file:net.sourceforge.fenixedu.dataTransferObject.InfoExam.java

@Override
public DiaSemana getDiaSemana() {
    Calendar day = this.getDay();
    return new DiaSemana(day.get(Calendar.DAY_OF_WEEK));
}

From source file:com.discovery.darchrow.date.DateUtil.java

/**
 * date <span style="color:red"> ()</span> <code>00:00:00.000</code> .
 * <p>//from   w ww .  j a va  2 s  . c o  m
 * ?:,<span style="color:red">,?</span><br>
 * ?:,?
 * </p>
 * 
 * <pre>
 *   2012-10-11 17:10:30.701(),
 * return 2012-10-07 00:00:00.000
 * 
 * 
 * getFirstDateOfThisWeek(2014-01-01 05:00:00)
 * return 2013-12-29 00:00:00.000
 * </pre>
 * 
 * @param date
 *            the date
 * @return date  <code>00:00:00.000</code> 
 * @see #toCalendar(Date)
 * @see Calendar#set(int, int)
 * @see CalendarUtil#resetDayBegin(Calendar)
 * @see Calendar#getTime()
 */
public static Date getFirstDateOfThisWeek(Date date) {
    Calendar calendar = toCalendar(date);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    CalendarUtil.resetDayBegin(calendar);
    return CalendarUtil.toDate(calendar);
}

From source file:net.rrm.ehour.ui.admin.config.MainConfigBackingBean.java

public void setFirstWeekStart(Date firstWeekStart) {
    this.firstWeekStart = firstWeekStart;

    Calendar cal = new GregorianCalendar();
    cal.setTime(firstWeekStart);/*from w  w w  .  j  a  va2s  . c o m*/
    config.setFirstDayOfWeek(cal.get(Calendar.DAY_OF_WEEK));
}

From source file:com.netflix.simianarmy.basic.BasicCalendar.java

/**
 * Work day in year./*w w w .j  a v  a2s.  co m*/
 *
 * @param year
 *            the year
 * @param month
 *            the month
 * @param day
 *            the day
 * @return the day of the year adjusted to the closest workday
 */
private int workDayInYear(int year, int month, int day) {
    Calendar holiday = now();
    holiday.set(Calendar.YEAR, year);
    holiday.set(Calendar.MONTH, month);
    holiday.set(Calendar.DAY_OF_MONTH, day);
    int doy = holiday.get(Calendar.DAY_OF_YEAR);
    int dow = holiday.get(Calendar.DAY_OF_WEEK);

    if (dow == Calendar.SATURDAY) {
        return doy - 1; // FRIDAY
    }

    if (dow == Calendar.SUNDAY) {
        return doy + 1; // MONDAY
    }

    return doy;
}

From source file:Main.java

/**
 * This constructs an <code>Iterator</code> over each day in a date
 * range defined by a focus date and range style.
 *
 * For instance, passing Thursday, July 4, 2002 and a
 * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code>
 * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3,
 * 2002, returning a Calendar instance for each intermediate day.
 *
 * This method provides an iterator that returns Calendar objects.
 * The days are progressed using {@link Calendar#add(int, int)}.
 *
 * @param focus  the date to work with/*from w w w.  jav  a2  s.com*/
 * @param rangeStyle  the style constant to use. Must be one of
 * {@link DateUtils#RANGE_MONTH_SUNDAY}, 
 * {@link DateUtils#RANGE_MONTH_MONDAY},
 * {@link DateUtils#RANGE_WEEK_SUNDAY},
 * {@link DateUtils#RANGE_WEEK_MONDAY},
 * {@link DateUtils#RANGE_WEEK_RELATIVE},
 * {@link DateUtils#RANGE_WEEK_CENTER}
 * @return the date iterator
 * @throws IllegalArgumentException if the date is <code>null</code>
 * @throws IllegalArgumentException if the rangeStyle is invalid
 */
public static Iterator iterator(Calendar focus, int rangeStyle) {
    if (focus == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    Calendar start = null;
    Calendar end = null;
    int startCutoff = Calendar.SUNDAY;
    int endCutoff = Calendar.SATURDAY;
    switch (rangeStyle) {
    case RANGE_MONTH_SUNDAY:
    case RANGE_MONTH_MONDAY:
        //Set start to the first of the month
        start = truncate(focus, Calendar.MONTH);
        //Set end to the last of the month
        end = (Calendar) start.clone();
        end.add(Calendar.MONTH, 1);
        end.add(Calendar.DATE, -1);
        //Loop start back to the previous sunday or monday
        if (rangeStyle == RANGE_MONTH_MONDAY) {
            startCutoff = Calendar.MONDAY;
            endCutoff = Calendar.SUNDAY;
        }
        break;
    case RANGE_WEEK_SUNDAY:
    case RANGE_WEEK_MONDAY:
    case RANGE_WEEK_RELATIVE:
    case RANGE_WEEK_CENTER:
        //Set start and end to the current date
        start = truncate(focus, Calendar.DATE);
        end = truncate(focus, Calendar.DATE);
        switch (rangeStyle) {
        case RANGE_WEEK_SUNDAY:
            //already set by default
            break;
        case RANGE_WEEK_MONDAY:
            startCutoff = Calendar.MONDAY;
            endCutoff = Calendar.SUNDAY;
            break;
        case RANGE_WEEK_RELATIVE:
            startCutoff = focus.get(Calendar.DAY_OF_WEEK);
            endCutoff = startCutoff - 1;
            break;
        case RANGE_WEEK_CENTER:
            startCutoff = focus.get(Calendar.DAY_OF_WEEK) - 3;
            endCutoff = focus.get(Calendar.DAY_OF_WEEK) + 3;
            break;
        }
        break;
    default:
        throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid.");
    }
    if (startCutoff < Calendar.SUNDAY) {
        startCutoff += 7;
    }
    if (startCutoff > Calendar.SATURDAY) {
        startCutoff -= 7;
    }
    if (endCutoff < Calendar.SUNDAY) {
        endCutoff += 7;
    }
    if (endCutoff > Calendar.SATURDAY) {
        endCutoff -= 7;
    }
    while (start.get(Calendar.DAY_OF_WEEK) != startCutoff) {
        start.add(Calendar.DATE, -1);
    }
    while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) {
        end.add(Calendar.DATE, 1);
    }
    return new DateIterator(start, end);
}