java.util.Calendar#MINUTE源码实例Demo

下面列出了java.util.Calendar#MINUTE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: astor   文件: DateUtils.java
/**
 * 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;
}
 
源代码2 项目: sakai   文件: Utilities.java
/**
 * Calculate the time according to the input parameters.
 * 
 * @param date
 *            a Date object.
 * @param time
 *            an int value.
 * @param dateType
 *            a string value.
 * @return a converted Date object according to the input parameters.
 */
public static Date subTractTimeToDate(Date date, int time, String dateType) {
	if (time == 0)
		return date;

	int type = -1;
	if (dateType.equals(MINUTES))
		type = Calendar.MINUTE;
	else if (dateType.equals(HOURS))
		type = Calendar.HOUR;
	else if (dateType.equals(START_NOW)){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}else {// days{
		time = 24 * time; // convert to hours
		type = Calendar.HOUR;
	}
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.add(type, -1 * time);

	return calendar.getTime();
}
 
源代码3 项目: coming   文件: Lang_21_DateUtils_t.java
/**
 * 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;
}
 
源代码4 项目: sakai   文件: Utilities.java
/**
 * Calculate the time according to the input parameters.
 * 
 * @param date
 *            a Date object.
 * @param time
 *            an int value.
 * @param dateType
 *            a string value.
 * @return a converted Date object according to the input parameters.
 */
public static Date subTractTimeToDate(Date date, int time, String dateType) {
	if (time == 0)
		return date;

	int type = -1;
	if (dateType.equals(MINUTES))
		type = Calendar.MINUTE;
	else if (dateType.equals(HOURS))
		type = Calendar.HOUR;
	else if (dateType.equals(START_NOW)){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}else {// days{
		time = 24 * time; // convert to hours
		type = Calendar.HOUR;
	}
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date);
	calendar.add(type, -1 * time);

	return calendar.getTime();
}
 
源代码5 项目: datacollector   文件: PathResolver.java
@ElFunction(name = "every")
public static int every(@ElParam("value") int value, @ElParam("unit") int unit) {
  switch (unit){
    case Calendar.YEAR:
    case Calendar.MONTH:
    case Calendar.DAY_OF_MONTH:
    case Calendar.HOUR_OF_DAY:
    case Calendar.MINUTE:
    case Calendar.SECOND:
      ELEval.getVariablesInScope().getContextVariable(TIME_INCREMENT_VALUE);
      break;
    default:
      throw new IllegalArgumentException(Utils.format("Invalid Calendar unit ordinal '{}'", unit));
  }
  return 0;
}
 
源代码6 项目: astor   文件: DateTickUnit.java
/**
 * 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;
    }

}
 
源代码7 项目: astor   文件: DateUtilsRoundingTest.java
/**
 * Tests DateUtils.round()-method with Calendar.MINUTE
 * Includes rounding the extremes of one minute 
 * Includes rounding to January 1
 * 
 * @throws Exception
 * @since 3.0
 */
public void testRoundMinute() throws Exception {
    final int calendarField = Calendar.MINUTE;
    Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
    Date minDate, maxDate;

    roundedUpDate = dateTimeParser.parse("June 1, 2008 8:16:00.000");
    roundedDownDate = targetMinuteDate;
    lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:15:29.999");
    baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate,  calendarField);
    
    //round to January 1
    minDate = dateTimeParser.parse("December 31, 2007 23:59:30.000");
    maxDate = dateTimeParser.parse("January 1, 2008 0:00:29.999");
    roundToJanuaryFirst(minDate, maxDate, calendarField);
}
 
源代码8 项目: eagle   文件: DateTimeUtil.java
public static String getCalendarFieldName(int field) {
    switch (field) {
        case Calendar.DAY_OF_MONTH:
            return "DAY_OF_MONTH";
        case Calendar.DAY_OF_WEEK:
            return "DAY_OF_WEEK";
        case Calendar.DAY_OF_YEAR:
            return "DAY_OF_YEAR";
        case Calendar.HOUR:
            return "HOUR";
        case Calendar.MINUTE:
            return "MINUTE";
        case Calendar.SECOND:
            return "SECOND";
        default:
            throw new IllegalArgumentException("Unknown field code: " + field);
    }
}
 
源代码9 项目: astor   文件: DateUtilsRoundingTest.java
/**
 * Test DateUtils.truncate()-method with Calendar.MINUTE
 * 
 * @throws Exception
 * @since 3.0
 */
@Test
public void testTruncateMinute() throws Exception {
    final int calendarField = Calendar.MINUTE;
    Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:59.999");
    baseTruncateTest(targetMinuteDate, lastTruncateDate, calendarField);
}
 
