Home > Java > JavaBase > body text

How to determine the day of the week in java?

Release: 2019-11-21 15:45:33
Original
3409 people have browsed it

How to determine the day of the week in java?

java determines the day of the week the current date is:

  /** 
    * 判断当前日期是星期几<br> 
    * <br> 
    * @param pTime 修要判断的时间<br> 
    * @return dayForWeek 判断结果<br> 
    * @Exception 发生异常<br> 
    */  
public static int dayForWeek(String pTime) throws Exception {  
 format = new SimpleDateFormat("yyyy-MM-dd");  
 Calendar c = Calendar.getInstance();  
 c.setTime(format.parse(pTime));  
 int dayForWeek = 0;  
 if(c.get(Calendar.DAY_OF_WEEK) == 1){  
  dayForWeek = 7;  
 }else{  
  dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;  
 }  
 return dayForWeek;  
}
Copy after login

It should be noted that Sunday is the first day of the week internationally, and the DAY_OF_WEEK provided in Calendar The week obtained also starts with Sunday.

The Chinese custom is to start the week on Monday, so it needs to be set according to the actual needs of the project.

The Calendar class provides some methods for conversion between calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc., and provides some methods for operating calendar fields (such as getting the date of next week).

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

The above is the detailed content of How to determine the day of the week in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template