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:jp.co.golorp.emarf.util.DateUtil.java

/**
 * @param y//w  w  w.jav  a2  s .  c  o m
 *            
 * @param m
 *            
 * @param d
 *            
 * @return 1:, 2:, 3:?, 4:, 5:, 6:, 7:
 */
public static int getDayOfWeek(final int y, final int m, final int d) {
    Calendar now = Calendar.getInstance();
    now.set(Calendar.YEAR, y);
    now.set(Calendar.MONTH, m - 1);
    now.set(Calendar.DATE, d);
    int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    return dayOfWeek;
}

From source file:course1778.mobileapp.safeMedicare.Main.FamMemFrag.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fam_mem_main_frag, container, false);

    TextView date = (TextView) view.findViewById(R.id.date);
    //listView = (ListView) view.findViewById(R.id.listView);
    //ListView listView = (ListView) view.findViewById(R.id.list_view);
    Calendar c = Calendar.getInstance();
    String day, month;/*from  w  w  w.  ja  v  a  2  s .c  om*/

    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            add();
        }
    });

    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
        day = "Sunday";
    } else if (c.get(Calendar.DAY_OF_WEEK) == 2) {
        day = "Monday";
    } else if (c.get(Calendar.DAY_OF_WEEK) == 3) {
        day = "Tuesday";
    } else if (c.get(Calendar.DAY_OF_WEEK) == 4) {
        day = "Wednesday";
    } else if (c.get(Calendar.DAY_OF_WEEK) == 5) {
        day = "Thursday";
    } else if (c.get(Calendar.DAY_OF_WEEK) == 6) {
        day = "Friday";
    } else {
        day = "Saturday";
    }

    if (c.get(Calendar.MONTH) == 0) {
        month = "January";
    } else if (c.get(Calendar.MONTH) == 1) {
        month = "February";
    } else if (c.get(Calendar.MONTH) == 2) {
        month = "March";
    } else if (c.get(Calendar.MONTH) == 3) {
        month = "April";
    } else if (c.get(Calendar.MONTH) == 4) {
        month = "May";
    } else if (c.get(Calendar.MONTH) == 5) {
        month = "June";
    } else if (c.get(Calendar.MONTH) == 6) {
        month = "July";
    } else if (c.get(Calendar.MONTH) == 7) {
        month = "August";
    } else if (c.get(Calendar.MONTH) == 8) {
        month = "September";
    } else if (c.get(Calendar.MONTH) == 9) {
        month = "October";
    } else if (c.get(Calendar.MONTH) == 10) {
        month = "November";
    } else {
        month = "December";
    }

    String sDate = day + ",  " + month + "  " + c.get(Calendar.DAY_OF_MONTH);
    date.setText(sDate);

    //        SimpleCursorAdapter adapter =
    //                new SimpleCursorAdapter(getActivity(), R.layout.fam_mem_frag,
    //                        current, new String[]{
    //                        DatabaseHelper.TITLE,
    //                        //String.format(format, DatabaseHelper.TIME_H),
    //                        DatabaseHelper.TIME_H,
    //                        //String.format(format, DatabaseHelper.TIME_M)},
    //                        DatabaseHelper.TIME_M},
    //                        new int[]{R.id.title, R.id.time_h, R.id.time_m},
    //                        0);
    //
    //        listView.setAdapter(adapter);
    //
    //        if (current == null) {
    //            db = new DatabaseHelper(getActivity());
    //            task = new LoadCursorTask().execute();
    //        }
    //
    //        // onBackPress key listener
    //        view.setFocusableInTouchMode(true);
    //        view.requestFocus();
    //        view.setOnKeyListener(new View.OnKeyListener() {
    //            @Override
    //            public boolean onKey(View v, int keyCode, KeyEvent event) {
    //                if (keyCode == KeyEvent.KEYCODE_BACK) {
    //                    Intent intent = new Intent(getActivity().getApplicationContext(), WelcomePage.class);
    //                    startActivity(intent);
    //                    return true;
    //                } else {
    //                    return false;
    //                }
    //            }
    //        });
    return view;
}

