Java Code Examples for java.util.Calendar.MONTH

The following are Jave code examples for showing how to use MONTH of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: asura   File: DateUtil.java   View Source Code Vote up 9 votes
private static int translate(final IntervalUnit unit) {
	switch (unit) {
	case DAY:
		return Calendar.DAY_OF_YEAR;
	case HOUR:
		return Calendar.HOUR_OF_DAY;
	case MINUTE:
		return Calendar.MINUTE;
	case MONTH:
		return Calendar.MONTH;
	case SECOND:
		return Calendar.SECOND;
	case MILLISECOND:
		return Calendar.MILLISECOND;
	case WEEK:
		return Calendar.WEEK_OF_YEAR;
	case YEAR:
		return Calendar.YEAR;
	default:
		throw new IllegalArgumentException("Unknown IntervalUnit");
	}
}
 
Example 2
Project: alfresco-data-model   File: SearchDateConversion.java   View Source Code Vote up 8 votes
/**
 * @param dateAndResolution
 * @return String date
 */
public static String getDateEnd(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.getActualMaximum(Calendar.MONTH));
        case Calendar.MONTH:
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        case Calendar.DAY_OF_MONTH:
            cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        case Calendar.HOUR_OF_DAY:
            cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        case Calendar.MINUTE:
            cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        case Calendar.SECOND:
            cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        case Calendar.MILLISECOND:
        default:
    }
    SimpleDateFormat formatter = CachingDateFormat.getSolrDatetimeFormat();
    formatter.setTimeZone(UTC_TIMEZONE);
    return formatter.format(cal.getTime());
}
 
Example 3
Project: neoscada   File: DateTimeDialog.java   View Source Code Vote up 7 votes
private Calendar makeCalendar ( final int[] is, final String[] strings )
{
    final Calendar c = Calendar.getInstance ();

    if ( is.length != strings.length )
    {
        throw new IllegalArgumentException ( "Array length is not equal. Internal error." );
    }

    for ( int i = 0; i < is.length; i++ )
    {
        if ( is[i] == Calendar.MONTH )
        {
            c.set ( is[i], Integer.parseInt ( strings[i] ) - 1 );
        }
        else
        {
            c.set ( is[i], Integer.parseInt ( strings[i] ) );
        }
    }
    return c;
}
 
Example 4
Project: oscm   File: TimerServiceBean.java   View Source Code Vote up 7 votes
/**
 * Determines the calendar field object that corresponds to the given
 * period.
 * 
 * @param period
 *            The specifie period.
 * @return The calendar field.
 */
private int getCalendarFieldForPeriod(Period period) {
    int field;
    switch (period) {
    case MONTH:
        field = Calendar.MONTH;
        break;
    case DAY:
        field = Calendar.DAY_OF_YEAR;
        break;
    default:
        // init the default period to day of year.
        field = Calendar.DAY_OF_YEAR;
    }
    return field;
}
 
Example 5
Project: oscm   File: TimeSlice.java   View Source Code Vote up 7 votes
/**
 * Operation is based on java util calendar in order to support daylight
 * saving times.
 */
public TimeSlice previous() {
    long newStart = start;
    long newEnd = end;
    Calendar temp = Calendar.getInstance();
    temp.setTimeInMillis(start);
    int field = 0;
    switch (period) {
    case HOUR:
        field = Calendar.HOUR_OF_DAY;
        break;
    case DAY:
        field = Calendar.DAY_OF_MONTH;
        break;
    case WEEK:
        field = Calendar.WEEK_OF_YEAR;
        break;
    case MONTH:
        field = Calendar.MONTH;
        break;
    default:
        break;
    }
    temp.add(field, -1);
    newStart = temp.getTimeInMillis();
    temp.setTimeInMillis(end);
    temp.add(field, -1);
    newEnd = temp.getTimeInMillis();
    return new TimeSlice(newStart, newEnd, period);
}
 
