Java Code Examples for java.util.Calendar#DAY_OF_MONTH

The following examples show how to use java.util.Calendar#DAY_OF_MONTH .
Example 1
Project: astor   File: DateUtilsRoundingTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests DateUtils.round()-method with Calendar.DAY_OF_MONTH
 * Includes rounding the extremes of one day 
 * Includes rounding to January 1
 * 
 * @throws Exception
 * @since 3.0
 */
@Test
public void testRoundDayOfMonth() throws Exception {
    final int calendarField = Calendar.DAY_OF_MONTH;
    Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
    Date minDate, maxDate;

    roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000");
    roundedDownDate = targetDayOfMonthDate;
    lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
    baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate,  calendarField);
    
    //round to January 1
    minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000");
    maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999");
    roundToJanuaryFirst(minDate, maxDate, calendarField);
}
 
Example 2
Project: snap-desktop   File: DateTimePicker.java    License: GNU General Public License v3.0 6 votes vote down vote up
private JPanel createTimePanel() {
    JPanel newPanel = new JPanel();
    newPanel.setLayout(new FlowLayout());
    Date date = getDate();
    if (date == null) {
        Calendar calendar = Calendar.getInstance(timeZone);
        date = calendar.getTime();
    }
    SpinnerDateModel dateModel = new SpinnerDateModel(date, null, null, Calendar.DAY_OF_MONTH);
    timeSpinner = new JSpinner(dateModel);
    if (timeFormat == null) {
        timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
    }
    updateTextFieldFormat();
    newPanel.add(new JLabel("Time:"));
    newPanel.add(timeSpinner);
    newPanel.setBackground(Color.WHITE);
    return newPanel;
}
 
Example 3
Project: alfresco-data-model   File: SearchDateConversion.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @param dateAndResolution
 * @return String date
 */
public static String getDateStart(Pair<Date, Integer> dateAndResolution)
{
    Calendar cal = Calendar.getInstance(I18NUtil.getLocale());
    cal.setTime(dateAndResolution.getFirst());
    switch (dateAndResolution.getSecond())
    {
        case Calendar.YEAR:
            cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH));
        case Calendar.MONTH:
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        case Calendar.DAY_OF_MONTH:
            cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        case Calendar.HOUR_OF_DAY:
            cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        case Calendar.MINUTE:
            cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        case Calendar.SECOND:
            cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        case Calendar.MILLISECOND:
        default:
    }
    SimpleDateFormat formatter = CachingDateFormat.getSolrDatetimeFormat();
    formatter.setTimeZone(UTC_TIMEZONE);
    return formatter.format(cal.getTime());
}
 
Example 4
Project: astor   File: DateUtilsRoundingTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests DateUtils.round()-method with Calendar.DAY_OF_MONTH
 * Includes rounding the extremes of one day 
 * Includes rounding to January 1
 * 
 * @throws Exception
 * @since 3.0
 */
public void testRoundDayOfMonth() throws Exception {
    final int calendarField = Calendar.DAY_OF_MONTH;
    Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
    Date minDate, maxDate;

    roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000");
    roundedDownDate = targetDayOfMonthDate;
    lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
    baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate,  calendarField);
    
    //round to January 1
    minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000");
    maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999");
    roundToJanuaryFirst(minDate, maxDate, calendarField);
}
 
Example 5
Project: dhis2-core   File: Cal.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Adds the given amount of time to the given calendar field.
 *
 * @param field  the calendar field.
 * @param amount the amount of time.
 */
public Cal add( int field, int amount )
{
    switch ( field )
    {
        case Calendar.YEAR:
            dateTimeUnit = getCalendar().plusYears( dateTimeUnit, amount );
        case Calendar.MONTH:
            dateTimeUnit = getCalendar().plusMonths( dateTimeUnit, amount );
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_YEAR:
            dateTimeUnit = getCalendar().plusDays( dateTimeUnit, amount );
        break;
        default:
            throw new UnsupportedOperationException();
    }

    return this;
}
 
Example 6
Project: hortonmachine   File: Summary.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void setPeriod(String period) {
    if (period.equals(WEEKLY)) {
        field = Calendar.DAY_OF_WEEK;
    } else if (period.equals(MONTHLY)) {
        field = Calendar.DAY_OF_MONTH;
    } else if (period.equals(YEARLY)) {
        field = Calendar.DAY_OF_YEAR;
    } else {
        throw new IllegalArgumentException(period);
    }
}
 
