Example usage for java.util GregorianCalendar setFirstDayOfWeek

List of usage examples for java.util GregorianCalendar setFirstDayOfWeek

Introduction

In this page you can find the example usage for java.util GregorianCalendar setFirstDayOfWeek.

Prototype

public void setFirstDayOfWeek(int value) 

Source Link

Document

Sets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

Usage

From source file:dao.Graficos.java

public static Date dia_inicial_final_semana(boolean isPrimeiro) {
    //Seta a data atual.
    Date dataAtual = new Date();
    GregorianCalendar calendar = new GregorianCalendar();
    //Define que a semana comea no domingo.
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    //Define a data atual.
    calendar.setTime(dataAtual);//from ww  w. j  a v a  2  s  .  c  o m
    if (isPrimeiro) {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    } else {
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    }
    return calendar.getTime();
}

From source file:eionet.util.Util.java

/**
 * A method for calculating time difference in MILLISECONDS, between a date-time specified in input parameters and the current
 * date-time. <BR>//  ww  w  . ja  v  a 2  s  .  co m
 * This should be useful for calculating sleep time for code that has a certain schedule for execution.
 *
 * @param hour
 *            An integer from 0 to 23. If less than 0 or more than 23, then the closest next hour to current hour is taken.
 * @param date
 *            An integer from 1 to 31. If less than 1 or more than 31, then the closest next date to current date is taken.
 * @param month
 *            An integer from Calendar.JANUARY to Calendar.DECEMBER. If out of those bounds, the closest next month to current
 *            month is taken.
 * @param wday
 *            An integer from 1 to 7. If out of those bounds, the closest next weekday to weekday month is taken.
 * @param zone
 *            A String specifying the time-zone in which the calculations should be done. Please see Java documentation an
 *            allowable time-zones and formats.
 * @return Time difference in milliseconds.
 */
public static long timeDiff(int hour, int date, int month, int wday, String zone) {

    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zone));
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    cal.setFirstDayOfWeek(Calendar.MONDAY);

    /*
     * here we force the hour to be one of the defualts if (hour < 0) hour = 0; if (hour > 23) hour = 23;
     */
    int cur_hour = cal.get(Calendar.HOUR);

    if (cal.get(Calendar.AM_PM) == Calendar.PM) {
        cur_hour = 12 + cur_hour;
    }

    // here we assume that every full hour is accepted
    /*
     * if (hour < 0 || hour > 23) { hour = cur_hour>=23 ? 0 : cur_hour + 1; }
     */

    if (wday >= 1 && wday <= 7) {

        int cur_wday = cal.get(Calendar.DAY_OF_WEEK);
        if (hour < 0 || hour > 23) {
            if (cur_wday != wday) {
                hour = 0;
            } else {
                hour = cur_hour >= 23 ? 0 : cur_hour + 1;
            }
        }

        int amount = wday - cur_wday;
        if (amount < 0) {
            amount = 7 + amount;
        }
        if (amount == 0 && cur_hour >= hour) {
            amount = 7;
        }
        cal.add(Calendar.DAY_OF_WEEK, amount);
    } else if (month >= Calendar.JANUARY && month <= Calendar.DECEMBER) { // do something about when every date is accepted
        if (date < 1) {
            date = 1;
        }
        if (date > 31) {
            date = 31;
        }
        int cur_month = cal.get(Calendar.MONTH);
        int amount = month - cur_month;
        if (amount < 0) {
            amount = 12 + amount;
        }
        if (amount == 0) {
            if (cal.get(Calendar.DATE) > date) {
                amount = 12;
            } else if (cal.get(Calendar.DATE) == date) {
                if (cur_hour >= hour) {
                    amount = 12;
                }
            }
        }
        // cal.set(Calendar.DATE, date);
        cal.add(Calendar.MONTH, amount);
        if (date > cal.getActualMaximum(Calendar.DATE)) {
            date = cal.getActualMaximum(Calendar.DATE);
        }
        cal.set(Calendar.DATE, date);
    } else if (date >= 1 && date <= 31) {
        int cur_date = cal.get(Calendar.DATE);
        if (cur_date > date) {
            cal.add(Calendar.MONTH, 1);
        } else if (cur_date == date) {
            if (cur_hour >= hour) {
                cal.add(Calendar.MONTH, 1);
            }
        }
        cal.set(Calendar.DATE, date);
    } else {
        if (hour < 0 || hour > 23) {
            hour = cur_hour >= 23 ? 0 : cur_hour + 1;
        }
        if (cur_hour >= hour) {
            cal.add(Calendar.DATE, 1);
        }
    }

    if (hour >= 12) {
        cal.set(Calendar.HOUR, hour - 12);
        cal.set(Calendar.AM_PM, Calendar.PM);
    } else {
        cal.set(Calendar.HOUR, hour);
        cal.set(Calendar.AM_PM, Calendar.AM);
    }

    Date nextDate = cal.getTime();
    Date currDate = new Date();

    long nextTime = cal.getTime().getTime();
    long currTime = (new Date()).getTime();

    return nextTime - currTime;
}