From source file:org.activequant.util.charting.IntradayMarketTimeline.java

/**
 * Translates a millisecond (as defined by java.util.Date) into an index
 * along this timeline./* w w  w.  j  av a  2  s . co m*/
 *
 * @param target - the milliseconds of a date to convert
 *
 * @return A timeline value.
 */
public long toTimelineValue(long currentDayMillis) {

    //
    //find out the day of the week the current day is
    //SUNDAY = 1,...SATURDAY = 7
    Calendar currentDay = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    currentDay.setTimeInMillis(currentDayMillis);

    //a list of the days from the current date until last thursday 00:00
    //which includes thursday itself from (00:00,23:59)
    ArrayList<Integer> daysFromThursday = new ArrayList<Integer>();

    Calendar lastThursday = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

    lastThursday.setTimeInMillis(currentDayMillis);

    //move backwards in time until you hit a wednesday
    while (lastThursday.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
        //add this day to the list
        daysFromThursday.add(new Integer(lastThursday.get(Calendar.DAY_OF_WEEK)));
        //
        //move back one more day
        lastThursday.add(Calendar.DATE, -1);
    }
    //
    //add thursday to the list
    daysFromThursday.add(new Integer(Calendar.THURSDAY));

    //adjust Calendar to the beginning of last thursday
    lastThursday.set(Calendar.MILLISECOND, 0);
    lastThursday.set(Calendar.SECOND, 0);
    lastThursday.set(Calendar.MINUTE, 0);
    lastThursday.set(Calendar.HOUR_OF_DAY, 0);

    //get the milliseconds for the beginning of last Thursday
    long lastThursdayMillis = lastThursday.getTimeInMillis();

    //because Jan 1, 1970 was a Thursday, lastThursdayMillis
    //gives an even # of weeks from Jan 1, 1970 until lastThursdayMillis. 
    //so subtract the (down time per week * the
    //number of weeks) since Jan 1, 1970

    //the number of weeks since Jan 1, 1970
    int numberOfWeeks = (int) Math.round((new Long(lastThursdayMillis / MILLIS_PER_WEEK)).doubleValue());
    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

    TimeZone.setDefault(TimeZone.getDefault());

    //get the timeline value for the number of millis since
    //Jan 1, 1970 to last thursday, by multiplying the number of weeks
    //since Jan 1, 1970 by the amount of active milliseconds per week
    long timelineValue = numberOfWeeks * this.activeTimePerWeek;
    //          
    //add the amount of active millseconds for the current day
    //of the given time
    long millisOfCurrentDay = currentDay.get(Calendar.HOUR_OF_DAY) * 3600000
            + currentDay.get(Calendar.MINUTE) * 60000 + currentDay.get(Calendar.SECOND) * 1000
            + currentDay.get(Calendar.MILLISECOND);

    long startOfCurrentDay = this.getStartTime(currentDay.get(Calendar.DAY_OF_WEEK));
    long endOfCurrentDay = this.getEndTime(currentDay.get(Calendar.DAY_OF_WEEK));

    if (millisOfCurrentDay >= startOfCurrentDay && millisOfCurrentDay <= endOfCurrentDay) {

        timelineValue += millisOfCurrentDay - startOfCurrentDay;
    }

    //get the size of the list
    int listSize = daysFromThursday.size();

    //add the active time since last thursday, skipping the first day since
    //we already took care of that
    for (int i = 1; i < listSize; i++) {
        int day = ((Integer) daysFromThursday.get(i)).intValue();

        timelineValue += this.getActiveTimePerDay(day);

    }

    return timelineValue;
}

From source file:br.com.transport.report.ManagerReportBean.java

/**
 * Retorna o primeiro dia da semana// ww  w  .  j av  a2s.  c o m
 * @return
 */
private Calendar getInitCalendar() {

    Calendar init = GregorianCalendar.getInstance();
    init.setFirstDayOfWeek(Calendar.SUNDAY);
    init.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    init.set(Calendar.HOUR_OF_DAY, 0);
    init.set(Calendar.MINUTE, 0);
    init.set(Calendar.SECOND, 0);
    init.setTime(init.getTime());

    return init;
}