Example 6
Project: parabuild-ci   File: DateTickUnit.java   View Source Code Vote up 7 votes
/**
 * Returns a field code (that can be used with the Calendar class) for a given 'unit' code.
 * The 'unit' is one of:  YEAR, MONTH, DAY, HOUR, MINUTE, SECOND and MILLISECOND.
 *
 * @param unit  the unit.
 *
 * @return the field code.
 */
private int getCalendarField(int unit) {

    switch (unit) {
        case (YEAR):
            return Calendar.YEAR;
        case (MONTH):
            return Calendar.MONTH;
        case (DAY):
            return Calendar.DATE;
        case (HOUR):
            return Calendar.HOUR_OF_DAY;
        case (MINUTE):
            return Calendar.MINUTE;
        case (SECOND):
            return Calendar.SECOND;
        case (MILLISECOND):
            return Calendar.MILLISECOND;
        default:
            return Calendar.MILLISECOND;
    }

}
 
Example 7
Project: parabuild-ci   File: DateTickUnit.java   View Source Code Vote up 7 votes
/**
 * Returns a field code (that can be used with the Calendar class) for a 
 * given 'unit' code.  The 'unit' is one of:  {@link #YEAR}, {@link #MONTH},
 * {@link #DAY}, {@link #HOUR}, {@link #MINUTE}, {@link #SECOND} and 
 * {@link #MILLISECOND}.
 *
 * @param tickUnit  the unit.
 *
 * @return The field code.
 */
private int getCalendarField(int tickUnit) {

    switch (tickUnit) {
        case (YEAR):
            return Calendar.YEAR;
        case (MONTH):
            return Calendar.MONTH;
        case (DAY):
            return Calendar.DATE;
        case (HOUR):
            return Calendar.HOUR_OF_DAY;
        case (MINUTE):
            return Calendar.MINUTE;
        case (SECOND):
            return Calendar.SECOND;
        case (MILLISECOND):
            return Calendar.MILLISECOND;
        default:
            return Calendar.MILLISECOND;
    }

}
 
Example 8
Project: abhot   File: RelativeTime.java   View Source Code Vote up 6 votes
public long getTimeRelativeTo(long time)
{
	int valueToUse = -(int) value;
	int field = 0;
	if (getUnit() == TimeUnit.MILLISECONDS)
	{
		field = Calendar.MILLISECOND;
		//we do our own adjustment as calendar cannot handle a long.
		time -= value;
		valueToUse = 0;
	}
	else if (getUnit() == TimeUnit.SECONDS)
		field = Calendar.SECOND;
	else if (getUnit() == TimeUnit.MINUTES)
		field = Calendar.MINUTE;
	else if (getUnit() == TimeUnit.HOURS)
		field = Calendar.HOUR;
	else if (getUnit() == TimeUnit.DAYS)
		field = Calendar.DATE;
	else if (getUnit() == TimeUnit.WEEKS)
		field = Calendar.WEEK_OF_MONTH;
	else if (getUnit() == TimeUnit.MONTHS)
		field = Calendar.MONTH;
	else if (getUnit() == TimeUnit.YEARS)
		field = Calendar.YEAR;

	calendar.setTimeInMillis(time);
	calendar.add(field, valueToUse);

	return calendar.getTime().getTime();
}
 
Example 9
Project: abhot   File: RelativeTime.java   View Source Code Vote up 6 votes
public long getFutureTimeRelativeTo(long time)
{
	int valueToUse = (int) value;
	int field = 0;
	if (getUnit() == TimeUnit.MILLISECONDS)
	{
		field = Calendar.MILLISECOND;
		//we do our own adjustment as calendar cannot handle a long.
		time += value;
		valueToUse = 0;
	}
	else if (getUnit() == TimeUnit.SECONDS)
		field = Calendar.SECOND;
	else if (getUnit() == TimeUnit.MINUTES)
		field = Calendar.MINUTE;
	else if (getUnit() == TimeUnit.HOURS)
		field = Calendar.HOUR;
	else if (getUnit() == TimeUnit.DAYS)
		field = Calendar.DATE;
	else if (getUnit() == TimeUnit.WEEKS)
		field = Calendar.WEEK_OF_MONTH;
	else if (getUnit() == TimeUnit.MONTHS)
		field = Calendar.MONTH;
	else if (getUnit() == TimeUnit.YEARS)
		field = Calendar.YEAR;

	calendar.setTimeInMillis(time);
	calendar.add(field, valueToUse);

	return calendar.getTime().getTime();
}
 