Example 7
Project: astor   File: DateUtilsRoundingTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test DateUtils.truncate()-method with Calendar.DAY_OF_MONTH
 * 
 * @throws Exception
 * @since 3.0
 */
@Test
public void testTruncateDayOfMonth() throws Exception {
    final int calendarField = Calendar.DAY_OF_MONTH;
    final Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999");
    baseTruncateTest(targetDayOfMonthDate, lastTruncateDate, calendarField);
}
 
Example 8
Project: openregistry   File: DefaultIdentifierNotificationService.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public DefaultIdentifierNotificationService(
		final PersonRepository personRepository,
		final IdentiferNotificationStrategy notificationStrategy,
		final int daysForActivationKey) {
    this.personRepository = personRepository;
    this.notificationStrategy = notificationStrategy;
    this.endDateGenerator = new AdditiveDateTimeDateGenerator(Calendar.DAY_OF_MONTH, daysForActivationKey);
}
 
Example 9
Project: jdbc-driver-csv   File: SQLCalendarFunction.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
public Object eval(Map<String, Object> env) throws SQLException {
    Object retval = null;
    Object o = expression.eval(env);
    if (o != null) {
        /*
* Accept either java.sql.Date, java.sql.Time or java.sql.Timestamp.
*/
        java.util.Date date = null;
        if (o instanceof java.util.Date) {
            date = (java.util.Date) o;
        } else {
/*
 * Try and convert from String to a Timestamp or Date/Time.
 */
            Expression stringConverter = new ColumnName(StringConverter.COLUMN_NAME);
            StringConverter sc = (StringConverter) stringConverter.eval(env);
            date = sc.parseTimestamp(o.toString());
            if (date == null) {
                if (calendarField == Calendar.DAY_OF_MONTH ||
                        calendarField == Calendar.MONTH ||
                        calendarField == Calendar.YEAR) {
                    date = sc.parseDate(o.toString());
                } else {
                    date = sc.parseTime(o.toString());
                }
            }
        }
        if (date != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int fieldValue = cal.get(calendarField);
            if (calendarField == Calendar.MONTH) {
                fieldValue++;
            }
            retval = fieldValue;
        }
    }
    return retval;
}
 
Example 10
Project: cql_engine   File: FhirModelResolver.java    License: Apache License 2.0 5 votes vote down vote up
protected DateTime toDateTime(BaseDateTimeType value, Integer calendarConstant) {
    Calendar calendar = this.getCalendar(value);

    TimeZone tz = calendar.getTimeZone() == null ? TimeZone.getDefault() : calendar.getTimeZone();
    ZoneOffset zoneOffset = tz.toZoneId().getRules().getStandardOffset(calendar.toInstant());
    switch (calendarConstant) {
        case Calendar.YEAR: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR)
        );
        case Calendar.MONTH: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1
        );
        case Calendar.DAY_OF_MONTH: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)
        );
        case Calendar.HOUR_OF_DAY: return new DateTime(
            TemporalHelper.zoneToOffset(zoneOffset),
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY)
        );
        case Calendar.MINUTE: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE)
        );
        case Calendar.SECOND: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)
        );
        case Calendar.MILLISECOND: return new DateTime(
                TemporalHelper.zoneToOffset(zoneOffset),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND)
        );
        default: throw new InvalidPrecision(String.format("Invalid temporal precision %s", calendarConstant));
    }
}
 
