从给定的String输入返回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
}
}
推荐答案
您这样做太复杂了.
一个问题是您正在使用哑字符串而不是智能对象来思考文本.不要绕过字符串"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
)
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" ) ;
这篇关于从给定的String输入返回DayOfTheWeek(作为大写字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!