【发布时间】:2020-01-19 04:50:17
【问题描述】:
这是我第一次研究 Date Api,我不明白我哪里出错了。该问题已被注释掉,因此您可以确切地看到预期的内容。
有人可以简单解释一下如何解决/解决这个问题吗?
当我到达 DateUtil 类>DayofTheWeek 方法时,我尝试使用现在已经初始化的 theDate 字段返回 LocalDate.DayofTheWeek 方法。但它不会工作。它一直说'无法解析方法'?
public class ChallengeThree {
public static String dayOfWeek(String date) {
/**
*** Returns a String storing the day of the week in all capital letters of the
* given date String
* Complete the implementation of the DateUtil class and use it in this function
* Arguments
* date - a String storing a local date, such as "2000-01-01"
* Examples
* dayOfWeek("2000-01-01") returns "SATURDAY"
*/**
// ====================================
// Do not change the code before this
// CODE1: Write code to return the day of the week of the String date
// using the DateUtil class at the bottom of this file
DateUtil newdates= new DateUtil("2000-01-01");
System.out.println(newdates.dayOfWeek());
// ====================================
// Do not change the code after this
}
// public static void main(String[] args) {
// String theDayOfWeek = dayOfWeek("2000-01-01");
String expected = "SATURDAY";
// Expected output is
// true
// System.out.println(theDayOfWeek == expected);
// }
}
class DateUtil {
LocalDate theDate;
public DateUtil(String date) {
/**
* Initialize the theDate field using the String date argument
* Arguments
* date - a String storing a local date, such as "2000-01-01"
*/
// ====================================
// Do not change the code before this
LocalDate theNewDate = LocalDate.parse(date);
this.theDate=theNewDate;
// ====================================
// Do not change the code after this
}
public String dayOfWeek() {
/**
* Return a String the day of the week represented by theDate
*/
// ====================================
// Do not chDate theDate = new ange the code before this
return LocalDate.of(theDate).getDayOfWeek();
// ====================================
// Do not change the code after this
}
}
【问题讨论】:
-
你检查
LocalDate.getDayOfWeek()返回的内容了吗? -
所以,我看了一下JavaDocs for
LocalDate并没有这样的方法LocalDate.of(LocalDate)...我不知道为什么会有,因为它没有意义。为什么不直接使用theDate.getDayOfWeek()? -
您也可以只使用
DateTimeFormatter将“星期几”值格式化为String,并根据需要将其大写 -
可能离题,但
theDayOfWeek == expected不正确。 How do I compare strings in Java?
标签: java api datetime java-8 localdate