Example 11
Project: java-swing-tips   File: MainPanel.java    License: MIT License 5 votes vote down vote up
private MainPanel() {
  super(new GridLayout(3, 1));
  String dateFormat = "yyyy/MM/dd";

  Date date = new Date();
  JSpinner spinner1 = new JSpinner(new SpinnerDateModel(date, date, null, Calendar.DAY_OF_MONTH));
  spinner1.setEditor(new JSpinner.DateEditor(spinner1, dateFormat));

  Calendar today = Calendar.getInstance();
  today.clear(Calendar.MILLISECOND);
  today.clear(Calendar.SECOND);
  today.clear(Calendar.MINUTE);
  today.set(Calendar.HOUR_OF_DAY, 0);
  Date start = today.getTime();

  System.out.println(date);
  System.out.println(start);

  JSpinner spinner2 = new JSpinner(new SpinnerDateModel(date, start, null, Calendar.DAY_OF_MONTH));
  spinner2.setEditor(new JSpinner.DateEditor(spinner2, dateFormat));

  JSpinner spinner3 = new JSpinner(new SpinnerDateModel(date, start, null, Calendar.DAY_OF_MONTH));
  JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner3, dateFormat);
  spinner3.setEditor(editor);
  editor.getTextField().addFocusListener(new FocusAdapter() {
    @Override public void focusGained(FocusEvent e) {
      EventQueue.invokeLater(() -> {
        int i = dateFormat.lastIndexOf("dd");
        editor.getTextField().select(i, i + 2);
      });
    }
  });

  add(makeTitledPanel("Calendar.DAY_OF_MONTH", spinner1));
  add(makeTitledPanel("min: set(Calendar.HOUR_OF_DAY, 0)", spinner2));
  add(makeTitledPanel("JSpinner.DateEditor + FocusListener", spinner3));
  setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 12
Project: openScale   File: AlarmHandler.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static boolean isSameDate(Calendar c1, Calendar c2)
{
    int[] dateFields = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH};
    for (int dateField : dateFields)
    {
        if (c1.get(dateField) != c2.get(dateField)) return false;
    }
    return true;
}
 
Example 13
Project: callmeter   File: DataProvider.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
     * Get length of bill period.
     *
     * @param period period type
     * @return array of [Calendar.FIELD, amount]
     */
    private static int[] getPeriodSettings(final int period) {
        int f; // Calendar.FIELD
        int v; // amount
        /*
         * The values j and k are used to add a different type of period to
* the value, eg. you can have a period of one month and for each
* month you can add one day so you would have the same day-number
* as start and end of the period. (01.01.2012 - 01.02.2012,
* 02.02.2012 - 02.03.2012 and so on...)
*/
        int j = Calendar.MILLISECOND; // Additional Calendar.FIELD
        int k = 0; // Additional amount
        switch (period) {
            case BILLPERIOD_DAY:
                f = Calendar.DAY_OF_MONTH;
                v = 1;
                break;
            case BILLPERIOD_28D:
                f = Calendar.DAY_OF_MONTH;
                v = 28;
                break;
            case BILLPERIOD_30D:
                f = Calendar.DAY_OF_MONTH;
                v = 30;
                break;
            case BILLPERIOD_31D:
                f = Calendar.DAY_OF_MONTH;
                v = 31;
                break;
            case BILLPERIOD_60D:
                f = Calendar.DAY_OF_MONTH;
                v = 60;
                break;
            case BILLPERIOD_90D:
                f = Calendar.DAY_OF_MONTH;
                v = 90;
                break;
            case BILLPERIOD_1MONTH:
                f = Calendar.MONTH;
                v = 1;
                break;
            case BILLPERIOD_2MONTH:
                f = Calendar.MONTH;
                v = 2;
                break;
            case BILLPERIOD_3MONTH:
                f = Calendar.MONTH;
                v = 3;
                break;
            case BILLPERIOD_4MONTH:
                f = Calendar.MONTH;
                v = 4;
                break;
            case BILLPERIOD_5MONTH:
                f = Calendar.MONTH;
                v = 5;
                break;
            case BILLPERIOD_6MONTH:
                f = Calendar.MONTH;
                v = 6;
                break;
            case BILLPERIOD_12MONTH:
                f = Calendar.YEAR;
                v = 1;
                break;
            case BILLPERIOD_1MONTH_1DAY:
                f = Calendar.MONTH;
                v = 1;
                j = Calendar.DAY_OF_MONTH;
                k = 1;
                break;
            case BILLPERIOD_WEEK:
                f = Calendar.DAY_OF_MONTH;
                v = 7;
                break;
            case BILLPERIOD_14D:
                f = Calendar.DAY_OF_MONTH;
                v = 14;
                break;
            case BILLPERIOD_15D:
                f = Calendar.DAY_OF_MONTH;
                v = 15;
                break;
            default:
                f = Calendar.MONTH;
                v = 1;
                break;
        }

        return new int[]{f, v, j, k};
    }
 
Example 14
Project: drftpd   File: TimeManagerTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void resetDay(Date d) {
    _lastReset = Calendar.DAY_OF_MONTH;
}
 
