【问题标题】:Return DayOfTheWeek (as an uppercase string) from a given String input从给定的字符串输入返回 DayOfTheWeek(作为大写字符串)
【发布时间】: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


【解决方案1】:

你把这弄得太复杂了。

一个问题是你在文本中思考,使用哑字符串而不是智能对象。不要传递字符串"2000-01-01",传递LocalDate 对象。不要传递字符串SATURDAY,传递DayOfWeek.SATURDAY 对象。

LocalDate
.parse(
    "2000-01-01"
)
.getDayOfWeek()
.equals(
    DayOfWeek.SATURDAY 
)

如果你坚持不按我的建议使用字符串,你可以通过调用 toString 以文本形式获取 DayOfWeek 枚举对象的名称。

String output = DayOfWeek.SATURDAY.toString() ;

换个方向,打电话给DayOfWeek.valueOf

DayOfWeek dow = DayOfWeek.valueOf( "SATURDAY" ) ;

【讨论】:

    【解决方案2】:

    已编辑

    我对你的代码做了一个小改动(它有效)

    DateUtil

    class DateUtil {
    
    private LocalDate date;
    
    public DateUtil(LocalDate date) {
        this.date = date;
    }
    
    public String dayOfWeek() {
        return String.valueOf(date.getDayOfWeek());
    
        }
    }
    

    挑战三

    public class ChallengeThree {
    
    public static void main(String[] args) {
    
        String theDayOfWeek = dayOfWeek("2000-01-01");
        System.out.println(theDayOfWeek);
    
    }
    
    public static String dayOfWeek(String date) {
    
        LocalDate localDate = LocalDate.parse(date);
        DateUtil dateUtil = new DateUtil(localDate);
        return dateUtil.dayOfWeek();
    
      }
    
    }
    

    【讨论】:

    • 这不是解决问题的好方法。我们应该更愿意将日期保留为LocalDate 内的DateUtil,如问题中所示。此外,代码中的注释可能意味着我们不允许更改它。
    • @OleV.V.你是对的,谢谢你的反馈,我编辑了它。
    【解决方案3】:

    其他答案都很好。我想更接近您的确切要求:

    它一直说'无法解析方法'?

    如你所见,错误信息出现在这一行:

         return LocalDate.of(theDate).getDayOfWeek();
    

    它无法解析的方法是of()LocalDate 有几个重载的 of 方法。 theDate 已经是 LocalDate,正如 MadProgrammer 在评论中所说,没有接受 LocalDateof 方法。这就是错误消息的原因。顺便说一句,我在 Eclipse 中收到的消息是“LocalDate 类型中的 (int, Month, int) 方法不适用于参数 (LocalDate)”。

    由于theDate 已经是LocalDate,因此您根本不需要该方法调用。把它放在一边,然后直接在theDate 上调用getDayOfWeek()(我故意让自己把它写成代码;你应该从这样做中学习,所以你应该是这样做的人)。

    您似乎遇到了另一个问题,LocalDate.getDayOfWeek() 返回一个DayOfWeek,而您的DateUtil.dayOfWeek() 应该返回一个String。当你解决它时,你可能会自己解决它。如果没有,请随时在 cmets 中跟进。

    顺便说一句,对于生产代码,我会考虑为此目的使用 DateUtil 类矫枉过正。我知道这是一项任务,所以我已经按照给定的任务忠实地回答了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      相关资源
      最近更新 更多