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:org.davidmendoza.esu.web.EstudiaController.java

@SuppressWarnings("unchecked")
@RequestMapping(value = "/popular/{posicion}", method = RequestMethod.GET)
@ResponseBody//  w ww .j  a  va2 s .c  om
public Map popular(@PathVariable Integer posicion) throws ParseException {
    log.info("Populares Estudia({})", posicion);
    Map resultado = new HashMap();
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE", new Locale("es"));
    Popular popular = publicacionService.obtieneSiguientePopularEstudia(posicion);
    popular.getPublicacion().getArticulo()
            .setVistas(publicacionService.agregarVista(popular.getPublicacion().getArticulo()));
    Trimestre t = trimestreService
            .obtiene(popular.getPublicacion().getAnio() + popular.getPublicacion().getTrimestre());
    if (t != null) {
        Calendar cal = new GregorianCalendar(Locale.ENGLISH);
        cal.setTime(t.getInicia());
        cal.add(Calendar.SECOND, 1);
        cal.set(Calendar.DAY_OF_WEEK, obtieneDia(popular.getPublicacion().getDia()));
        NumberFormat nf = NumberFormat.getInstance();
        int weeks = ((Long) nf.parse(popular.getPublicacion().getLeccion().substring(1))).intValue();
        if (popular.getPublicacion().getDia().equals("sabado")) {
            weeks--;
        }
        cal.add(Calendar.WEEK_OF_YEAR, weeks);
        Date hoy = cal.getTime();
        resultado.put("dia", hoy);
        resultado.put("diaString", sdf.format(hoy));
    }
    resultado.put("publicacion", popular.getPublicacion());
    resultado.put("posicion", popular.getId());
    return resultado;
}

From source file:com.virtusa.akura.attendance.controller.DailyAttendanceController.java

/**
 * Method is to return date list.//  w w w.j  a v  a 2 s. co  m
 * 
 * @return current date - String
 * @throws AkuraAppException - throw SMSExeption.
 */
@ModelAttribute(MODEL_ATT_CURRENT_DATE)
public List<String> populateDateList() throws AkuraAppException {

    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    List<Holiday> holidayList = getHolidayList(String.valueOf(year));
    List<String> dateList = new ArrayList<String>();

    while (true) {
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SATURDAY) {
            calendar.add(Calendar.DATE, -1);

        } else if (dayOfWeek == Calendar.SUNDAY) {
            calendar.add(Calendar.DATE, CONSTANT_MINUS_TWO);

        } else {

            Date date = calendar.getTime();
            if (!DateUtil.isHoliday(holidayList, DateUtil.getParseDate(DateUtil.getFormatDate(date)))) {
                dateList.add(DateUtil.getFormatDate(date));
                calendar.add(Calendar.DATE, -1);
            } else {
                calendar.add(Calendar.DATE, -1);
            }

        }

        if (dateList.size() == CONSTANT_FIVE) {
            break;
        }

    }

    return dateList;
}

From source file:CalendarUtilsTest.java

/**
 * Tests the parse method, which is supposed to handle various strings
 * as flexibly as CVS supports.//w w  w .j av a  2 s  .c o  m
 */
public void testParse() throws Exception {
    //This is difficult to test since the "now" used in the
    //  parse function cannot be controlled.  We could possibly control
    //  it by trying before and after and making sure the value we expect
    //  is between the two values calculated.
    //For now we're just using the custom assertEquals that takes a delta

    Calendar now = null;

    now = Calendar.getInstance();
    now.add(Calendar.MINUTE, -1);
    assertEquals("parse 1 minute ago", now, CalendarUtils.parse("1 minute ago"), 50);
    now = Calendar.getInstance();
    now.add(Calendar.MINUTE, -8);
    assertEquals("parse 8 minutes ago", now, CalendarUtils.parse("8 minutes ago"), 50);

    now = Calendar.getInstance();
    now.add(Calendar.DATE, -1);
    assertEquals("parse yesterday", now, CalendarUtils.parse("yesterday"), 50);

    now = Calendar.getInstance();
    now.add(Calendar.DATE, 1);
    assertEquals("parse tomorrow", now, CalendarUtils.parse("tomorrow"), 50);

    now = Calendar.getInstance();
    //Sunday would be 1, Saturday would be 7, so we walk back up to 6 days.
    if (now.get(Calendar.DATE) == 1) {
        //If Sunday already, we go back a full week
        now.add(Calendar.DATE, -7);
    } else {
        now.add(Calendar.DATE, 1 - now.get(Calendar.DAY_OF_WEEK));
    }
    assertEquals("parse last Sunday", now, CalendarUtils.parse("last Sunday"), 50);

    now = Calendar.getInstance();
    now.add(Calendar.DATE, -7);
    assertEquals("parse last week", now, CalendarUtils.parse("last week"), 50);

    now = Calendar.getInstance();
    //January would be 0, December would be 11, so we walk back up to 11 months
    if (now.get(Calendar.MONTH) == 0) {
        //If January already, we go back a full year
        now.add(Calendar.MONTH, -12);
    } else {
        now.add(Calendar.MONTH, 0 - now.get(Calendar.MONTH));
    }
    assertEquals("parse last January", now, CalendarUtils.parse("last January"), 50);
}