Example 10
Project: rapidminer   File: DateAdjust.java   View Source Code Vote up 6 votes
public Adjustment(int field, int amount) {
	this.originalField = field;

	switch (field) {
		case CALENDAR_FIELD_YEAR:
			this.calendarField = Calendar.YEAR;
			break;
		case CALENDAR_FIELD_MONTH:
			this.calendarField = Calendar.MONTH;
			break;
		case CALENDAR_FIELD_DAY:
			this.calendarField = Calendar.DAY_OF_YEAR;
			break;
		case CALENDAR_FIELD_HOUR:
			this.calendarField = Calendar.HOUR_OF_DAY;
			break;
		case CALENDAR_FIELD_MINUTE:
			this.calendarField = Calendar.MINUTE;
			break;
		case CALENDAR_FIELD_SECOND:
			this.calendarField = Calendar.SECOND;
			break;
		case CALENDAR_FIELD_MILLISECOND:
			this.calendarField = Calendar.MILLISECOND;
			break;
	}

	this.amount = amount;
}
 
Example 11
Project: parabuild-ci   File: DailyPersistentTestStatsRetriever.java   View Source Code Vote up 6 votes
protected StatisticsRetrieverConfiguration getConfiguration() {
  final int rollerInitTrauncateTo = Calendar.DAY_OF_MONTH;
  final int statisticsSize = DEFAULT_STATS_DAYS;
  final int rollerStep = Calendar.DAY_OF_MONTH;
  final int cutOffBefore = Calendar.MONTH;
  return new StatisticsRetrieverConfiguration(rollerInitTrauncateTo,
          statisticsSize,
          rollerStep,
          cutOffBefore);
}
 
Example 12
Project: elasticsearch_my   File: DateField.java   View Source Code Vote up 6 votes
static ValueSource getMethod(IndexFieldData<?> fieldData, String fieldName, String method) {
    switch (method) {
        case GETVALUE_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.MIN);
        case ISEMPTY_METHOD:
            return new EmptyMemberValueSource(fieldData);
        case SIZE_METHOD:
            return new CountMethodValueSource(fieldData);
        case MINIMUM_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.MIN);
        case MAXIMUM_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.MAX);
        case AVERAGE_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.AVG);
        case MEDIAN_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.MEDIAN);
        case SUM_METHOD:
            return new FieldDataValueSource(fieldData, MultiValueMode.SUM);
        case COUNT_METHOD:
            return new CountMethodValueSource(fieldData);
        case GET_YEAR_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.YEAR);
        case GET_MONTH_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.MONTH);
        case GET_DAY_OF_MONTH_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.DAY_OF_MONTH);
        case GET_HOUR_OF_DAY_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.HOUR_OF_DAY);
        case GET_MINUTES_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.MINUTE);
        case GET_SECONDS_METHOD:
            return new DateMethodValueSource(fieldData, MultiValueMode.MIN, method, Calendar.SECOND);
        default:
            throw new IllegalArgumentException("Member method [" + method + "] does not exist for date field [" + fieldName + "].");
    }
}
 
Example 13
Project: parabuild-ci   File: MonthlyPersistentBuildStatsRetriever.java   View Source Code Vote up 6 votes
protected StatisticsRetrieverConfiguration getConfiguration() {
  final int rollerInitTrauncateTo = Calendar.MONTH;
  final int statisticsSize = DEFAULT_STATS_MONTHS;
  final int rollerStep = Calendar.MONTH;
  final int cutOffBefore = Calendar.YEAR;
  return new StatisticsRetrieverConfiguration(rollerInitTrauncateTo,
          statisticsSize,
          rollerStep,
          cutOffBefore);
}
 