Example 15
Project: evosql   File: DateTimeType.java    License: Apache License 2.0 4 votes vote down vote up
public int getPart(Session session, Object dateTime, int part) {

        int calendarPart;
        int increment = 0;
        int divisor   = 1;

        switch (part) {

            case Types.SQL_INTERVAL_YEAR :
                calendarPart = Calendar.YEAR;
                break;

            case Types.SQL_INTERVAL_MONTH :
                increment    = 1;
                calendarPart = Calendar.MONTH;
                break;

            case Types.SQL_INTERVAL_DAY :
            case DAY_OF_MONTH :
                calendarPart = Calendar.DAY_OF_MONTH;
                break;

            case Types.SQL_INTERVAL_HOUR :
                calendarPart = Calendar.HOUR_OF_DAY;
                break;

            case Types.SQL_INTERVAL_MINUTE :
                calendarPart = Calendar.MINUTE;
                break;

            case Types.SQL_INTERVAL_SECOND :
                calendarPart = Calendar.SECOND;
                break;

            case DAY_OF_WEEK :
                calendarPart = Calendar.DAY_OF_WEEK;
                break;

            case WEEK_OF_YEAR :
                calendarPart = Calendar.WEEK_OF_YEAR;
                break;

            case SECONDS_MIDNIGHT : {
                if (typeCode == Types.SQL_TIME
                        || typeCode == Types.SQL_TIME_WITH_TIME_ZONE) {}
                else {
                    try {
                        Type target = withTimeZone
                                      ? Type.SQL_TIME_WITH_TIME_ZONE
                                      : Type.SQL_TIME;

                        dateTime = target.castToType(session, dateTime, this);
                    } catch (HsqlException e) {}
                }

                return ((TimeData) dateTime).getSeconds();
            }
            case TIMEZONE_HOUR :
                if (typeCode == Types.SQL_TIMESTAMP_WITH_TIME_ZONE) {
                    return ((TimestampData) dateTime).getZone() / 3600;
                } else {
                    return ((TimeData) dateTime).getZone() / 3600;
                }
            case TIMEZONE_MINUTE :
                if (typeCode == Types.SQL_TIMESTAMP_WITH_TIME_ZONE) {
                    return ((TimestampData) dateTime).getZone() / 60 % 60;
                } else {
                    return ((TimeData) dateTime).getZone() / 60 % 60;
                }
            case QUARTER :
                increment    = 1;
                divisor      = 3;
                calendarPart = Calendar.MONTH;
                break;

            case DAY_OF_YEAR :
                calendarPart = Calendar.DAY_OF_YEAR;
                break;

            default :
                throw Error.runtimeError(ErrorCode.U_S0500,
                                         "DateTimeType - " + part);
        }

        long millis = getMillis(dateTime);

        return HsqlDateTime.getDateTimePart(millis, calendarPart) / divisor
               + increment;
    }
 
Example 16
Project: nebula   File: DateHelper.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public static int getCalendarTypeForString(String oneChar) {

		int calType = -1;

		switch (oneChar.charAt(0)) {
			case 'G':
				calType = Calendar.ERA;
				break;
			case 'y':
				calType = Calendar.YEAR;
				break;
			case 'M':
				calType = Calendar.MONTH;
				break;
			case 'd':
				calType = Calendar.DAY_OF_MONTH;
				break;
			case 'E':
				calType = Calendar.DAY_OF_WEEK;
				break;
			case 'D':
				calType = Calendar.DAY_OF_YEAR;
				break;
			case 'F':
				calType = Calendar.DATE;
				break;
			case 'h':
				calType = Calendar.HOUR;
				break;
			case 'm':
				calType = Calendar.MINUTE;
				break;
			case 's':
				calType = Calendar.SECOND;
				break;
			case 'S':
				calType = Calendar.MILLISECOND;
				break;
			case 'w':
				calType = Calendar.WEEK_OF_YEAR;
				break;
			case 'W':
				calType = Calendar.WEEK_OF_MONTH;
				break;
			case 'a':
				calType = Calendar.AM_PM;
				break;
			case 'k':
				calType = Calendar.HOUR_OF_DAY;
				break;
			case 'K':
				// ?
				break;
			case 'z':
				calType = Calendar.ZONE_OFFSET;
				break;
		}

		return calType;
	}
 