From source file:org.webical.test.web.component.WeekViewPanelTest.java

/**
 * Test week view with less then 7 days//from w  w w  .j  ava 2  s  . com
 * @throws WebicalException
 */
public void testNonWeekUse() throws WebicalException {

    MockCalendarManager mockCalendarManager = (MockCalendarManager) annotApplicationContextMock
            .getBean("calendarManager");
    Calendar calendar1 = mockCalendarManager.getCalendarById("1");

    MockEventManager mockEventManager = new MockEventManager();
    annotApplicationContextMock.putBean("eventManager", mockEventManager);

    // Define the current date
    final GregorianCalendar currentDate = CalendarUtils.newTodayCalendar(getFirstDayOfWeek());
    currentDate.setFirstDayOfWeek(GregorianCalendar.MONDAY);

    // The list containing the different events
    List<Event> allEvents = new ArrayList<Event>();

    GregorianCalendar refcal = CalendarUtils.duplicateCalendar(currentDate);
    refcal.add(GregorianCalendar.DAY_OF_MONTH, 1);
    refcal.set(GregorianCalendar.HOUR_OF_DAY, 12);
    refcal.set(GregorianCalendar.MINUTE, 0);
    refcal.set(GregorianCalendar.SECOND, 0);

    GregorianCalendar cal = CalendarUtils.duplicateCalendar(currentDate);

    // Add a normal event tomorrow
    Event event = new Event();
    event.setUid("e1");
    event.setCalendar(calendar1);
    event.setSummary("Normal Event Description");
    event.setLocation("Normal Event Location");
    event.setDescription("Event e1");
    cal.setTime(refcal.getTime());
    event.setDtStart(cal.getTime());
    cal.add(GregorianCalendar.HOUR_OF_DAY, 2);
    event.setDtEnd(cal.getTime());
    log.debug(
            "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd());
    allEvents.add(event);
    mockEventManager.storeEvent(event);

    // Add a short recurring event, starting today, ending tomorrow
    event = new Event();
    event.setUid("e2");
    event.setCalendar(calendar1);
    event.setSummary("Recurring Event Today");
    event.setLocation("Recurring Event Location");
    event.setDescription("Event e2");
    cal.setTime(refcal.getTime());
    cal.add(GregorianCalendar.DAY_OF_MONTH, -1);
    event.setDtStart(cal.getTime());
    cal.add(GregorianCalendar.HOUR_OF_DAY, 2);
    cal.add(GregorianCalendar.DAY_OF_MONTH, 2);
    event.setDtEnd(cal.getTime());
    RecurrenceUtil.setRecurrenceRule(event,
            new Recurrence(Recurrence.DAILY, 1, CalendarUtils.getEndOfDay(cal.getTime())));
    log.debug(
            "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd());
    allEvents.add(event);
    mockEventManager.storeEvent(event);

    // Add a long recurring event, starting last week, ending next week
    event = new Event();
    event.setUid("e3");
    event.setCalendar(calendar1);
    event.setSummary("Recurring Event Last Month");
    event.setLocation("Recurring Event Location");
    event.setDescription("Event e3");
    cal.setTime(refcal.getTime());
    cal.add(GregorianCalendar.WEEK_OF_YEAR, -1);
    event.setDtStart(cal.getTime());
    cal.add(GregorianCalendar.HOUR_OF_DAY, 2);
    cal.add(GregorianCalendar.WEEK_OF_YEAR, 3);
    event.setDtEnd(cal.getTime());
    RecurrenceUtil.setRecurrenceRule(event,
            new Recurrence(Recurrence.DAILY, 1, CalendarUtils.getEndOfDay(cal.getTime())));
    log.debug(
            "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd());
    allEvents.add(event);
    mockEventManager.storeEvent(event);

    // Add a (pseudo) all day event, starting day + 1 midnight, ending day + 2 00:00 hours
    event = new Event();
    event.setUid("e4");
    event.setCalendar(calendar1);
    event.setSummary("Pseudo All Day Event");
    event.setLocation("All Day Event Location");
    event.setDescription("Starting day + 1, ending day + 2 00:00 hours");
    cal.setTime(refcal.getTime());
    cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
    cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
    cal.set(GregorianCalendar.MINUTE, 0);
    cal.set(GregorianCalendar.SECOND, 0);
    cal.set(GregorianCalendar.MILLISECOND, 0);
    event.setDtStart(cal.getTime());
    cal.add(GregorianCalendar.DAY_OF_MONTH, 1);
    event.setDtEnd(cal.getTime());
    log.debug(
            "Adding event: " + event.getDescription() + " -> " + event.getDtStart() + " - " + event.getDtEnd());
    allEvents.add(event);
    mockEventManager.storeEvent(event);

    // Create test page with a WeekViewPanel
    wicketTester.startPage(new ITestPageSource() {
        private static final long serialVersionUID = 1L;

        public Page getTestPage() {
            return new PanelTestPage(new WeekViewPanel(PanelTestPage.PANEL_MARKUP_ID, 4, currentDate) {
                private static final long serialVersionUID = 1L;

                @Override
                public void onAction(IAction action) {
                    /* NOTHING TO DO */ }
            });
        }
    });

    // Basic assertions
    wicketTester.assertRenderedPage(PanelTestPage.class);
    wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID, WeekViewPanel.class);

    // Assert number of days rendered
    // Set the correct dates to find the first and last day of the week
    GregorianCalendar viewFirstDayCalendar = CalendarUtils.duplicateCalendar(currentDate);
    // Assert the first day in the view
    wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day"
            + viewFirstDayCalendar.get(GregorianCalendar.DAY_OF_YEAR), WeekDayPanel.class);

    GregorianCalendar viewLastDayCalendar = CalendarUtils.duplicateCalendar(currentDate);
    viewLastDayCalendar.add(GregorianCalendar.DAY_OF_MONTH, 3);
    // Assert the last day in the view
    wicketTester.assertComponent(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day"
            + viewLastDayCalendar.get(GregorianCalendar.DAY_OF_YEAR), WeekDayPanel.class);

    // Events for days in this 4 day period
    List<Event> dayEvents = new ArrayList<Event>();
    // Assert weekday events
    GregorianCalendar weekCal = CalendarUtils.duplicateCalendar(currentDate);
    for (int i = 0; i < 4; ++i) {
        WeekDayPanel weekDayEventsListView = (WeekDayPanel) wicketTester.getLastRenderedPage()
                .get(PanelTestPage.PANEL_MARKUP_ID + ":weekColumnRepeater:day"
                        + weekCal.get(GregorianCalendar.DAY_OF_YEAR));
        switch (i) {
        case 0:
            dayEvents.clear();
            dayEvents.add(allEvents.get(1)); // e2
            dayEvents.add(allEvents.get(2)); // e3
            break;
        case 1:
            dayEvents.clear();
            dayEvents.add(allEvents.get(0)); // e1
            dayEvents.add(allEvents.get(1)); // e2
            dayEvents.add(allEvents.get(2)); // e3
            break;
        case 2:
            dayEvents.clear();
            dayEvents.add(allEvents.get(1)); // e2
            dayEvents.add(allEvents.get(2)); // e3
            dayEvents.add(allEvents.get(3)); // e4
            break;
        case 3:
            dayEvents.clear();
            dayEvents.add(allEvents.get(2)); // e3
            break;
        }
        String path = weekDayEventsListView.getPageRelativePath() + ":eventItem";
        wicketTester.assertListView(path, dayEvents);

        ListView listView = (ListView) wicketTester.getComponentFromLastRenderedPage(path);
        Iterator<?> lvIt = listView.iterator();
        while (lvIt.hasNext()) {
            ListItem item = (ListItem) lvIt.next();
            Event evt = (Event) item.getModelObject();
            List<?> bhvs = item.getBehaviors();
            if (evt.getUid().equals("e4"))
                assertEquals(1, bhvs.size()); // e4 all day event
            else
                assertEquals(0, bhvs.size());
        }

        weekCal.add(GregorianCalendar.DAY_OF_WEEK, 1);
    }
}