How to get the day of the week of the current date in java 您所在的位置:网站首页 java获取当前星期 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

2024-07-15 10:43| 来源: 网络整理| 查看: 265

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

How to get the day of the week in java

The first method:

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

Second method: Use SimpleDateFormat to format the date

Date date = new Date(); SimpleDateFormat dateFm = new SimpleDateFormat("EEEE"); String currSun = dateFm.format(date); System.out.println(currSun);Copy after login

Note: The format string is case-sensitive For the parameters passed in when creating SimpleDateFormat: EEEE represents the day of the week, such as "Thursday"; MMMM represents Chinese Month, such as "July"; MM represents the month, such as "07"; yyyy represents the year, such as "2017"; dd represents the day, such as "05"

Recommended tutorial: java tutorial

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!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有