Example 17
Project: nebula   File: DateTimeFormatter.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Compiles a given edit pattern, initializing <code>inputMask</code> and
 * <code>inputCache</code>. The content of the edit pattern is analyzed char
 * by char and the array of field descriptors is initialized.
 * Pattern chars allowed are : y, M, d, H, h, s, S, a.
 * The presence of other pattern chars defined in <code>SimpleDateFormat</code>
 * will raised an <code>IllegalArgumentException</code>.
 *
 * @param editPattern edit pattern
 * @throws IllegalArgumentException pattern is invalid
 */
private void compile(String editPattern) {
	inputMask  = new StringBuffer();
	inputCache = new StringBuffer();
	fields = new FieldDesc[10];
	int fi = 0;
	int length = editPattern.length();
	for (int i = 0; i < length; i++) {
		char c = editPattern.charAt(i);
		int  l = 1;
		while ( i < length - 1 && editPattern.charAt(i + 1) == c ) {
			i++;
			l++;
		}
		isValidCharPattern(c);
		switch ( c ) {
			case 'y' :	// Year
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.YEAR;
				fields[fi].minLen = fields[fi].maxLen = l <= 2 ? 2 : 4;
				if ( fields[fi].maxLen == 2 ) {
					yearStart = -1;
				}
				break;
			case 'M' :	// Month
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.MONTH;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 'd' :	// Day in month
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.DAY_OF_MONTH;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 'H' :	// Hour (0-23)
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.HOUR_OF_DAY;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 'h' :	// Hour (1-12 AM-PM)
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.HOUR;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 'm' :	// Minutes
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.MINUTE;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 's' :	// Seconds
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.SECOND;
				fields[fi].minLen = Math.min(l, 2);
				fields[fi].maxLen = 2;
				break;
			case 'S' :	// Milliseconds
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.MILLISECOND;
				fields[fi].minLen = Math.min(l, 3);
				fields[fi].maxLen = 3;
				break;
			case 'a' :	// AM-PM marker
				fields[fi] = new FieldDesc();
				fields[fi].field = Calendar.AM_PM;
				fields[fi].minLen = fields[fi].maxLen = 2;
				break;
			default :
				for (int j = 0; j < l; j++) {
					inputMask.append('*');
					inputCache.append(c);
				}
				continue;
		}
     fields[fi].empty = true;
		fields[fi].valid = false;
		calendar.clear(fields[fi].field);
		char k = (char) ('0' + fi);
		for (int j = 0; j < fields[fi].minLen; j++) {
			inputMask.append(k);
			inputCache.append(SPACE);
		}
		fields[fi].index = k;
		fi++;
	}
	fieldCount = fi;
}
 
Example 18
Project: nebula   File: CDateTime.java    License: 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$
	}
}
 
Example 19
Project: nebula   File: DatePicker.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public int[] getFields() {
    return new int[] { Calendar.YEAR, Calendar.MONTH,
            Calendar.DAY_OF_MONTH };
}
 
Example 20
Project: PE-HFT-Java   File: DateTimeUtil.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static int[] POSIXTimeToDateInt(long [] timesMills, String formatStr) {
	int field = -1;
	if(formatStr.equals("yyyy")) {
		field = Calendar.YEAR;
	}
	else if (formatStr.equals("MM")) {
		field = Calendar.MONTH;
	}
	else if (formatStr.equals("dd")) {
		field = Calendar.DAY_OF_MONTH;
	}
	else if (formatStr.equals("uu")) {
		field = Calendar.DAY_OF_WEEK;
	}
	else if (formatStr.equals("HH")) {
		field = Calendar.HOUR_OF_DAY;
	}
	else if (formatStr.equals("mm")) {
		field = Calendar.MINUTE;
	}
	else if (formatStr.equals("ss")) {
		field = Calendar.SECOND;
	}
	else if (formatStr.equals("SSS")) {
		field = Calendar.MILLISECOND;
	}
	else {
		throw new NumberFormatException(MessageStrings.NUMBER_FORMAT);
	}
	
	Calendar dataBaseTime = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
	int []  vals = new int[timesMills.length];
			
	for (int i = 0; i < timesMills.length; i ++ ) {
		dataBaseTime.setTimeInMillis(timesMills[i]);
		vals[i] = dataBaseTime.get(field);
		if(field == Calendar.MONTH) {
			vals[i] += 1;
		}	
	}
	return vals;
}