Code example for Calendar

Methods: clone, get

0
	@Override 
	public boolean weekdayRange(String wd1, String wd2, String gmt) {
		boolean useGmt = GMT.equalsIgnoreCase(wd2) || GMT.equalsIgnoreCase(gmt);
		Calendar cal = getCurrentTime(useGmt);
 
		int currentDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
		int from = DAYS.indexOf(wd1 == null ? null : wd1.toUpperCase());
		int to = DAYS.indexOf(wd2 == null ? null : wd2.toUpperCase());
		if (to == -1) {
			to = from;
		} 
 
		if (to < from) {
			return currentDay >= from || currentDay <= to;
		} else { 
			return currentDay >= from && currentDay <= to;
		} 
	} 
 
	/************************************************************************* 
	 * Sets a calendar with the current time. If this is set all date and time 
	 * based methods will use this calendar to determine the current time 
	 * instead of the real time. This is only be used by unit tests and is not 
	 * part of the public API. 
	 *  
	 * @param cal 
	 *            a Calendar to set. 
	 ************************************************************************/ 
 
	public void setCurrentTime(Calendar cal) {
		this.currentTime = cal;
	} 
 
	/************************************************************************* 
	 * Gets a calendar set to the current time. This is used by the date and 
	 * time based methods. 
	 *  
	 * @param useGmt 
	 *            flag to indicate if the calendar is to be created in GMT time 
	 *            or local time. 
	 * @return a Calendar set to the current time. 
	 ************************************************************************/ 
 
	private Calendar getCurrentTime(boolean useGmt) {
		if (this.currentTime != null) { // Only used for unit tests
			return (Calendar) this.currentTime.clone();
		} 
		return Calendar.getInstance(useGmt ? TimeZone.getTimeZone(GMT)
				: TimeZone.getDefault());
	}