From source file:fr.paris.lutece.plugins.workflow.modules.appointment.web.ExecuteWorkflowAction.java

/**
 * Do check if the given key is valid. If it is valid, then the user is
 * redirected to a page to execute a workflow action.
 * @param request The request/*from w w  w.  j a  v  a2s.c o  m*/
 * @param response The response
 * @return The next URL to redirect to
 * @throws SiteMessageException If a site message needs to be displayed
 */
public String doExecuteWorkflowAction(HttpServletRequest request, HttpServletResponse response)
        throws SiteMessageException {
    String strIdAction = request.getParameter(PARAMETER_ID_ACTION);
    String strIdAdminUser = request.getParameter(PARAMETER_ID_ADMIN_USER);
    String strIdResource = request.getParameter(PARAMETER_ID_RESOURCE);
    String strTimestamp = request.getParameter(PARAMETER_TIMESTAMP);
    String strKey = request.getParameter(PARAMETER_KEY);

    if (StringUtils.isNotEmpty(strIdAction) && StringUtils.isNumeric(strIdAction)
            && StringUtils.isNotEmpty(strTimestamp) && StringUtils.isNumeric(strTimestamp)
            && StringUtils.isNotEmpty(strIdResource) && StringUtils.isNumeric(strIdResource)
            && StringUtils.isNotEmpty(strKey) && StringUtils.isNotEmpty(strIdAdminUser)
            && StringUtils.isNumeric(strIdAdminUser)) {
        int nIdAction = Integer.parseInt(strIdAction);

        long lTimestamp = Long.parseLong(strTimestamp);
        int nIdResource = Integer.parseInt(strIdResource);

        AdminUser user = null;
        int nIdAdminUser = Integer.parseInt(strIdAdminUser);

        if (nIdAdminUser > 0) {
            user = AdminUserHome.findByPrimaryKey(nIdAdminUser);
        }

        int nLinkLimitValidity = AppPropertiesService.getPropertyInt(PROPERTY_LINKS_LIMIT_VALIDITY,
                DEFAULT_LIMIT_TIME_VALIDITY);

        if (nLinkLimitValidity > 0) {
            Calendar calendar = GregorianCalendar
                    .getInstance(WorkflowAppointmentPlugin.getPluginLocale(Locale.getDefault()));
            calendar.add(Calendar.DAY_OF_WEEK, -1 * nLinkLimitValidity);

            if (calendar.getTimeInMillis() > lTimestamp) {
                SiteMessageService.setMessage(request, ERROR_MESSAGE_ACCESS_DENIED, SiteMessage.TYPE_ERROR);

                return null;
            }
        }

        String strComputedKey = computeAuthenticationKey(nIdAction, nIdAdminUser, lTimestamp, nIdResource);

        if ((user == null) || !StringUtils.equals(strComputedKey, strKey)) {
            SiteMessageService.setMessage(request, ERROR_MESSAGE_ACCESS_DENIED, SiteMessage.TYPE_ERROR);

            return null;
        }

        try {
            AdminAuthenticationService.getInstance().registerUser(request, user);
        } catch (AccessDeniedException e) {
            AppLogService.error(e.getMessage(), e);
        } catch (UserNotSignedException e) {
            AppLogService.error(e.getMessage(), e);
        }

        return AppointmentJspBean.getUrlExecuteWorkflowAction(request, strIdResource, strIdAction);
    }

    SiteMessageService.setMessage(request, ERROR_MESSAGE_ACCESS_DENIED, SiteMessage.TYPE_ERROR);

    return null;
}

From source file:Holidays.java

