Example usage for java.util GregorianCalendar getFirstDayOfWeek

List of usage examples for java.util GregorianCalendar getFirstDayOfWeek

Introduction

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

Prototype

public int getFirstDayOfWeek() 

Source Link

Document

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

Usage

From source file:CalendarTest.java

public static void main(String[] args) {
    // construct d as current date
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);

    // set d to start date of the month
    d.set(Calendar.DAY_OF_MONTH, 1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    // get first day of week (Sunday in the U.S.)
    int firstDayOfWeek = d.getFirstDayOfWeek();

    // determine the required indentation for the first line
    int indent = 0;
    while (weekday != firstDayOfWeek) {
        indent++;// w ww  .  j  a  v a 2 s  .co  m
        d.add(Calendar.DAY_OF_MONTH, -1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    }

    // print weekday names
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    do {
        System.out.printf("%4s", weekdayNames[weekday]);
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();

    for (int i = 1; i <= indent; i++)
        System.out.print("    ");

    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
        // print day
        int day = d.get(Calendar.DAY_OF_MONTH);
        System.out.printf("%3d", day);

        // mark current day with *
        if (day == today)
            System.out.print("*");
        else
            System.out.print(" ");

        // advance d to the next day
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);

        // start a new line at the start of the week
        if (weekday == firstDayOfWeek)
            System.out.println();
    } while (d.get(Calendar.MONTH) == month);
    // the loop exits when d is day 1 of the next month

    // print final end of line if necessary
    if (weekday != firstDayOfWeek)
        System.out.println();
}

From source file:Main.java

public static long currentWeekInMills() {
    GregorianCalendar gregoriancalendar = new GregorianCalendar();
    GregorianCalendar gregoriancalendar1 = new GregorianCalendar(gregoriancalendar.get(1),
            gregoriancalendar.get(2), gregoriancalendar.get(5));
    gregoriancalendar1.setTimeZone(GMT);
    gregoriancalendar1.add(6, -(gregoriancalendar.get(7) - gregoriancalendar.getFirstDayOfWeek()));
    return gregoriancalendar1.getTimeInMillis();
}

From source file:org.jfree.data.time.WeekTest.java

/**
 * A test for a problem in constructing a new Week instance.
 *///from  ww w  . ja  v a 2  s  . c o  m