From source file:GenAppStoreSales.java

/**
 * Starting point for the demonstration application.
 * @throws IOException // w ww  . ja  v a  2 s. co m
 * 
 */
public static void main(String[] args) throws IOException {
    System.out.print("\nRefreshing Apple Store Reports...");

    // Init Calendars
    rollCalendar = Calendar.getInstance();
    updateRollCalendar();
    currentYear = rollCalendar.get(Calendar.YEAR);
    currentMonth = rollCalendar.get(Calendar.MONTH) + 1;
    currentDay = rollCalendar.get(Calendar.DATE);
    Calendar launchCalendar = Calendar.getInstance();
    int launchYear = 2013, launchMonth = 2, launchDay = 28; //Month from 0..11
    launchCalendar.set(launchYear, launchMonth, launchDay);
    /* Report Folders
     */
    currentPath = new File("").getAbsolutePath();
    String sourceName = "/sources";
    File sourceDir = new File(currentPath, sourceName);
    if (!sourceDir.isDirectory()) {
        if (!(new File(currentPath + sourceName)).mkdirs()) {
            System.out.println("[Error] Couldn't create 'source' folder.");
        }
    }
    sourcePath = sourceDir.getAbsolutePath();

    String chartName = "/charts";
    File chartDir = new File(currentPath, chartName);
    if (!chartDir.isDirectory()) {
        if (!(new File(currentPath + chartName)).mkdirs()) {
            System.out.println("[Error] Couldn't create 'chart' folder.");
        }
    }
    chartPath = chartDir.getAbsolutePath();

    String dateCode, reportName;

    // DAILY REPORT
    System.out.println("\n-> Daily reports");
    for (int d = 0; d < 14; d++) {
        rollCalendar.add(Calendar.DATE, -1);
        if (rollCalendar.compareTo(launchCalendar) <= 0)
            break;
        updateRollCalendar();
        dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
        reportName = "S_D_" + appID + "_" + dateCode + ".txt";
        autoingestionDownload(reportName, "Daily", dateCode);
    }
    printDatePeriod("DAILY", "report");

    // WEEKLY REPORT
    System.out.println("\n-> Weekly reports");
    rollCalendar = Calendar.getInstance();
    rollCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    pursuedDay = currentDay;
    pursuedMonth = currentMonth;
    pursuedYear = currentYear;
    for (int w = 0; w < 13; w++) {
        rollCalendar.add(Calendar.DATE, -7);
        if (rollCalendar.compareTo(launchCalendar) <= 0)
            break;
        updateRollCalendar();
        dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
        reportName = "S_W_" + appID + "_" + dateCode + ".txt";
        autoingestionDownload(reportName, "Weekly", dateCode);
    }
    printDatePeriod("WEEKLY", "report");

    // MONTHLY REPORTS
    System.out.println("\n-> Monthly reports");
    rollCalendar = Calendar.getInstance();
    pursuedDay = currentDay;
    pursuedMonth = currentMonth - 1;
    pursuedYear = currentYear;
    for (int m = 0; m < 12; m++) {
        rollCalendar.add(Calendar.MONTH, -1);
        rollCalendar.set(Calendar.DATE, rollCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        if (rollCalendar.compareTo(launchCalendar) <= 0)
            break;
        updateRollCalendar();
        dateCode = String.format("%d%02d", pursuedYear, pursuedMonth);
        reportName = "S_M_" + appID + "_" + dateCode + ".txt";
        autoingestionDownload(reportName, "Monthly", dateCode);
    }
    printDatePeriod("MONTHLY", "report");

    // YEARLY REPORTS
    System.out.println("\n-> Yearly reports");
    rollCalendar = Calendar.getInstance();
    rollCalendar.add(Calendar.DATE, -1);
    pursuedDay = currentDay - 1;
    pursuedMonth = currentMonth;
    pursuedYear = currentYear;
    for (int y = 0; y < 100; y++) {
        rollCalendar.add(Calendar.YEAR, -1);
        if (rollCalendar.compareTo(launchCalendar) <= 0)
            break;
        updateRollCalendar();
        dateCode = String.format("%d", pursuedYear);
        reportName = "S_Y_" + appID + "_" + dateCode + ".txt";
        autoingestionDownload(reportName, "Yearly", dateCode);
    }
    printDatePeriod("YEARLY", "report");

    /**
     * Reading Sales.txt & Generating Charts
     */
    // WEEK CHARTS
    String plotName, pursuedPeriodDate;
    System.out.print("\nRestoring charts...\n");
    System.out.println("-> Week charts");
    rollCalendar = Calendar.getInstance();
    rollCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    rollCalendar.add(Calendar.DATE, -7);
    updateRollCalendar();
    while (rollCalendar.compareTo(launchCalendar) > 0) {
        pursuedPeriodDate = String.format("%d.%d.%d", pursuedDay, pursuedMonth, pursuedYear);
        dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
        plotName = "S_W_" + appID + "_" + dateCode;
        File plotFile = new File(chartPath + "/" + plotName + ".png");
        if (!plotFile.isFile()) {
            readSales("week", true, true);
            if (countryLabels.size() > 0) {
                genPlot("WEEK", plotName, pursuedPeriodDate, countryLabels, countryUnits);
            }
            if (countryInAppLabels.size() > 0) {
                genPlot("WEEK IN-APP", plotName + "_InApp", pursuedPeriodDate, countryInAppLabels,
                        countryInAppUnits);
            }
        } else
            readSales("week", false, true);// Week already plotted
    }
    printDatePeriod("WEEK", "charts");

    // Incomplete current Week (let the current day be computed)
    rollCalendar = Calendar.getInstance();
    updateRollCalendar();
    readSales("week", false, true);
    // MONTH CHARTS
    System.out.println("\n-> Month charts");
    rollCalendar = Calendar.getInstance();
    rollCalendar.add(Calendar.MONTH, -1);
    rollCalendar.set(Calendar.DATE, rollCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    updateRollCalendar();
    while (rollCalendar.compareTo(launchCalendar) > 0) {
        pursuedPeriodDate = String.format("%d.%d.%d", pursuedDay, pursuedMonth, pursuedYear);
        dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
        plotName = "S_M_" + appID + "_" + dateCode;
        File plotFile = new File(chartPath + "/" + plotName + ".png");
        if (!plotFile.isFile()) {
            readSales("month", true, false);
            if (countryLabels.size() > 0) {
                genPlot("MONTH", plotName, pursuedPeriodDate, countryLabels, countryUnits);
            }
            if (countryInAppLabels.size() > 0) {
                genPlot("MONTH", plotName + "_InApp", pursuedPeriodDate, countryInAppLabels, countryInAppUnits);
            }
        } else {
            readSales("month", false, false);
        }

    }
    printDatePeriod("MONTH", "charts");

    // YEAR CHARTS
    System.out.println("\n-> Year charts");
    rollCalendar = (Calendar) launchCalendar.clone();
    rollCalendar.set(Calendar.DATE, -1);
    rollCalendar.add(Calendar.YEAR, -1);
    updateRollCalendar();
    while (rollCalendar.compareTo(launchCalendar) > 0) {
        pursuedPeriodDate = String.format("%d.%d.%d", pursuedDay, pursuedMonth, pursuedYear);
        dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
        plotName = "S_Y_" + appID + "_" + dateCode;
        File plotFile = new File(chartPath + "/" + plotName + ".png");
        if (!plotFile.isFile()) {
            readSales("year", true, false);
            if (countryLabels.size() > 0) {
                genPlot("YEAR", plotName, pursuedPeriodDate, countryLabels, countryUnits);
            }
            if (countryInAppLabels.size() > 0) {
                genPlot("YEAR", plotName + "_InApp", pursuedPeriodDate, countryInAppLabels, countryInAppUnits);
            }
        } else
            readSales("year", false, false);
    }
    printDatePeriod("YEAR", "charts");

    // CUSTOM CHART PERIOD
    System.out.println("\n-> Custom charts");
    customCalendar = (Calendar) launchCalendar.clone(); // begin day
    rollCalendar = Calendar.getInstance(); // end day
    rollCalendar.add(Calendar.DATE, -1);
    updateRollCalendar();
    pursuedPeriodDate = String.format("%d.%d.%d", pursuedDay, pursuedMonth, pursuedYear);
    dateCode = String.format("%d%02d%02d", pursuedYear, pursuedMonth, pursuedDay);
    plotName = "S_C_" + appID + "__whole";// + dateCode;
    File plotFile = new File(chartPath + "/" + plotName + ".png");
    if (!plotFile.isFile()) {
        readSales("custom", true, false);
        if (countryLabels.size() > 0) {
            genPlot("CUSTOM", plotName, pursuedPeriodDate, countryLabels, countryUnits);
        }
        if (countryInAppLabels.size() > 0) {
            genPlot("CUSTOM IN-APP", plotName + "_InApp", pursuedPeriodDate, countryInAppLabels,
                    countryInAppUnits);
        }
    }
    printDatePeriod("CUSTOM Period", "charts");

    // Day Sales units
    rollCalendar = Calendar.getInstance();
    rollCalendar.add(Calendar.DATE, -1);
    updateRollCalendar();
    readSales("day", false, false);

    System.out.println("\nTotal units: " + totalUnits + "/" + totalUpdateUnits + "-Up (+" + dayUnits + ")");
    System.out.println("Total IN-APP units: " + totalInAppUnits + " (+" + dayINAPPUnits + ")\n");
    System.exit(0);
}

From source file:com.kongwu.insweb.utils.DateUtils.java

/***
 * /*from ww w . ja  v a 2 s.  c om*/
 * @Title: getMonday
 * @param date
 * @throws
 */
public static Date getMonday(Date date) {
    Calendar cal = date2Calendar(date);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return cal.getTime();
}

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

/**
 * date ?() <code>23:59:59.999</code> <br>
 * ?:,,?<br>//from   ww  w  .j a va  2s .  c  om
 * ?:,?
 * 
 * <pre>
 *   2012-10-11 17:10:30.701 (),
 * return 2012-10-13 23:59:59.999
 * 
 * 
 * getLastDateOfThisWeek(2014-12-31 05:00:00)
 * return 2015-01-03 23:59:59.999
 * </pre>
 * 
 * @param date
 *            ?date
 * @return date ? <code>23:59:59.999</code> 
 * @see #toCalendar(Date)
 * @see Calendar#set(int, int)
 * @see #dayEnd(Calendar)
 * @see Calendar#getTime()
 * @since 1.0.1
 */
public static final Date getLastDateOfThisWeek(Date date) {
    Calendar calendar = toCalendar(date);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    dayEnd(calendar);
    return calendar.getTime();
}

From source file:com.xumpy.timesheets.services.implementations.JobsSrvImpl.java

public static boolean jobInWeekend(Jobs jobs) {
    Date date = jobs.getJobDate();

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//w  w  w. ja v a2s . com

    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
            || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
        return true;
    }

    return false;
}

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

private boolean isWeekend(Calendar calendar) {
    int dow = calendar.get(Calendar.DAY_OF_WEEK);
    return dow == Calendar.SATURDAY || dow == Calendar.SUNDAY;
}

From source file:Cal.java

/** Compute which days to put where, in the Cal panel */
protected void recompute() {
    // System.out.println("Cal::recompute: " + yy + ":" + mm + ":" + dd);
    if (mm < 0 || mm > 11)
        throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");
    clearDayActive();//from   w w w .  ja v  a  2  s  .c o m
    calendar = new GregorianCalendar(yy, mm, dd);

    // Compute how much to leave before the first.
    // getDay() returns 0 for Sunday, which is just right.
    leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1;
    // System.out.println("leadGap = " + leadGap);

    int daysInMonth = dom[mm];
    if (isLeap(calendar.get(Calendar.YEAR)) && mm == 1)
        //      if (isLeap(calendar.get(Calendar.YEAR)) && mm > 1)
        ++daysInMonth;

    // Blank out the labels before 1st day of month
    for (int i = 0; i < leadGap; i++) {
        labs[0][i].setText("");
    }

    // Fill in numbers for the day of month.
    for (int i = 1; i <= daysInMonth; i++) {
        JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
        b.setText(Integer.toString(i));
    }

    // 7 days/week * up to 6 rows
    for (int i = leadGap + 1 + daysInMonth; i < 6 * 7; i++) {
        labs[(i) / 7][(i) % 7].setText("");
    }

    // Shade current day, only if current month
    if (thisYear == yy && mm == thisMonth)
        setDayActive(dd); // shade the box for today

    // Say we need to be drawn on the screen
    repaint();
}

From source file:DateUtil.java

/**
 * Returns the milliseconds until the next Period, as based on the difference
 * between the current cursor and the Period. If the current cursor is at the
 * start of a Period, ignoring milliseconds, 0 is returned.
 * /*from www .  ja  v  a2 s.c o m*/
 * @param u
 * @return milliseconds until next period.
 */
public long msToNextPeriod(Period u) {
    long ms = 0;

    switch (u) {
    case YEAR:
        mCal.set(mCursor.mYear + 1, 0, 0, 0, 0, 0);
        ms = mCal.getTimeInMillis() - mCursor.mMillis;
        if (ms == YEAR_MS)
            ms = 0;
        break;
    case QUARTER:
        if (mCursor.mMonth >= 9)
            mCal.set(mCursor.mYear + 1, 0, 0, 0, 0, 0);
        else if (mCursor.mMonth >= 6)
            mCal.set(mCursor.mYear, 9, 0, 0, 0, 0);
        else if (mCursor.mMonth >= 3)
            mCal.set(mCursor.mYear, 6, 0, 0, 0, 0);
        else
            mCal.set(mCursor.mYear, 3, 0, 0, 0, 0);
        ms = mCal.getTimeInMillis() - mCursor.mMillis;
        if (ms == QUARTER_MS)
            ms = 0;
        break;
    case MONTH:
        if (mCursor.mMonth == 11)
            mCal.set(mCursor.mYear + 1, 0, 0, 0, 0, 0);
        else
            mCal.set(mCursor.mYear, mCursor.mMonth + 1, 0, 0, 0, 0);
        ms = mCal.getTimeInMillis() - mCursor.mMillis;
        if (ms == MONTH_MS)
            ms = 0;
        break;
    case WEEK:
        mCal.setTimeInMillis(mCursor.mMillis);

        int first = mCal.getFirstDayOfWeek();

        mCal.add(Calendar.WEEK_OF_YEAR, 1);
        mCal.set(Calendar.DAY_OF_WEEK, first);
        mCal.set(Calendar.HOUR_OF_DAY, 0);
        mCal.set(Calendar.MINUTE, 0);
        mCal.set(Calendar.SECOND, 0);
        mCal.set(Calendar.MILLISECOND, 0);
        ms = mCal.getTimeInMillis() - mCursor.mMillis;
        if (ms == WEEK_MS)
            ms = 0;
        break;
    case DAY:
        if (mCursor.mMinute == 0 && mCursor.mHour == 0)
            return 0;
        ms = ((60 - mCursor.mMinute) + (60 * (24 - mCursor.mHour))) * MINUTE_MS;
        break;
    case AMPM:
        if (mCursor.mMinute == 0 && (mCursor.mHour == 0 || mCursor.mHour == 12))
            return 0;
        ms = ((60 - mCursor.mMinute) + (60 * (24 - (mCursor.mHour % 12)))) * MINUTE_MS;
        break;
    case HOUR:
        if (mCursor.mMinute == 0)
            return 0;
        ms = (60 - mCursor.mMinute) * MINUTE_MS;
        break;
    case MINUTE:
    default:
        if (mCursor.mSecond == 0)
            return 0;
        ms = (60 - mCursor.mSecond) * SECOND_MS;
        break;
    }

    return ms;
}