从给定的字符串输入返回 DayOfTheWeek(作为大写字符串) [英] Return DayOfTheWeek (as an uppercase string) from a given String input
问题描述
这是我第一次查看 Date Api,我不明白我哪里出错了.该问题已被注释掉,因此您可以准确了解预期内容.
This is my first time looking into the Date Api's i don't understand where i'm going wrong. The Question has been commented out so you can see exactly whats expected.
有人可以简单解释一下如何解决/解决这个问题吗?
Could someone please simply explain how to solve/approach this problem?
当我进入 DateUtil 类>DayofTheWeek 方法时,我尝试使用 theDate 字段返回 LocalDate.DayofTheWeek 方法,该字段现在已初始化.但它不会工作.一直提示无法解析方法"?
When i get to the DateUtil class>DayofTheWeek method i attempt to return the LocalDate.DayofTheWeek method using the theDate field which by now has been initialised. but it wont work. It keeps saying 'cannot resolve method'?
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
}
}
推荐答案
你让这件事变得太复杂了.
You are making this much too complicated.
一个问题是你在文本中思考,使用哑字符串而不是智能对象.不要传递字符串 "2000-01-01"
,传递一个 LocalDate
对象.不要传递字符串 SATURDAY
,传递 DayOfWeek.SATURDAY
对象.
One problem is that you are thinking in text, using dumb strings rather than smart objects. Do not be passing around the string "2000-01-01"
, pass around a LocalDate
object. Do not pass around the string SATURDAY
, pass around the DayOfWeek.SATURDAY
object.
LocalDate
.parse(
"2000-01-01"
)
.getDayOfWeek()
.equals(
DayOfWeek.SATURDAY
)
如果你违背我的建议坚持使用字符串,你可以得到DayOfWeek
通过调用 toString
将对象作为文本枚举.
If you insist on using strings against my advice, you can get the name of the DayOfWeek
enum object as text by calling toString
.
String output = DayOfWeek.SATURDAY.toString() ;
往另一个方向走,调用 DayOfWeek.valueOf
.
Going the other direction, calling DayOfWeek.valueOf
.
DayOfWeek dow = DayOfWeek.valueOf( "SATURDAY" ) ;
这篇关于从给定的字符串输入返回 DayOfTheWeek(作为大写字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!