Home >Java >JavaBase >How to get the day of the week of the current date in java

How to get the day of the week of the current date in java

尚
Original
2019-12-06 13:40:393553browse

How to get the day of the week of the current date in java

java Gets the day of the week at the current time:

 /**
     * 获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

The Calendar.getInstance() method can get the current date and time.

Calendar.DAY_OF_WEEK actually means: the day of the week, so it will be affected by the day of the week the first day is.
Some areas use Sunday as the first day of the week

2. For the parameters passed in when creating SimpleDateFormat: EEEE represents the week, such as "Friday"; MMMM represents the Chinese month, such as "November"; MM represents the month, such as "10";
yyyy represents the year, such as "2017"; dd represents the day, such as "28"

Note Note Note: Case sensitive! ! ! !

  Date date=new Date();
   SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
   dateFm.format(date);

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

The above is the detailed content of How to get the day of the week of the current date in java. 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