Java 如何查找计划日期的星期几

Java 如何查找计划日期的星期几,java,android,calendar,Java,Android,Calendar,我不知道怎样才能找到一周中我想要的一天 例如: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); String s="2019-10-07"; try { calendar.setTime(simpleDateFormat.parse(s)); if(C

我不知道怎样才能找到一周中我想要的一天

例如:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String s="2019-10-07";
        try {
            calendar.setTime(simpleDateFormat.parse(s));
            if(Calendar.DAY_OF_WEEK == 2){
                Toast.makeText(this, "Mo", Toast.LENGTH_SHORT).show();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

但它并不正确!刚刚考虑的日历的当前日期,而不是我指定的日期。

是一个常量
7
,因此它工作正常,只是不是您期望的那样。您不应在新代码中使用
日历
(而应使用)。但是会给你你想要的价值。

作为一种旁白,考虑扔掉长期过时且臭名昭著的
SimpleDateFormat
和像
Calendar
这样的朋友,并添加到你的Android项目中,以便使用
java.time
,现代java日期和时间API。使用java.time更简单、更清晰:只需要
LocalDate=LocalDate.parse
然后
if(date.getDayOfWeek().equals(DayOfWeek.MONDAY))
(if条件的计算结果为
true
)。