Java 返回今天星期几

下面的代码是一位 8 年工作经验的大哥写的

    private String handleDayWeeks() {
        String name = "";
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        if (dayForWeek == 1) {
            name = "Monday";
        } else if (dayForWeek == 2) {
            name = "Tuesday";
        } else if (dayForWeek == 3) {
            name = "Wednesday";
        } else if (dayForWeek == 4) {
            name = "Thursday";
        } else if (dayForWeek == 5) {
            name = "Friday";
        } else if (dayForWeek == 6) {
            name = "Saturday";
        } else if (dayForWeek == 7) {
            name = "Sunday";
        }
        return name;
    }

不得不说 JDK8 都已经发布 7 年了,实现同样的功能一行就搞定

public class TestLocalDate {

    public static void main(String[] args) {
        String today = LocalDate.now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
        System.out.println(today);
    }
}
java
177 views
Comments
登录后评论
Sign In
·

哈哈哈笑死了,高级黑🌚

·

如果需求要显示周几呢,不是星期几呢