@Test
public void testConstructor() {
    Locale savedLocale = Locale.getDefault();
    TimeZone savedZone = TimeZone.getDefault();
    Locale.setDefault(new Locale("da", "DK"));
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Copenhagen"));
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance(TimeZone.getDefault(),
            Locale.getDefault());

    // first day of week is monday
    assertEquals(Calendar.MONDAY, cal.getFirstDayOfWeek());
    cal.set(2007, Calendar.AUGUST, 26, 1, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date t = cal.getTime();
    Week w = new Week(t, TimeZone.getTimeZone("Europe/Copenhagen"));
    assertEquals(34, w.getWeek());

    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("US/Detroit"));
    cal = (GregorianCalendar) Calendar.getInstance(TimeZone.getDefault());
    // first day of week is Sunday
    assertEquals(Calendar.SUNDAY, cal.getFirstDayOfWeek());
    cal.set(2007, Calendar.AUGUST, 26, 1, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);

    t = cal.getTime();
    w = new Week(t, TimeZone.getTimeZone("Europe/Copenhagen"));
    assertEquals(35, w.getWeek());
    w = new Week(t, TimeZone.getTimeZone("Europe/Copenhagen"), new Locale("da", "DK"));
    assertEquals(34, w.getWeek());

    Locale.setDefault(savedLocale);
    TimeZone.setDefault(savedZone);
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.group.AddBookingRoomViewParamsController.java

@Override
public Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
    Map map = new HashMap<String, Object>();

    int filterGroup = Integer.parseInt(request.getParameter("filterGroup"));
    String filterDate = request.getParameter("filterDate");
    if (filterDate.compareTo("") == 0)
        filterDate = null;/*w ww  .  j  av  a 2 s  . c  om*/
    int repType = Integer.parseInt(request.getParameter("repType"));
    int repCount = Integer.parseInt(request.getParameter("repCount"));

    String date = request.getParameter("date");
    String startStr = request.getParameter("startTime");
    String endStr = request.getParameter("endTime");

    GregorianCalendar cal = BookingRoomUtils.getCalendar(startStr);
    boolean collisions = false;

    //reservations in not visible time period
    if (repCount > 0) {
        GregorianCalendar nextS = BookingRoomUtils.getCalendar(startStr);
        GregorianCalendar nextE = BookingRoomUtils.getCalendar(endStr);

        List<Reservation> coll = BookingRoomUtils.getCollisions(reservationDao, repCount, repType, nextS,
                nextE);

        map.put("collisionsInNext", coll);
        map.put("collisionsInNextCount", coll.size());
        if (coll.size() > 0)
            collisions = true;
    }

    //reservations in currently visible time period
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    GregorianCalendar weekStart = BookingRoomUtils.getCalendar(BookingRoomUtils.getDate(cal) + " 00:00:00");

    GregorianCalendar weekEnd = (GregorianCalendar) weekStart.clone();
    weekEnd.add(Calendar.DAY_OF_YEAR, 7);
    weekEnd.add(Calendar.SECOND, -1);

    map.put("reservations", reservationDao.getReservationsBetween(weekStart, weekEnd, filterDate, filterGroup));
    map.put("timerange", date + " " + BookingRoomUtils.getHoursAndMinutes(startStr) + " - "
            + BookingRoomUtils.getHoursAndMinutes(endStr));

    String displayed = String.format(
            messageSource.getMessage("bookRoom.displayed.week", null, RequestContextUtils.getLocale(request)),
            BookingRoomUtils.getDate(weekStart), BookingRoomUtils.getDate(weekEnd));

    boolean filtered = false;

    if (filterDate != null) {//filter of date
        filtered = true;
        displayed = messageSource.getMessage("bookRoom.displayed.day", null,
                RequestContextUtils.getLocale(request)) + " " + filterDate;
    }

    if (filterGroup > 0) {
        filtered = true;
        displayed += (filterDate == null ? "," : " and") + " "
                + messageSource.getMessage("bookRoom.displayed.group", null,
                        RequestContextUtils.getLocale(request))
                + " " + getResearchGroup(filterGroup).getTitle();
    }

    if (filtered) {//we must verify that there are no reservations in selected range
        GregorianCalendar start = BookingRoomUtils.getCalendar(startStr);
        GregorianCalendar end = BookingRoomUtils.getCalendar(endStr);
        List<Reservation> coll = reservationDao.getReservationsBetween(start, end);
        if (coll.size() > 0) {
            //if the collision exists
            collisions = true;
            map.put("collisions", coll);
            map.put("collisionsCount", coll.size());
        }
    }

    map.put("displayed", displayed);

    map.put("collisionsExist", (collisions) ? "1" : "0");

    /*
    -- JSP can get this from params object --
    map.put("repCount", repCount);
    map.put("repType", repType);
    map.put("group", group);
    map.put("date", date);*/
    map.put("startTime", BookingRoomUtils.getHoursAndMinutes(startStr).replaceAll(":", ""));
    map.put("endTime", BookingRoomUtils.getHoursAndMinutes(endStr).replaceAll(":", ""));
    map.put("loggedUser", personDao.getLoggedPerson());
    GroupMultiController.setPermissionToRequestGroupRole(map, personDao.getLoggedPerson());

    return map;
}

From source file:org.webical.web.component.event.EventForm.java

/**
 * Create an empty Event and set the start and end times
 * @return Event//w  ww .j a va  2s . co m
 */
private EventWrapper createEmptyEvent() {
    this.formEvent = new Event();
    this.eventWrapper = new EventWrapper(formEvent);

    GregorianCalendar dateCalendar = CalendarUtils.duplicateCalendar(selectedDate);
    GregorianCalendar timeCalendar = CalendarUtils.newTodayCalendar(dateCalendar.getFirstDayOfWeek());
    dateCalendar.set(GregorianCalendar.HOUR_OF_DAY, timeCalendar.get(GregorianCalendar.HOUR_OF_DAY));
    dateCalendar.set(GregorianCalendar.MINUTE, timeCalendar.get(GregorianCalendar.MINUTE) / 5 * 5);

    //Fill the start and end times with proper values
    eventWrapper.setDtStart(dateCalendar.getTime());
    dateCalendar.add(GregorianCalendar.HOUR_OF_DAY, 1);
    eventWrapper.setDtEnd(dateCalendar.getTime());

    return this.eventWrapper;
}