Example 14
Project: mumu   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 根据单位字段比较两个日期
 * 
 * @param date
 *            日期1
 * @param otherDate
 *            日期2
 * @param withUnit
 *            单位字段,从Calendar field取值
 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值
 */
public static int compareDate(Date date, Date otherDate, int withUnit) {
	Calendar dateCal = Calendar.getInstance();
	dateCal.setTime(date);
	Calendar otherDateCal = Calendar.getInstance();
	otherDateCal.setTime(otherDate);

	switch (withUnit) {
	case Calendar.YEAR:
		dateCal.clear(Calendar.MONTH);
		otherDateCal.clear(Calendar.MONTH);
	case Calendar.MONTH:
		dateCal.set(Calendar.DATE, 1);
		otherDateCal.set(Calendar.DATE, 1);
	case Calendar.DATE:
		dateCal.set(Calendar.HOUR_OF_DAY, 0);
		otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
	case Calendar.HOUR:
		dateCal.clear(Calendar.MINUTE);
		otherDateCal.clear(Calendar.MINUTE);
	case Calendar.MINUTE:
		dateCal.clear(Calendar.SECOND);
		otherDateCal.clear(Calendar.SECOND);
	case Calendar.SECOND:
		dateCal.clear(Calendar.MILLISECOND);
		otherDateCal.clear(Calendar.MILLISECOND);
	case Calendar.MILLISECOND:
		break;
	default:
		throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
	}
	return dateCal.compareTo(otherDateCal);
}
 
Example 15
Project: Remindy   File: TaskUtil.java   View Source Code Vote up 6 votes
private static int getDateFieldFromRepeatType(ReminderRepeatType repeatType) {

        switch (repeatType) {
            case DAILY: return Calendar.DAY_OF_MONTH;
            case WEEKLY: return Calendar.WEEK_OF_YEAR;
            case MONTHLY: return Calendar.MONTH;
            case YEARLY: return Calendar.YEAR;
            default: throw new InvalidParameterException("Invalid RepeatType parameter in TaskUtil.getRepeatingReminderEndCalendar()");
        }
    }
 
Example 16
Project: TimeAndMoney   File: TimeUnit.java   View Source Code Vote up 6 votes
int javaCalendarConstantForBaseType() {
    if (baseType.equals(Type.millisecond))
        return Calendar.MILLISECOND;
    if (baseType.equals(Type.month))
        return Calendar.MONTH;
    return 0;
}
 
Example 17
Project: parabuild-ci   File: StatisticsUtils.java   View Source Code Vote up 6 votes
/**
   * @param date
   * @param truncateAt
   */
  public static Date truncateDate(final Date date, final int truncateAt) {
    final Calendar source = Calendar.getInstance();
    source.clear();
    source.setTime(date);
    final Calendar result = Calendar.getInstance();
    result.clear();
    result.set(Calendar.YEAR, source.get(Calendar.YEAR));
    final TimeZone sourceTimeZone = source.getTimeZone();
    result.setTimeZone(sourceTimeZone);
    if (truncateAt == Calendar.HOUR_OF_DAY) {
      result.set(Calendar.HOUR_OF_DAY, source.get(Calendar.HOUR_OF_DAY));
      result.set(Calendar.MONTH, source.get(Calendar.MONTH));
      result.set(Calendar.DAY_OF_MONTH, source.get(Calendar.DAY_OF_MONTH));
      result.set(Calendar.DST_OFFSET, source.get(Calendar.DST_OFFSET));
    } else if (truncateAt == Calendar.DAY_OF_MONTH) {
      result.set(Calendar.MONTH, source.get(Calendar.MONTH));
      result.set(Calendar.DAY_OF_MONTH, source.get(Calendar.DAY_OF_MONTH));
//      if (log.isDebugEnabled()) log.debug("result.get(Calendar.HOUR_OF_DAY): " + result.get(Calendar.HOUR_OF_DAY));
    } else if (truncateAt == Calendar.MONTH) {
      result.set(Calendar.MONTH, source.get(Calendar.MONTH));
    } else if (truncateAt == Calendar.YEAR) { // NOPMD
      // do nothing
    } else { // NOPMD
      throw new IllegalArgumentException("Unknown truncateAt");
    }
    return result.getTime();
  }
 