@Override
public void generateKLine(int range, int field, long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long endTick = calendar.getTimeInMillis();
    String endTime = df.format(calendar.getTime());
    //往前推range个时间单位
    calendar.add(field, -range);
    String fromTime = df.format(calendar.getTime());
    long startTick = calendar.getTimeInMillis();
    System.out.println("time range from " + fromTime + " to " + endTime);
    List<ExchangeTrade> exchangeTrades = service.findTradeByTimeRange(this.symbol, startTick, endTick);

    KLine kLine = new KLine();
    kLine.setTime(endTick);
    String rangeUnit = "";
    if (field == Calendar.MINUTE) {
        rangeUnit = "min";
    } else if (field == Calendar.HOUR_OF_DAY) {
        rangeUnit = "hour";
    } else if (field == Calendar.DAY_OF_WEEK) {
        rangeUnit = "week";
    } else if (field == Calendar.DAY_OF_YEAR) {
        rangeUnit = "day";
    } else if (field == Calendar.MONTH) {
        rangeUnit = "month";
    }
    kLine.setPeriod(range + rangeUnit);

    // 处理K线信息
    for (ExchangeTrade exchangeTrade : exchangeTrades) {
        processTrade(kLine, exchangeTrade);
    }
    service.saveKLine(symbol, kLine);
}
 
源代码11 项目: Knowage-Server   文件: DateRangeUtils.java
private static boolean isBeginOfDay(Calendar c) {
	int[] fields = new int[] { Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND };
	for (int f : fields) {
		if (c.get(f) != 0) {
			return false;
		}
	}
	return true;
}
 
源代码12 项目: astor   文件: DateUtilsRoundingTest.java
/**
 * Test DateUtils.truncate()-method with Calendar.MINUTE
 * 
 * @throws Exception
 * @since 3.0
 */
@Test
public void testTruncateMinute() throws Exception {
    final int calendarField = Calendar.MINUTE;
    Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:59.999");
    baseTruncateTest(targetMinuteDate, lastTruncateDate, calendarField);
}
 
源代码13 项目: jaamsim   文件: SimCalendar.java
/**
 * Sets this Calendar's current time from the given long value.
 * @param millis - time in milliseconds from the epoch
 */
@Override
public void setTimeInMillis(long millis) {

	// Gregorian calendar
	if (gregorian) {
		super.setTimeInMillis(millis);
		return;
	}

	// Simple calendar with 365 days per year
	long seconds = millis / millisPerSec;
	long minutes = millis / millisPerMin;
	long hours = millis / millisPerHr;
	long days = millis / millisPerDay;
	long years = millis / millisPerYr;

	int dayOfYear = (int) (days % 365L) + 1;  // dayOfYear = 1 - 365;
	int month = getMonthForDay(dayOfYear);    // month = 0 - 11
	int dayOfMonth = dayOfYear - firstDayOfMonth[month] + 1;

	super.set(Calendar.YEAR, (int) years + epoch);
	super.set(Calendar.MONTH, month);
	super.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	super.set(Calendar.HOUR_OF_DAY, (int) (hours % 24L));
	super.set(Calendar.MINUTE, (int) (minutes % 60L));
	super.set(Calendar.SECOND, (int) (seconds % 60L));
	super.set(Calendar.MILLISECOND, (int) (millis % 1000L));
}
 
源代码14 项目: hortonmachine   文件: Times.java
/** This used to be 'deltim' in MMS.
 */
public static double deltaHours(int calUnit, int increments) {
    if (calUnit == Calendar.DATE) {
        return 24 * increments;
    } else if (calUnit == Calendar.HOUR) {
        return increments;
    } else if (calUnit == Calendar.MINUTE) {
        return increments / 60;
    } else if (calUnit == Calendar.SECOND) {
        return increments / 3600;
    }
    return -1;
}
 
源代码15 项目: astor   文件: DateUtils.java
/**
 * 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(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;
}
 
源代码16 项目: dubbo-spring-boot-learning   文件: DateUtils.java
/**
 * 获取日期绝对差
 *
 * @param d1    日期1
 * @param d2    日期2
 * @param field Calendar字段,YEAR、MONTH等
 * @return 日期差
 */