public static java.util.Calendar ColumbusDayObserved(int nYear) {
    // Second Monday in October
    int nX;/*www  .j  a v  a 2 s .c  o  m*/
    int nMonth = 9; // October 
    java.util.Calendar cal;

    cal = java.util.Calendar.getInstance();
    cal.set(nYear, nMonth, 1);
    nX = cal.get(Calendar.DAY_OF_WEEK);
    switch (nX) {
    case 0: {// Sunday
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 9);
        return cal;
    }
    case 1: {// Monday
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 15);
        return cal;
    }
    case 2: // Tuesday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 14);
        return cal;
    }
    case 3: // Wednesday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 13);
        return cal;
    }
    case 4: // Thrusday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 12);
        return cal;
    }
    case 5: // Friday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 11);
        return cal;
    }
    default: // Saturday
    {
        cal = java.util.Calendar.getInstance();
        cal.set(nYear, nMonth, 10);
        return cal;
    }
    }

}

From source file:com.philliphsu.clock2.alarms.Alarm.java

public long ringsAt() {
    // Always with respect to the current date and time
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, hour());
    calendar.set(Calendar.MINUTE, minutes());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    long baseRingTime = calendar.getTimeInMillis();

    if (!hasRecurrence()) {
        if (baseRingTime <= System.currentTimeMillis()) {
            // The specified time has passed for today
            baseRingTime += TimeUnit.DAYS.toMillis(1);
        }/*from   w w w  .  j av  a2s.  c o  m*/
        return baseRingTime;
    } else {
        // Compute the ring time just for the next closest recurring day.
        // Remember that day constants defined in the Calendar class are
        // not zero-based like ours, so we have to compensate with an offset
        // of magnitude one, with the appropriate sign based on the situation.
        int weekdayToday = calendar.get(Calendar.DAY_OF_WEEK);
        int numDaysFromToday = -1;

        for (int i = weekdayToday; i <= Calendar.SATURDAY; i++) {
            if (isRecurring(i - 1 /*match up with our day constant*/)) {
                if (i == weekdayToday) {
                    if (baseRingTime > System.currentTimeMillis()) {
                        // The normal ring time has not passed yet
                        numDaysFromToday = 0;
                        break;
                    }
                } else {
                    numDaysFromToday = i - weekdayToday;
                    break;
                }
            }
        }

        // Not computed yet
        if (numDaysFromToday < 0) {
            for (int i = Calendar.SUNDAY; i < weekdayToday; i++) {
                if (isRecurring(i - 1 /*match up with our day constant*/)) {
                    numDaysFromToday = Calendar.SATURDAY - weekdayToday + i;
                    break;
                }
            }
        }

        // Still not computed yet. The only recurring day is weekdayToday,
        // and its normal ring time has already passed.
        if (numDaysFromToday < 0 && isRecurring(weekdayToday - 1)
                && baseRingTime <= System.currentTimeMillis()) {
            numDaysFromToday = 7;
        }

        if (numDaysFromToday < 0)
            throw new IllegalStateException("How did we get here?");

        return baseRingTime + TimeUnit.DAYS.toMillis(numDaysFromToday);
    }
}

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

public TreeMap<Date, SynchronizedDescriptiveStatistics> getWeeklySummaryStatsAbs(Date startDate, Date endDate,
        Long hospitalser, String filter, boolean includeWeekends, boolean ptflag, boolean scheduledFlag) {
    Calendar cal = new GregorianCalendar();
    TreeMap<Date, SynchronizedDescriptiveStatistics> retval = new TreeMap<>();

    //SET TO BEGINNING OF WK FOR ABSOLUTE CALC
    cal.setTime(startDate);//from   ww w  .  jav  a 2 s. c  om
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    startDate = cal.getTime();

    List<Object[]> events = getDailyCounts(startDate, endDate, hospitalser, filter, false, ptflag,
            scheduledFlag);

    DateFormat df = new SimpleDateFormat("MM/dd/yy");
    cal.setTime(startDate);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    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 = "";
    String prevYrWk = currYrWk;
    SynchronizedDescriptiveStatistics currStats = new SynchronizedDescriptiveStatistics();
    int i = 0;
    while (cal.getTime().before(endDate) && i < events.size()) {

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

        cal.setTime(d);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        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))) {
            GregorianCalendar lastMon = new GregorianCalendar();
            lastMon.setTime(cal.getTime());
            lastMon.add(Calendar.DATE, -7);
            retval.put(lastMon.getTime(), currStats);
            currStats = new SynchronizedDescriptiveStatistics();
        }

        prevYrWk = currYrWk;

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

    return retval;
}

