Example usage for java.util GregorianCalendar set

List of usage examples for java.util GregorianCalendar set

Introduction

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

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:org.mifos.calendar.CalendarUtils.java

/**
 * for monthly on date return the next date falling on the same day. If date has passed, pass in the date of next
 * month, adjust to day number if day number exceed total number of days in month
 *///from   w w  w .j ava  2 s . c om
public static DateTime getFirstDateForMonthOnDate(final DateTime startDate, final int dayOfMonth) {

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    int dt = gc.get(GregorianCalendar.DATE);
    // if date passed in, is after the date on which schedule has to
    // lie, move to next month
    if (dt > dayOfMonth) {
        gc.add(GregorianCalendar.MONTH, 1);
    }
    // set the date on which schedule has to lie
    int M1 = gc.get(GregorianCalendar.MONTH);
    gc.set(GregorianCalendar.DATE, dayOfMonth);
    int M2 = gc.get(GregorianCalendar.MONTH);

    int daynum = dayOfMonth;
    while (M1 != M2) {
        gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
        gc.set(GregorianCalendar.DATE, daynum - 1);
        M2 = gc.get(GregorianCalendar.MONTH);
        daynum--;
    }

    return new DateTime(gc.getTime());
}

From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.DataTypeUtils.java

private static GregorianCalendar getCalendarFromDT(DT dt) throws DataTypeException {

    // hl7/hapi returns 0 for no date
    if (dt.getYear() == 0 || dt.getMonth() == 0 || dt.getDay() == 0)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    // zero out fields we don't use
    cal.setTimeInMillis(0);/*  w  ww .j  av a  2s .  com*/
    cal.set(GregorianCalendar.YEAR, dt.getYear());
    cal.set(GregorianCalendar.MONTH, dt.getMonth() - 1);
    cal.set(GregorianCalendar.DAY_OF_MONTH, dt.getDay());

    // force materialisation of values
    cal.getTimeInMillis();

    return (cal);
}

From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.DataTypeUtils.java

public static GregorianCalendar getCalendarFromDTM(DTM dtm) throws DataTypeException {

    // hl7/hapi returns 0 for no date
    if (dtm.getYear() == 0 || dtm.getMonth() == 0 || dtm.getDay() == 0)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    // zero out fields we don't use
    cal.setTimeInMillis(0);//from  w w  w .  j ava2s . c om
    cal.set(GregorianCalendar.YEAR, dtm.getYear());
    cal.set(GregorianCalendar.MONTH, dtm.getMonth() - 1);
    cal.set(GregorianCalendar.DAY_OF_MONTH, dtm.getDay());
    cal.set(GregorianCalendar.HOUR_OF_DAY, dtm.getHour());
    cal.set(GregorianCalendar.MINUTE, dtm.getMinute());
    cal.set(GregorianCalendar.SECOND, dtm.getSecond());

    // force materialisation of values
    cal.getTimeInMillis();

    return (cal);
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized date:
 * - when pattern doesn't contain year pattern -> current year is used
 * - when pattern doesn't contain month pattern
 *    date represents current year -> current month is used
 *                        else -> january is used
 * - when pattern doesn't contain day pattern
 *    date represents current year and month -> current day is used
 *                                 else -> first day of month is used
 */// w w  w  .j a v a 2 s  .c  o  m
private static Date normalizeDate(Calendar nowCld, Date date, Locale locale, String pattern) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    //set current date
    if (pattern.toLowerCase(locale).indexOf('y') == -1) {
        dateCld.set(Calendar.YEAR, nowCld.get(Calendar.YEAR));
    }

    if (pattern.indexOf('M') == -1) {
        if (nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR)) {
            dateCld.set(Calendar.MONTH, nowCld.get(Calendar.MONTH));
        } else {
            dateCld.set(Calendar.MONTH, 0);
        }
    }
    if (pattern.toLowerCase(locale).indexOf('d') == -1) {
        if (nowCld.get(Calendar.YEAR) != dateCld.get(Calendar.YEAR)
                || nowCld.get(Calendar.MONTH) != dateCld.get(Calendar.MONTH)) {
            dateCld.set(Calendar.DAY_OF_MONTH, 1);
        } else {
            dateCld.set(Calendar.DAY_OF_MONTH, nowCld.get(Calendar.DAY_OF_MONTH));
        }
    }
    return dateCld.getTime();
}

From source file:org.nuclos.common2.DateUtils.java