public static long diff(Date d1, Date d2, int field) {
    long ms = d2.getTime() - d1.getTime();
    //如果d2在d1前面
    if (getCalendar(d2).before(getCalendar(d1))) {
        //记录后一个日期
        Date d0 = d2;
        d2 = d1;
        d1 = d0;
    }
    long res = 0;
    switch (field) {
        case Calendar.YEAR:
            Calendar c = getCalendar(d2);
            //将年设置成相同的
            c.set(Calendar.YEAR, year(d1));
            //然后比较日期前后关系,如果d0在d1前面,年份-1
            res = year(d2) - year(d1) - (c.before(getCalendar(d1)) ? 1 : 0);
            break;

        case Calendar.MONTH:
            int years = year(d2) - year(d1);
            Calendar c1 = getCalendar(d2);
            //将年设置成相同的
            c1.set(Calendar.YEAR, year(d1));
            //然后比较日期前后关系,如果d0在d1前面,月份-1
            c1.set(Calendar.MONTH, month(d1) - 1);
            res = (month(d2) >= month(d1) ? (month(d2) - month(d1)) : (month(d2) + 12 - month(d1)) % 12) + years * 12 - (c1.before(getCalendar(d1)) ? 1 : 0);
            break;

        case Calendar.WEEK_OF_YEAR:
            res = ms / (7 * 24 * 60 * 60 * 1000);
            break;

        case Calendar.DATE:
            res = ms / (24 * 60 * 60 * 1000);
            break;

        case Calendar.HOUR:
            res = ms / (60 * 60 * 1000);
            break;

        case Calendar.MINUTE:
            res = ms / (60 * 1000);
            break;

        case Calendar.SECOND:
            res = ms / (1000);
            break;

        case Calendar.MILLISECOND:
            res = ms;
            break;

        default:
            break;
    }
    return res;
}
 
源代码17 项目: sophia_scaffolding   文件: DateTimeUtil.java
/**
 * 把给定的时间加上指定的时间值,可以为负
 *
 * @param d
 *            需要设定的日期对象
 * @param times
 *            时间值
 * @param type
 *            类型,Calendar.MILLISECOND,毫秒<BR>
 *            Calendar.SECOND,秒<BR>
 *            Calendar.MINUTE,分钟<BR>
 *            Calendar.HOUR,小时<BR>
 *            Calendar.DATE,日<BR>
 * @return 如果d为null,返回null
 */
