Home >Java >JavaBase >Java determines whether a date is a weekend

Java determines whether a date is a weekend

尚
Original
2019-12-27 15:39:003798browse

Java determines whether a date is a weekend

java determines whether a date is a weekend:

/** 
 * 判断是否是周末 
 * @return 
 */  
private boolean isWeekend(Calendar cal){  
    int week=cal.get(Calendar.DAY_OF_WEEK)-1;  
    if(week ==6 || week==0){//0代表周日,6代表周六  
        return true;  
    }  
    return false;  
}

The Calendar class is an abstract class that provides us with related functions for date calculation, such as: year, month, Display and calculation of day, hour, minute and second.

GregorianCalendar is a concrete subclass of Calendar that provides the standard calendar system used by most countries in the world.

Calendar.DAY_OF_WEEK——The day of the week, the value of 1--7, corresponding to: Sunday, Monday, Tuesday, Wednesday....Saturday

Getting the day of the week Calendar.DAY_OF_WEEK - The reason for 1

Calendar.DAY_OF_WEEK in Java actually means: the day of the week, so it will be affected by the day of the week when the first day is.

Some areas use Sunday as the first day of the week, while some areas use Monday as the first day of the week. These two situations need to be distinguished.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Java determines whether a date is a weekend. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn