Java Code Examples for java.util.Calendar#DAY_OF_WEEK_IN_MONTH

The following examples show how to use java.util.Calendar#DAY_OF_WEEK_IN_MONTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartialDate.java    From dialogflow-java-client with Apache License 2.0 6 votes vote down vote up
public void set(final int field, final Integer value) {
    if (value == UNSPECIFIED_VALUE) {
        if (field == Calendar.YEAR) {
            unspecifiedFields.add(Calendar.YEAR);
        } else if (field == Calendar.MONTH) {
            unspecifiedFields.add(Calendar.MONTH);
        } else if (field >= Calendar.WEEK_OF_YEAR && field <= Calendar.DAY_OF_WEEK_IN_MONTH) {
            unspecifiedFields.add(Calendar.DATE);
        } else if (field >= Calendar.HOUR && field <= Calendar.HOUR_OF_DAY) {
            unspecifiedFields.add(Calendar.HOUR_OF_DAY);
        } else if (field == Calendar.MINUTE) {
            unspecifiedFields.add(Calendar.MINUTE);
        }

        // do nothing with other fields

    } else {
        unspecifiedFields.remove(field);
        c.set(field, value);
    }
}
 
Example 2
Source File: DateHelper.java    From ActivityDiary with GNU General Public License v3.0 5 votes vote down vote up
public static Calendar startOf(int field, long timeRef){
    Calendar result = Calendar.getInstance();
    result.setTimeInMillis(timeRef);
    result.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
    result.clear(Calendar.MINUTE);
    result.clear(Calendar.SECOND);
    result.clear(Calendar.MILLISECOND);
    switch(field){
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_WEEK:
        case Calendar.DAY_OF_WEEK_IN_MONTH:
        case Calendar.DAY_OF_YEAR:
            /* nothing to do, as HOUR_OF_DAY is already 0 */
            break;
        case Calendar.WEEK_OF_YEAR:
            result.set(Calendar.DAY_OF_WEEK, result.getFirstDayOfWeek());
            break;
        case Calendar.MONTH:
            result.set(Calendar.DAY_OF_MONTH, 1);
            break;
        case Calendar.YEAR:
            result.set(Calendar.DAY_OF_YEAR, 1);
            break;
        default:
            throw new RuntimeException("date field not supported: " + field);
    }
    return result;
}
 
Example 3
Source File: PartialDate.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
public Integer get(final int field) {

        if (field == Calendar.YEAR) {
            if (!unspecifiedFields.contains(Calendar.YEAR)) {
                return c.get(field);
            }
            return UNSPECIFIED_VALUE;
        } else if (field == Calendar.MONTH) {
            if (!unspecifiedFields.contains(Calendar.MONTH)) {
                return c.get(field);
            }
            return UNSPECIFIED_VALUE;
        } else if (field >= Calendar.WEEK_OF_YEAR && field <= Calendar.DAY_OF_WEEK_IN_MONTH) {
            if (!unspecifiedFields.contains(Calendar.DATE)) {
                return c.get(field);
            }
            return UNSPECIFIED_VALUE;
        } else if (field >= Calendar.HOUR && field <= Calendar.HOUR_OF_DAY) {
            if (!unspecifiedFields.contains(Calendar.HOUR_OF_DAY)) {
                return c.get(field);
            }
            return UNSPECIFIED_VALUE;
        } else if (field == Calendar.MINUTE) {
            if (!unspecifiedFields.contains(Calendar.MINUTE)) {
                return c.get(Calendar.MINUTE);
            }
            return UNSPECIFIED_VALUE;
        } else {
            return c.get(field);
        }

        //return UNSPECIFIED_VALUE;
    }
 