From source file:com.livinglogic.ul4.FunctionFormat.java

public static String call(Date obj, String formatString, Locale locale) {
    if (locale == null)
        locale = Locale.ENGLISH;//from  w w w.j a  va  2  s  .com
    Calendar calendar = new GregorianCalendar();
    calendar.setTime((Date) obj);
    StringBuilder buffer = new StringBuilder();
    boolean escapeCharacterFound = false;
    int formatStringLength = formatString.length();
    for (int i = 0; i < formatStringLength; i++) {
        char c = formatString.charAt(i);
        if (escapeCharacterFound) {
            switch (c) {
            case 'a':
                buffer.append(new SimpleDateFormat("EE", locale).format(obj));
                break;
            case 'A':
                buffer.append(new SimpleDateFormat("EEEE", locale).format(obj));
                break;
            case 'b':
                buffer.append(new SimpleDateFormat("MMM", locale).format(obj));
                break;
            case 'B':
                buffer.append(new SimpleDateFormat("MMMM", locale).format(obj));
                break;
            case 'c': {
                String format = cFormats.get(locale.getLanguage());
                if (format == null)
                    format = cFormats.get("en");
                buffer.append(call(obj, format, locale));
                break;
            }
            case 'd':
                buffer.append(twodigits.format(calendar.get(Calendar.DAY_OF_MONTH)));
                break;
            case 'f':
                buffer.append(sixdigits.format(calendar.get(Calendar.MILLISECOND) * 1000));
                break;
            case 'H':
                buffer.append(twodigits.format(calendar.get(Calendar.HOUR_OF_DAY)));
                break;
            case 'I':
                buffer.append(twodigits.format(((calendar.get(Calendar.HOUR_OF_DAY) - 1) % 12) + 1));
                break;
            case 'j':
                buffer.append(threedigits.format(calendar.get(Calendar.DAY_OF_YEAR)));
                break;
            case 'm':
                buffer.append(twodigits.format(calendar.get(Calendar.MONTH) + 1));
                break;
            case 'M':
                buffer.append(twodigits.format(calendar.get(Calendar.MINUTE)));
                break;
            case 'p':
                buffer.append(new SimpleDateFormat("aa", locale).format(obj));
                break;
            case 'S':
                buffer.append(twodigits.format(calendar.get(Calendar.SECOND)));
                break;
            case 'U':
                buffer.append(twodigits.format(BoundDateMethodWeek.call(obj, 6)));
                break;
            case 'w':
                buffer.append(weekdayFormats.get(calendar.get(Calendar.DAY_OF_WEEK)));
                break;
            case 'W':
                buffer.append(twodigits.format(BoundDateMethodWeek.call(obj, 0)));
                break;
            case 'x': {
                String format = xFormats.get(locale.getLanguage());
                if (format == null)
                    format = xFormats.get("en");
                buffer.append(call(obj, format, locale));
                break;
            }
            case 'X': {
                String format = XFormats.get(locale.getLanguage());
                if (format == null)
                    format = XFormats.get("en");
                buffer.append(call(obj, format, locale));
                break;
            }
            case 'y':
                buffer.append(twodigits.format(calendar.get(Calendar.YEAR) % 100));
                break;
            case 'Y':
                buffer.append(fourdigits.format(calendar.get(Calendar.YEAR)));
                break;
            default:
                buffer.append(c);
                break;
            }
            escapeCharacterFound = false;
        } else {
            if (c == '%')
                escapeCharacterFound = true;
            else
                buffer.append(c);

        }
    }
    if (escapeCharacterFound)
        buffer.append('%');
    return buffer.toString();
}

From source file:jp.co.golorp.emarf.util.DateUtil.java

/**
 * @return 1:, 2:, 3:?, 4:, 5:, 6:, 7:/* w ww  .ja va  2  s . c  o  m*/
 */
public static int getDayOfWeek() {
    Calendar now = Calendar.getInstance();
    now.setTime(DateUtil.getDate());
    int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    return dayOfWeek;
}