public static Date addTime(Date d, double times, int type) {
    if (d == null) {
        throw new IllegalArgumentException("参数d不能是null对象!");
    }
    long qv = 1;
    switch (type) {
        case Calendar.MILLISECOND:
            qv = 1;
            break;
        case Calendar.SECOND:
            qv = 1000;
            break;
        case Calendar.MINUTE:
            qv = 1000 * 60;
            break;
        case Calendar.HOUR:
            qv = 1000 * 60 * 60;
            break;
        case Calendar.DATE:
            qv = 1000 * 60 * 60 * 24;
            break;
        default:
            throw new RuntimeException("时间类型不正确!type=" + type);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    long milliseconds = (long) Math.round(Math.abs(times) * qv);
    if (times > 0) {
        for (; milliseconds > 0; milliseconds -= 2147483647) {
            if (milliseconds > 2147483647) {
                calendar.add(Calendar.MILLISECOND, 2147483647);
            } else {
                calendar.add(Calendar.MILLISECOND, (int) milliseconds);
            }
        }
    } else {
        for (; milliseconds > 0; milliseconds -= 2147483647) {
            if (milliseconds > 2147483647) {
                calendar.add(Calendar.MILLISECOND, -2147483647);
            } else {
                calendar.add(Calendar.MILLISECOND, -(int) milliseconds);
            }
        }
    }
    return calendar.getTime();
}
 
源代码18 项目: nebula   文件: RoundScaleTickLabels.java
/**
     * Gets the grid step.
     * 
     * @param lengthInPixels
     *            scale length in pixels
     * @param min
     *            minimum value
     * @param max
     *            maximum value
     * @return rounded value.
     */
    private double getGridStep(int lengthInPixels, final double minR, final double maxR) {
    	if((int) scale.getMajorGridStep() != 0) {
    		return scale.getMajorGridStep();
    	}
    	double min = minR, max = maxR;
        if (lengthInPixels <= 0) {
            lengthInPixels = 1;
        }
        boolean minBigger = false;
        if (min >= max) {        	
        	if(max == min)
        		max ++;
        	else{
        		minBigger = true;
        		double swap = min;
        		min = max;
        		max= swap;
        	}
//        		throw new IllegalArgumentException("min must be less than max.");
        }

        double length = Math.abs(max - min);
        double markStepHint = scale.getMajorTickMarkStepHint();
        if(markStepHint > lengthInPixels)
        	markStepHint = lengthInPixels;
        double gridStepHint = length / lengthInPixels
                * markStepHint;       	
        
        if(scale.isDateEnabled()) {
        	//by default, make the least step to be minutes
        	long timeStep = 60000l;       		
        	if (scale.getTimeUnit() == Calendar.SECOND) {
        		timeStep = 1000l;
        	} else if (scale.getTimeUnit() == Calendar.MINUTE) {
        		timeStep = 60000l;
        	}else if (scale.getTimeUnit() == Calendar.HOUR_OF_DAY) {
        		timeStep = 3600000l;
        	}else if (scale.getTimeUnit() == Calendar.DATE) {
        		timeStep = 86400000l;
        	}else if (scale.getTimeUnit() == Calendar.MONTH) {
        		timeStep = 30l*86400000l;
        	}else if (scale.getTimeUnit() == Calendar.YEAR) {
        		timeStep = 365l*86400000l;  
        	}
        	double temp = gridStepHint + (timeStep - gridStepHint%timeStep);       	
        	return temp;
        }
        
        double mantissa = gridStepHint;
        int exp = 0;
        if (mantissa < 1) {
        	if(mantissa != 0){
	            while (mantissa < 1) {
	                mantissa *= 10.0;
	                exp--;
	            }
            }
        } else {
            while (mantissa >= 10) {
                mantissa /= 10.0;
                exp++;
            }
        }

        double gridStep;
		if (mantissa > 7.5) {
			// 10*10^exp
			gridStep = 10 * Math.pow(10, exp);
		} else if (mantissa > 3.5) {
			// 5*10^exp
			gridStep = 5 * Math.pow(10, exp);
		} else if (mantissa > 1.5) {
			// 2.0*10^exp
			gridStep = 2 * Math.pow(10, exp);
		} else {
			gridStep = Math.pow(10, exp); // 1*10^exponent
		}
		if (minBigger)
			gridStep = -gridStep;
		return gridStep;
    }
 
源代码19 项目: nebula   文件: CDateTime.java
/**
 * Set the style of this CDateTime to work with dates and / or times as
 * determined by the given pattern. This will set the fields shown in the
 * text box and, if <code>DROP_DOWN</code> style is set, the fields of the
 * drop down component.<br>
 * This method is backed by an implementation of SimpleDateFormat, and as
 * such, any string pattern which is valid for SimpleDateFormat may be used.
 * Examples (US Locale):<br>
 * </code>setPattern("MM/dd/yyyy h:mm a");</code><br />
 * </code>setPattern("'Meeting @' h:mm a 'on' EEEE, MMM dd,
 * yyyy");</code><br />
 * 
 * @param pattern
 *            the pattern to use, if it is invalid, the original is restored
 * @throws IllegalArgumentException
 * @see SimpleDateFormat
 * @see #getPattern()
 * @see #setFormat(int)
 */
public void setPattern(String pattern) throws IllegalArgumentException {
	this.allowedTimezones = null;
	if (isOpen()) {
		setOpen(false);
	}
	df = new SimpleDateFormat(pattern, locale);
	df.setTimeZone(timezone);
	if (updateFields()) {
		this.pattern = pattern;
		this.format = -1;
		boolean wasDate = isDate;
		boolean wasTime = isTime;
		isDate = isTime = false;
		calendarFields = new int[field.length];
		for (int i = 0; i < calendarFields.length; i++) {
			calendarFields[i] = getCalendarField(field[i]);
			switch (calendarFields[i]) {
			case Calendar.AM_PM:
			case Calendar.HOUR:
			case Calendar.HOUR_OF_DAY:
			case Calendar.MILLISECOND:
			case Calendar.MINUTE:
			case Calendar.SECOND:
			case Calendar.ZONE_OFFSET:
				isTime = true;
				break;
			case Calendar.DAY_OF_MONTH:
			case Calendar.DAY_OF_WEEK:
			case Calendar.DAY_OF_WEEK_IN_MONTH:
			case Calendar.DAY_OF_YEAR:
			case Calendar.ERA:
			case Calendar.MONTH:
			case Calendar.WEEK_OF_MONTH:
			case Calendar.WEEK_OF_YEAR:
			case Calendar.YEAR:
				isDate = true;
				break;
			default:
				break;
			}
		}
		if (checkButton() && (isDate != wasDate || isTime != wasTime)) {
			if (defaultButtonImage) {
				if (isDate && isTime) {
					doSetButtonImage(Resources.getIconCalendarClock());
				} else if (isDate) {
					doSetButtonImage(Resources.getIconCalendar());
				} else {
					doSetButtonImage(Resources.getIconClock());
				}
			}
			updateNullText();
		}
		if (checkText()) {
			updateText();
		}
		if (isSimple()) {
			disposePicker();
			createPicker();
		}
	} else {
		throw new IllegalArgumentException(
				"Problem setting pattern: \"" + pattern + "\""); //$NON-NLS-1$ //$NON-NLS-2$
	}
}
 
源代码20 项目: astor   文件: DateUtils.java
/**
 * 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(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;
}