Java获取今天是周几

下面介绍如何在Java中获取今天是周几

public static Integer getTodayOfWeekAsInt() {

        Calendar c = Calendar.getInstance();

        int firstDayOfWeek = c.getFirstDayOfWeek();

        c.setTime(new Date());
        int dow = c.get(Calendar.DAY_OF_WEEK);
        if (firstDayOfWeek == Calendar.SUNDAY) {
            dow = dow - 1;
            if (dow == 0) {
                dow = 7;
            }
        }
        return dow;
    }

如果是JDK 8,你还有另外一种方法

 

@Test
    public void dayOfWeek2Test() {
        String cm = "[email protected]";
        LocalDate today = LocalDate.now();

        Integer dow = today.getDayOfWeek().getValue();
        log.info(cm + " dayOfWeek: {}", dow);
    }

 

具体参考JSR 310

JSR 310: Date and Time API
 
 

 

分类: 默认 标签: 发布于: 2022-06-21 10:32:48, 更新于: 2022-06-21 11:09:16