Sample Programs

Sample java program to check if the given date is a week day

 import java.util.Calendar;
 import java.util.Date;


 public class DateUtility {

    public static void main(String[] args) {

    	System.out.println("Is Current Date a Week day ? = " + isWeekDay(new Date()));
    }
	
    /**
     * Return whether the specified date is a week day, M-F.
     * @param date
     * @return true if the date is M-F, false if it is Saturday or Sunday.
     */
    public static boolean isWeekDay(Date date) {
    	int dayOfWeek = getDayOfWeek(date);
    	if (dayOfWeek == -1) {
    		// It's null, so it's not a weekday..
    		return false;
    	}
    	return dayOfWeek != Calendar.SUNDAY && dayOfWeek != Calendar.SATURDAY;
    }
    
    /**
     * Return the int representing Calendar.DAY_OF_WEEK for the given date
     * @param date
     * @return 
     */
    public static int getDayOfWeek(Date date) {
        if (date == null) return -1;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }
 }




blog comments powered by Disqus