Java Code Examples for java.util.Calendar.DATE

The following are Jave code examples for showing how to use DATE 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: 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 2
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 3
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 4
Project: ipayquery   File: DateUtil.java   View Source Code Vote up 7 votes
/**
 * 获取时间间隔类型
 *
 * @param field
 *            时间间隔类型
 * @return 日历的时间间隔
 */
protected static int getInterval(String field) {
    String tmpField = field.toUpperCase();
    if (DEFAULT_YEAR.equals(tmpField)) {
        return Calendar.YEAR;
    } else if (DEFAULT_MONTH.equals(tmpField)) {
        return Calendar.MONTH;
    } else if (DEFAULT_DATE.equals(tmpField)) {
        return Calendar.DATE;
    } else if (DEFAULT_HOUR.equals(tmpField)) {
        return Calendar.HOUR;
    } else if (DEFAULT_MINUTE.equals(tmpField)) {
        return Calendar.MINUTE;
    } else {
        return Calendar.SECOND;
    }
}
 
Example 5
Project: wcs-android-sdk   File: DateUtil.java   View Source Code Vote up 6 votes
public static long dateDiff(Date startTime, Date endTime, int type) {
    long result = 0;
    long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
    long nh = 1000 * 60 * 60;// 一小时的毫秒数
    long nm = 1000 * 60;// 一分钟的毫秒数
    long ns = 1000;// 一秒钟的毫秒数
    long diff;
    try {
        // 获得两个时间的毫秒时间差异
        diff = endTime.getTime() - startTime.getTime();
        switch (type) {
            case Calendar.DATE:
                result = diff / nd;// 计算差多少天
                break;
            case Calendar.HOUR:
                result = diff % nd / nh;// 计算差多少小时
                break;
            case Calendar.MINUTE:
                result = diff % nd % nh / nm;// 计算差多少分钟
                break;
            case Calendar.SECOND:
                result = diff % nd % nh % nm / ns;// 计算差多少秒
                break;
            default:
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 6
Project: xbin-store   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 7
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 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: 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 11
Project: mumu-core   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 12
Project: MainCalendar   File: LunarCalendar.java   View Source Code Vote up 5 votes
/**
 * 使用农历日期计算出公历日期
 */
private void computeGregorianFields() {
    int y = lunarYear;
    int m = lunarMonth;
    int d = lunarDate;

    areLunarFieldsComputed = true;
    areFieldsSet = true;
    lastSetLunar = false;

    // 调整日期范围
    if (y < 1900) {
        y = 1899;
    }
    else if (y > 2100) {
        y = 2101;
    }
    
    if (m < -12) {
        m = -12;
    }
    else if (m > 12) {
        m = 12;
    }

    if (d < 1) {
        d = 1;
    }
    else if (d > 30) {
        d = 30;
    }

    int dateInt = y * 10000 + Math.abs(m) * 100 + d;
    if (dateInt < 19001111) {
        // 太小
        set(1901, Calendar.JANUARY, 1);
        super.complete();
    } else if (dateInt > 21001201) {
        // 太大
        set(2100, Calendar.DECEMBER, 31);
        super.complete();
    } else {
        if (Math.abs(m) > 12) {
            m = 12;
        }

        int days = LunarCalendar.daysInLunarMonth(y, m);
        if (days == 0) {
            m = -m;
            days = LunarCalendar.daysInLunarMonth(y, m);
        }

        if (d > days) {
            d = days;
        }

        set(y, Math.abs(m) - 1, d);
        computeLunarFields();

        int amount = 0;
        while (lunarYear != y || lunarMonth != m) {
            amount += daysInLunarMonth(lunarYear, lunarMonth);
            lunarMonth = nextLunarMonth(lunarYear, lunarMonth);
        }

        amount += d - lunarDate;
        super.add(Calendar.DATE, amount);
    }

    computeLunarFields();
}
 
Example 13
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 14
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 15
Project: Dispatch   File: CrisisManager.java   View Source Code Vote up 5 votes
public void setArrivalTime(String crisisID){
    String currentDateTimeString = "" + Calendar.DATE + Calendar.getInstance().getTime();
    mDatabase.child("crisis").child(crisisID).child("arrivalTime").setValue(currentDateTimeString);
}
 
Example 16
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;
}