Java Code Examples for java.util.Calendar#DAY_OF_YEAR

The following examples show how to use java.util.Calendar#DAY_OF_YEAR . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of milliseconds of a {@code Calendar} field, if this is a constant value.
 * This handles millisecond, second, minute, hour and day (even though days can very in length).
 * 
 * @param unit  a {@code Calendar} field constant which is a valid unit for a fragment
 * @return the number of milliseconds in the field
 * @throws IllegalArgumentException if date can't be represented in milliseconds
 * @since 2.4 
 */
private static long getMillisPerUnit(int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 2
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of milliseconds of a {@code Calendar} field, if this is a constant value.
 * This handles millisecond, second, minute, hour and day (even though days can very in length).
 * 
 * @param unit  a {@code Calendar} field constant which is a valid unit for a fragment
 * @return the number of milliseconds in the field
 * @throws IllegalArgumentException if date can't be represented in milliseconds
 * @since 2.4 
 */
private static long getMillisPerUnit(final int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 3
Source Project: lams   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of millis of a datefield, if this is a constant value
 * 
 * @param unit A Calendar field which is a valid unit for a fragment
 * @return number of millis
 * @throws IllegalArgumentException if date can't be represented in millisenconds
 * @since 2.4 
 */
private static long getMillisPerUnit(int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 4
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of millis of a datefield, if this is a constant value
 * 
 * @param unit A Calendar field which is a valid unit for a fragment
 * @return number of millis
 * @throws IllegalArgumentException if date can't be represented in millisenconds
 * @since 2.4 
 */
private static long getMillisPerUnit(int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 5
Source Project: eagle   File: DateTimeUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * this could be accurate only when timezone is UTC
 * for the timezones other than UTC, there is possibly issue, for example
 * assume timezone is GMT+8 in China
 * When user time is "2014-07-15 05:00:00", it will be converted to timestamp first,
 * internally it would be  "2014-07-14 21:00:00" in UTC timezone. When rounded down to day, the internal time would
 * be changed to "2014-07-14 00:00:00", and that means the user time is "2014-07-14 08:00:00".
 * But originally user wants to round it to "2014-07-15 00:00:00"
 *
 * @param timeInMillis the seconds elapsed since 1970-01-01 00:00:00
 */
public static long roundDown(int field, long timeInMillis) {
    switch (field) {
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_WEEK:
        case Calendar.DAY_OF_YEAR:
            return (timeInMillis - timeInMillis % (24 * 60 * 60 * 1000));
        case Calendar.HOUR:
            return (timeInMillis - timeInMillis % (60 * 60 * 1000));
        case Calendar.MINUTE:
            return (timeInMillis - timeInMillis % (60 * 1000));
        case Calendar.SECOND:
            return (timeInMillis - timeInMillis % (1000));
        default:
            return 0L;
    }
}
 
Example 6
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of millis of a datefield, if this is a constant value
 * 
 * @param unit A Calendar field which is a valid unit for a fragment
 * @return number of millis
 * @throws IllegalArgumentException if date can't be represented in millisenconds
 * @since 2.4 
 */
private static long getMillisPerUnit(int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 7
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the number of milliseconds of a {@code Calendar} field, if this is a constant value.
 * This handles millisecond, second, minute, hour and day (even though days can very in length).
 * 
 * @param unit  a {@code Calendar} field constant which is a valid unit for a fragment
 * @return the number of milliseconds in the field
 * @throws IllegalArgumentException if date can't be represented in milliseconds
 * @since 2.4 
 */
private static long getMillisPerUnit(int unit) {
    long result = Long.MAX_VALUE;
    switch (unit) {
        case Calendar.DAY_OF_YEAR:
        case Calendar.DATE:
            result = MILLIS_PER_DAY;
            break;
        case Calendar.HOUR_OF_DAY:
            result = MILLIS_PER_HOUR;
            break;
        case Calendar.MINUTE:
            result = MILLIS_PER_MINUTE;
            break;
        case Calendar.SECOND:
            result = MILLIS_PER_SECOND;
            break;
        case Calendar.MILLISECOND:
            result = 1;
            break;
        default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
    }
    return result;
}
 
Example 8
Source Project: development   File: TimerServiceBean.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 9
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 10
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
Source Project: das   File: TimeRangeShardLocator.java    License: Apache License 2.0 4 votes vote down vote up
TimeRangeShardLocator(TimeRangeStrategy.TIME_PATTERN timePattern) {
    switch (timePattern){
        case WEEK_OF_YEAR:
            field = Calendar.WEEK_OF_YEAR;
            range = Range.atLeast(1);
            period = DAY * 366L;
            break;
        case WEEK_OF_MONTH:
            field = Calendar.WEEK_OF_MONTH;
            range = Range.atLeast(1);
            period = DAY * 31L;
            break;
        case DAY_OF_MONTH:
            field = Calendar.DAY_OF_MONTH;
            range = Range.atLeast(1);
            period = DAY * 31L;
            break;
        case DAY_OF_YEAR:
            field = Calendar.DAY_OF_YEAR;
            range = Range.atLeast(1);
            period = DAY * 366L;
            break;
        case DAY_OF_WEEK:
            field = Calendar.DAY_OF_WEEK;
            range = Range.closed(1, 7);
            period = DAY * 7;
            break;
        case HOUR_OF_DAY:
            field = Calendar.HOUR_OF_DAY;
            range = Range.closed(0, 23);
            period = DAY;
            break;
        case MINUTE_OF_HOUR:
            field = Calendar.MINUTE;
            range = Range.closed(0, 59);
            period = HOUR;
            break;
        case SECOND_OF_MINUTE:
            field = Calendar.SECOND;
            range = Range.closed(0, 59);
            period = MINUTE;
            break;
    }
}
 
Example 12
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets a Calendar fragment for any unit.
 * 
 * @param calendar the calendar to work with, not null
 * @param fragment the Calendar field part of calendar to calculate 
 * @param unit the {@code 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(final Calendar calendar, final int fragment, final int unit) {
    if(calendar == null) {
        throw  new IllegalArgumentException("The date must not be null"); 
    }
    final 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 13
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 14
Source Project: coming   File: Lang_21_DateUtils_t.java    License: MIT License 4 votes vote down vote up
/**
 * 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 15
Source 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 16
Source Project: astor   File: DateUtils.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 17
Source Project: yandex-money-sdk-java   File: Days.java    License: MIT License 4 votes vote down vote up
@Override
public int getField() {
    return Calendar.DAY_OF_YEAR;
}
 
Example 18
Source Project: lams   File: DateUtils.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 19
Source Project: coming   File: Lang_21_DateUtils_s.java    License: MIT License 4 votes vote down vote up
/**
 * 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
Source 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;
    }