private static void calc(GregorianCalendar result, CalcFunction cf, CalcPair cp) {
    switch (cf) {
    case ADD://from   w ww.j a va 2s. c  o m
        result.add(cp.x, cp.y);
        break;
    case SUBTRACT:
        result.add(cp.x, cp.y * (-1));
        break;
    case SET:
        switch (cp.x) {
        case Calendar.YEAR:
            result.set(Calendar.DAY_OF_YEAR,
                    cp.y == Integer.MIN_VALUE ? result.getActualMinimum(Calendar.DAY_OF_YEAR)
                            : result.getActualMaximum(Calendar.DAY_OF_YEAR));
            break;
        case Calendar.MONTH:
            result.set(Calendar.DAY_OF_MONTH,
                    cp.y == Integer.MIN_VALUE ? result.getActualMinimum(Calendar.DAY_OF_MONTH)
                            : result.getActualMaximum(Calendar.DAY_OF_MONTH));
            break;
        case Calendar.WEEK_OF_YEAR:
            result.set(Calendar.DAY_OF_WEEK, cp.y == Integer.MIN_VALUE ? Calendar.MONDAY : Calendar.SUNDAY);
            break;
        }
        break;
    }
}

From source file:oscar.util.DateUtils.java

/**
 * This will take 2 date objects, presumably one that holds the date and the other that holds the time
 * and it will merge the two into one object that has both the date and the time. This method is not 
 * normally useful but our database seems to have a lot of split date/time objects.
 * If either parameters are null it will return null.
 * This method will materialise the result before returning.
 *//*from   w ww .jav a 2s .  c o  m*/
public static GregorianCalendar toGregorianCalendar(Date date, Date time) {
    if (date == null || time == null)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);

    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTime(time);

    cal.set(GregorianCalendar.HOUR_OF_DAY, cal2.get(GregorianCalendar.HOUR_OF_DAY));
    cal.set(GregorianCalendar.MINUTE, cal2.get(GregorianCalendar.MINUTE));
    cal.set(GregorianCalendar.SECOND, cal2.get(GregorianCalendar.SECOND));
    cal.set(GregorianCalendar.MILLISECOND, cal2.get(GregorianCalendar.MILLISECOND));
    cal.getTime();

    return (cal);
}

From source file:org.zephyrsoft.sdb2.model.statistics.SongStatistics.java

private Date wipeTime(Date date) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);//from  w  ww.j  a  v  a 2s  . c o  m
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:org.nuclos.common2.SeriesUtils.java

/**
 * //from  w ww  .  ja  v  a 2s  . c  o  m
 * @param series
 * @param dateOrigin
 * @return the next date calculated by series from origin. 
 *          origin could be a calculated date (result >= origin)
 */
public static DateTime getSeriesNext(String series, DateTime dateOrigin) {
    if (series == null)
        return dateOrigin;

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(dateOrigin.getDate());

    String[] split = org.apache.commons.lang.StringUtils.split(series, '|');

    if (split.length > 0) {
        String modus = split[0];

        if ("d".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            int days = Integer.parseInt(split[3]);
            if (days == 0) {
                // add one day if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

                while (!isWorkingDay(calendar.get(GregorianCalendar.DAY_OF_WEEK))) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

            } else {
                // add one day if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

                if (days > 1) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, days - 1);
                }
            }

        } else if ("w".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            int weeks = Integer.parseInt(split[3]);

            List<Integer> possibleWeekdays = new ArrayList<Integer>();
            int firstSelectedWeekday = -1000;
            int lastWeekday = -1000;

            // use getWeekdayItems() in order to get the right start (end) of the week
            for (SeriesListItem sli : getWeekdayItems()) {
                boolean addWeekday = false;

                switch (sli.getId()) {
                case GregorianCalendar.MONDAY:
                    if (split[4].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.TUESDAY:
                    if (split[5].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.WEDNESDAY:
                    if (split[6].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.THURSDAY:
                    if (split[7].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.FRIDAY:
                    if (split[8].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.SATURDAY:
                    if (split[9].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.SUNDAY:
                    if (split[10].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                }

                if (addWeekday) {
                    possibleWeekdays.add(sli.getId());
                    if (firstSelectedWeekday == -1000)
                        firstSelectedWeekday = sli.getId();
                }

                lastWeekday = sli.getId();
            }

            // add one day if calculated date is before origin
            boolean weeksAdded = false;
            if (calendar.getTime().before(dateOrigin.getDate())) {
                if (lastWeekday == calendar.get(GregorianCalendar.DAY_OF_WEEK)) {
                    calendar.add(GregorianCalendar.WEEK_OF_YEAR, weeks - 1);
                    weeksAdded = true;
                } else {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }
            }

            while (!possibleWeekdays.contains(new Integer(calendar.get(GregorianCalendar.DAY_OF_WEEK)))) {
                if (!weeksAdded && lastWeekday == calendar.get(GregorianCalendar.DAY_OF_WEEK)) {
                    calendar.add(GregorianCalendar.WEEK_OF_YEAR, weeks - 1);
                }
                calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
            }

        } else if ("m".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            if ("m1".equals(split[3])) {
                int day = Integer.parseInt(split[4]);
                int months = Integer.parseInt(split[5]);

                calendar.set(GregorianCalendar.DAY_OF_MONTH, day);

                // add one month if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.MONTH, 1);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

                if (months > 1) {
                    calendar.add(GregorianCalendar.MONTH, months - 1);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

            } else {
                int number = Integer.parseInt(split[4]);
                int weekday = Integer.parseInt(split[5]);
                int months = Integer.parseInt(split[6]);

                calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);

                // add one month if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.MONTH, 1);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }

                if (months > 1) {
                    calendar.add(GregorianCalendar.MONTH, months - 1);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }
            }

        } else if ("y".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            if ("y1".equals(split[3])) {
                int day = Integer.parseInt(split[4]);
                int month = Integer.parseInt(split[5]);

                calendar.set(GregorianCalendar.MONTH, month);
                calendar.set(GregorianCalendar.DAY_OF_MONTH, day);

                // add one year if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.YEAR, 1);
                    calendar.set(GregorianCalendar.MONTH, month);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

            } else {
                int number = Integer.parseInt(split[4]);
                int weekday = Integer.parseInt(split[5]);
                int month = Integer.parseInt(split[6]);

                calendar.set(GregorianCalendar.MONTH, month);
                calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);

                // add one year if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.YEAR, 1);
                    calendar.set(GregorianCalendar.MONTH, month);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }
            }
        }
    }

    return new DateTime(calendar.getTimeInMillis());
}

From source file:org.openehealth.coala.converter.PXSDateConverterTest.java

/**
 * @throws java.lang.Exception// w  w  w. j  ava2  s  .  com
 */
@Before
public void setUp() throws Exception {
    ResourceBundle properties = ResourceBundle.getBundle("coala-document");
    longPattern = properties.getString("coala.consent.longdatepattern");
    shortPattern = properties.getString("coala.consent.shortdatepattern");

    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2011);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, 15);
    cal.set(Calendar.HOUR, 3);
    cal.set(Calendar.MINUTE, 36);
    cal.set(Calendar.SECOND, 50);
    cal.set(Calendar.MILLISECOND, 0);
    referenceDateLong = cal.getTime();

    cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2011);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, 15);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    referenceDateShort = cal.getTime();

}

