To get the day of the week for a specified date, you can use the following two methods to get the day of the week:
1. Use Calendar
Class
//根据日期取得星期几 public static String getWeek(Date date){ String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1; if(week_index<0){ week_index = 0; } return weeks[week_index]; }
Online learning video recommendation: java course
2. Use SimpleDateFormat
Class
//根据日期取得星期几 public static String getWeek(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); String week = sdf.format(date); return week; }
Note: The format string is case-sensitive
For the parameters passed in when creatingSimpleDateFormat
: EEEE represents the day of the week, such as "Thursday"; MMMM represents the Chinese month, such as "October" "January"; MM represents the month, such as "11"; yyyy represents the year, such as "2010"; dd represents the day, such as "25".
Recommended related articles and tutorials: java entry program
The above is the detailed content of How to get the day of the week for a specified date in java. For more information, please follow other related articles on the PHP Chinese website!