Example 4
Source File: PartialDate.java    From dialogflow-java-client with Apache License 2.0 5 votes vote down vote up
private String getFieldAsString(final int field) {
    if (field == Calendar.YEAR) {
        if (unspecifiedFields.contains(Calendar.YEAR)) {
            return UNSPECIFIED_YEAR;
        } else {
            return String.format("%4d", c.get(field));
        }
    } else if (field == Calendar.MONTH) {
        if (unspecifiedFields.contains(Calendar.MONTH)) {
            return UNSPECIFIED_MONTH;
        } else {
            return String.format("%02d", c.get(field));
        }
    } else if (field >= Calendar.WEEK_OF_YEAR && field <= Calendar.DAY_OF_WEEK_IN_MONTH) {
        if (unspecifiedFields.contains(Calendar.DATE)) {
            return UNSPECIFIED_DATE;
        } else {
            return String.format("%02d", c.get(field));
        }
    } else if (field >= Calendar.HOUR && field <= Calendar.HOUR_OF_DAY) {
        if (unspecifiedFields.contains(Calendar.HOUR_OF_DAY)) {
            return UNSPECIFIED_HOUR;
        } else {
            return String.format("%02d", c.get(field));
        }
    } else if (field == Calendar.MINUTE) {
        if (unspecifiedFields.contains(Calendar.MINUTE)) {
            return UNSPECIFIED_MINUTE;
        } else {
            return String.format("%02d", c.get(field));
        }
    } else {
        return String.format("%s", c.get(field));
    }
}
 
Example 5
Source File: CDateTime.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Set the style of this CDateTime to work with dates and / or times as
 * determined by the given pattern. This will set the fields shown in the
 * text box and, if <code>DROP_DOWN</code> style is set, the fields of the
 * drop down component.<br>
 * This method is backed by an implementation of SimpleDateFormat, and as
 * such, any string pattern which is valid for SimpleDateFormat may be used.
 * Examples (US Locale):<br>
 * </code>setPattern("MM/dd/yyyy h:mm a");</code><br />
 * </code>setPattern("'Meeting @' h:mm a 'on' EEEE, MMM dd,
 * yyyy");</code><br />
 * 
 * @param pattern
 *            the pattern to use, if it is invalid, the original is restored
 * @throws IllegalArgumentException
 * @see SimpleDateFormat
 * @see #getPattern()
 * @see #setFormat(int)
 */
public void setPattern(String pattern) throws IllegalArgumentException {
	this.allowedTimezones = null;
	if (isOpen()) {
		setOpen(false);
	}
	df = new SimpleDateFormat(pattern, locale);
	df.setTimeZone(timezone);
	if (updateFields()) {
		this.pattern = pattern;
		this.format = -1;
		boolean wasDate = isDate;
		boolean wasTime = isTime;
		isDate = isTime = false;
		calendarFields = new int[field.length];
		for (int i = 0; i < calendarFields.length; i++) {
			calendarFields[i] = getCalendarField(field[i]);
			switch (calendarFields[i]) {
			case Calendar.AM_PM:
			case Calendar.HOUR:
			case Calendar.HOUR_OF_DAY:
			case Calendar.MILLISECOND:
			case Calendar.MINUTE:
			case Calendar.SECOND:
			case Calendar.ZONE_OFFSET:
				isTime = true;
				break;
			case Calendar.DAY_OF_MONTH:
			case Calendar.DAY_OF_WEEK:
			case Calendar.DAY_OF_WEEK_IN_MONTH:
			case Calendar.DAY_OF_YEAR:
			case Calendar.ERA:
			case Calendar.MONTH:
			case Calendar.WEEK_OF_MONTH:
			case Calendar.WEEK_OF_YEAR:
			case Calendar.YEAR:
				isDate = true;
				break;
			default:
				break;
			}
		}
		if (checkButton() && (isDate != wasDate || isTime != wasTime)) {
			if (defaultButtonImage) {
				if (isDate && isTime) {
					doSetButtonImage(Resources.getIconCalendarClock());
				} else if (isDate) {
					doSetButtonImage(Resources.getIconCalendar());
				} else {
					doSetButtonImage(Resources.getIconClock());
				}
			}
			updateNullText();
		}
		if (checkText()) {
			updateText();
		}
		if (isSimple()) {
			disposePicker();
			createPicker();
		}
	} else {
		throw new IllegalArgumentException(
				"Problem setting pattern: \"" + pattern + "\""); //$NON-NLS-1$ //$NON-NLS-2$
	}
}