Java Code Examples for java.util.Calendar.WEEK_OF_YEAR

The following are Jave code examples for showing how to use WEEK_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: 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 3
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 4
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 5
Project: wisebite_android   File: RestaurantService.java   View Source Code Vote up 6 votes
private boolean checkTime(Date date, int kind) {
    Calendar currentCalendar = Calendar.getInstance();
    currentCalendar.setTime(Utils.getAnalyticsDate());

    Calendar orderCalendar = Calendar.getInstance();
    orderCalendar.setTime(date);
    switch (kind) {
        case Calendar.DATE:
            if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
                    orderCalendar.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH) &&
                    orderCalendar.get(Calendar.DATE) == currentCalendar.get(Calendar.DAY_OF_MONTH)) {
                return true;
            }
            break;
        case Calendar.WEEK_OF_YEAR:
            if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
                    orderCalendar.get(Calendar.WEEK_OF_YEAR) == currentCalendar.get(Calendar.WEEK_OF_YEAR)) {
                return true;
            }
            break;
        case Calendar.MONTH:
            if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
                    orderCalendar.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH)) {
                return true;
            }
            break;
        default:
            return false;
    }
    return false;
}
 
Example 6
Project: HueSense   File: DateAxis.java   View Source Code Vote up 5 votes
/**
 * Makes dates even, in the sense of that years always begin in January,
 * months always begin on the 1st and days always at midnight.
 *
 * @param dates The list of dates.
 * @return The new list of dates.
 */
private List<Date> makeDatesEven(List<Date> dates, Calendar calendar) {
    // If the dates contain more dates than just the lower and upper bounds, make the dates in between even.
    if (dates.size() > 2) {
        List<Date> evenDates = new ArrayList<>();

        // For each interval, modify the date slightly by a few millis, to make sure they are different days.
        // This is because Axis stores each value and won't update the tick labels, if the value is already known.
        // This happens if you display days and then add a date many years in the future the tick label will still be displayed as day.
        for (int i = 0; i < dates.size(); i++) {
            calendar.setTime(dates.get(i));
            switch (actualInterval.interval) {
                case Calendar.YEAR:
                    // If its not the first or last date (lower and upper bound), make the year begin with first month and let the months begin with first day.
                    if (i != 0 && i != dates.size() - 1) {
                        calendar.set(Calendar.MONTH, 0);
                        calendar.set(Calendar.DATE, 1);
                    }
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 6);
                    break;
                case Calendar.MONTH:
                    // If its not the first or last date (lower and upper bound), make the months begin with first day.
                    if (i != 0 && i != dates.size() - 1) {
                        calendar.set(Calendar.DATE, 1);
                    }
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 5);
                    break;
                case Calendar.WEEK_OF_YEAR:
                    // Make weeks begin with first day of week?
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 4);
                    break;
                case Calendar.DATE:
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 3);
                    break;
                case Calendar.HOUR:
                    if (i != 0 && i != dates.size() - 1) {
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                    }
                    calendar.set(Calendar.MILLISECOND, 2);
                    break;
                case Calendar.MINUTE:
                    if (i != 0 && i != dates.size() - 1) {
                        calendar.set(Calendar.SECOND, 0);
                    }
                    calendar.set(Calendar.MILLISECOND, 1);
                    break;
                case Calendar.SECOND:
                    calendar.set(Calendar.MILLISECOND, 0);
                    break;

            }
            evenDates.add(calendar.getTime());
        }

        return evenDates;
    } else {
        return dates;
    }
}
 
Example 7
Project: wisebite_android   File: RestaurantService.java   View Source Code Vote up 5 votes
public PieChartData getAllOrdersCount(String restaurantId, int kind) {
    float[] time;
    if (kind == Calendar.DATE) time = new float[24];
    else if (kind == Calendar.WEEK_OF_YEAR) time = new float[7];
    else time = new float[31];
    for (int i = 0; i < time.length; i++) time[i] = 0;

    List<String> ordersList = getOrders(restaurantId);

    Calendar calendar = Calendar.getInstance();
    Order order;
    for (String orderKey : ordersList) {
        order = orderRepository.get(orderKey);
        if (checkTime(order.getDate(), kind)) {
            calendar.setTime(order.getDate());
            if (kind == Calendar.DATE)
                time[calendar.get(Calendar.HOUR_OF_DAY)] += 1.0f;
            else if (kind == Calendar.WEEK_OF_YEAR)
                time[calendar.get(Calendar.DAY_OF_WEEK)-1] += 1.0f;
            else
                time[calendar.get(Calendar.DAY_OF_MONTH)] += 1.0f;
        }
    }

    Integer count = 0;
    for (float t : time) if (t != 0) ++count;

    PieChartData data = new PieChartData(count);
    String[] xData = data.getXData();
    float[] yData = data.getYData();

    Integer j = 0;
    for (int i = 0; i < time.length; i++) {
        if (time[i] != 0) {
            yData[j] = time[i];


            String label = "";
            if (kind == Calendar.DATE) {
                if (i < 10) label += '0';
                label += String.valueOf(i) + "h - ";
                if (i + 1 < 10) label += '0';
                label += String.valueOf(i + 1) + 'h';
            } else if (kind == Calendar.WEEK_OF_YEAR) {
                if (i == 0) label = "Sunday";
                else if (i == 1) label = "Monday";
                else if (i == 2) label = "Tuesday";
                else if (i == 3) label = "Wednesday";
                else if (i == 4) label = "Thursday";
                else if (i == 5) label = "Friday";
                else if (i == 6) label = "Saturday";
            } else {
                label = "Day number " + String.valueOf(i) + " of the month.";
            }
            xData[j] = label;

            ++j;
        }
    }

    data.setXData(xData);
    data.setYData(yData);
    return data;
}