Java Code Examples for java.util.Calendar.DAY_OF_YEAR

The following are Jave code examples for showing how to use DAY_OF_YEAR 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: SamaGamesAPI   File: Misc.java   View Source Code Vote up 7 votes
/**
 * Parse a raw phrase into a date {@link Date}
 *
 * @param str Raw phrase
 *
 * @return Date object
 */
public static Date parseTime(String str)
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());

    for (String t : str.split("\\+"))
    {
        String[] end = t.split(":");
        int type;

        if (end[1].equalsIgnoreCase("d"))
            type = Calendar.DAY_OF_YEAR;
        else if (end[1].equalsIgnoreCase("h"))
            type = Calendar.HOUR;
        else if (end[1].equalsIgnoreCase("m"))
            type = Calendar.MINUTE;
        else
            type = Calendar.SECOND;

        cal.add(type, Integer.parseInt(end[0]));
    }
    return cal.getTime();
}
 
Example 3
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 4
Project: lams   File: DateUtils.java   View Source Code Vote up 7 votes
/**
 * 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
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 6
Project: lams   File: DateBuilder.java   View Source Code Vote up 6 votes
private static int translate(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 7
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;
}