From source file:com.projity.server.data.MPXConverter.java

public static void toMpxCalendar(WorkingCalendar workCalendar, ProjectCalendar mpx) {
    mpx.setName(workCalendar.getName());
    //      mpx.setUniqueID((int) workCalendar.getId()); // TODO watch out for int overrun

    WorkingCalendar wc = workCalendar;/*from  www . ja va  2  s.c o  m*/
    if (workCalendar.isBaseCalendar())
        wc = (WorkingCalendar) workCalendar.getBaseCalendar();
    for (int i = 0; i < 7; i++) {// MPX days go from SUNDAY=1 to SATURDAY=7
        WorkDay day = workCalendar.isBaseCalendar() ? workCalendar.getDerivedWeekDay(i)
                : workCalendar.getWeekDay(i);
        ProjectCalendarHours mpxDay = null;
        Day d = Day.getInstance(i + 1);
        if (day == null) {
            mpx.setWorkingDay(d, ProjectCalendar.DEFAULT);
        } else {
            mpx.setWorkingDay(d, day.isWorking());
            if (day.isWorking()) {
                mpxDay = mpx.addCalendarHours(Day.getInstance(i + 1));
                toMpxCalendarDay(day, mpxDay);
            }
        }
    }

    WorkDay[] workDays = workCalendar.getExceptionDays();
    if (workDays != null)
        for (int i = 0; i < workDays.length; i++) {
            if (workDays[i] == null || workDays[i].getStart() == 0L || workDays[i].getStart() == Long.MAX_VALUE)
                continue;
            ProjectCalendarException exception = mpx.addCalendarException();
            Date start = new Date(workDays[i].getStart());
            exception.setFromDate(start);
            GregorianCalendar cal = DateTime.calendarInstance();
            // days go from 00:00 to 23:59
            cal.setTime(start);
            cal.set(Calendar.HOUR, 23);
            cal.set(Calendar.MINUTE, 59);
            exception.setToDate(DateTime.fromGmt(cal.getTime()));
            toMpxExceptionDay(workDays[i], exception);
            exception.setWorking(workDays[i].isWorking());
        }
    WorkCalendar baseCalendar = workCalendar.getBaseCalendar();
    if (baseCalendar != null) {
        mpx.setBaseCalendar(ImportedCalendarService.getInstance().findExportedCalendar(baseCalendar));
    }

    //mpx.setUniqueID((int)workCalendar.getUniqueId());
}