Example 18
Project: NeverLag   File: DateUtils.java   View Source Code Vote up 6 votes
public static String formatDateDiff(Calendar fromDate, Calendar toDate) {
	boolean future = false;
	if (toDate.equals(fromDate)) {
		return I18N.tr("now");
	}
	if (toDate.after(fromDate)) {
		future = true;
	}
	StringBuilder sb = new StringBuilder();
	int[] types = { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
	String[] names = {
		I18N.tr("year"), I18N.tr("years"),
		I18N.tr("month"), I18N.tr("months"),
		I18N.tr("day"), I18N.tr("days"),
		I18N.tr("hour"), I18N.tr("hours"),
		I18N.tr("minute"), I18N.tr("minutes"),
		I18N.tr("second"), I18N.tr("seconds")
	};
	int accuracy = 0;
	for (int i = 0; i < types.length; i++) {
		if (accuracy > 2) {
			break;
		}
		int diff = dateDiff(types[i], fromDate, toDate, future);
		if (diff > 0) {
			accuracy++;
			sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
		}
	}
	if (sb.length() == 0) {
		return "now";
	}
	return sb.toString().trim();
}
 
Example 19
Project: lams   File: DateUtils.java   View Source Code Vote up 5 votes
/**
 * Calendar-version for fragment-calculation in any unit
 * 
 * @param calendar the calendar to work with, not null
 * @param fragment the Calendar field part of calendar to calculate 
 * @param unit Calendar field defining the unit
 * @return number of units within the fragment of the calendar
 * @throws IllegalArgumentException if the date is <code>null</code> or 
 * fragment is not supported
 * @since 2.4
 */
private static long getFragment(Calendar calendar, int fragment, int unit) {
    if(calendar == null) {
        throw  new IllegalArgumentException("The date must not be null"); 
    }
    long millisPerUnit = getMillisPerUnit(unit);
    long result = 0;
    
    // Fragments bigger than a day require a breakdown to days
    switch (fragment) {
        case Calendar.YEAR:
            result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit;
            break;
        case Calendar.MONTH:
            result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit;
            break;
    }

    switch (fragment) {
        // Number of days already calculated for these cases
        case Calendar.YEAR:
        case Calendar.MONTH:
        
        // The rest of the valid cases
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.HOUR_OF_DAY:
            result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.MINUTE:
            result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit;
            //$FALL-THROUGH$
        case Calendar.SECOND:
            result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit;
            break;
        case Calendar.MILLISECOND: 
            break;//never useful
        default: 
            throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
    }
    return result;
}
 
Example 20
Project: date-helper   File: DateHelper.java   View Source Code Vote up 4 votes
/**
 * Liest das gegebene Feld aus dem gegebenen {@link Date} Objekt und gibt
 * dieses zurück.
 *
 * @param field das zu lesende Feld. Muss eines von {@link Calendar#YEAR},
 * {@link Calendar#MONTH}, {@link Calendar#DAY_OF_YEAR},
 * {@link Calendar#DAY_OF_MONTH}, {@link Calendar#DAY_OF_WEEK},
 * {@link Calendar#DAY_OF_WEEK_IN_MONTH}, {@link Calendar#HOUR},
 * {@link Calendar#HOUR_OF_DAY}, {@link Calendar#MINUTE},
 * {@link Calendar#SECOND}, {@link Calendar#MILLISECOND} sein.
 * @param date das Datum von welchem gelesen werden soll.
 * @return das gewünschte Feld
 */
public static int get(int field, Date date) {

	Calendar cal = getCalendar(date);
	int retVal = cal.get(field);
	if (Calendar.MONTH == field) {
		// Calendar.MONTH geht von 0-11
		retVal++;
	}
	return retVal;
}