【问题标题】:Java Date Utils exerciseJava 日期实用程序练习
【发布时间】:2020-06-23 15:30:04
【问题描述】:

我目前正在参加培训课程,这给我带来了很多问题。 代码如下。 我遇到的问题是培训课程需要基于 java.time.LocalDate 导入的特定解决方案。

我已经尝试了多种不同的方法来解决这个问题,但是内置编译器一直在爆炸。 这是我第一次尝试学习 Java,但绝对没有帮助。 需要在指定的3点添加代码。

感觉好像没必要在这方面寻求帮助,但我在这里看不到任何解决方案。

import java.time.LocalDate;

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
        

        // ====================================
        // 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

        // CODE2: Write code to initialize the date field of the class
        

        // ====================================
        // Do not change the code after this
    }

    public String dayOfWeek() {
        /**
         * Return a String the day of the week represented by theDate
         */

        // ====================================
        // Do not change the code before this

        // CODE3: Write code to return the String day of the week of theDate
        

        // ====================================
        // Do not change the code after this
    }
}

【问题讨论】:

  • 请发布代码而不是屏幕截图:如果人们需要测试或调试您的代码,复制/粘贴它比从屏幕截图转录更容易。
  • 我无法正确格式化代码以进行发布
  • 您的问题是您尝试定义String[] date,但date 已定义为String(来自方法的参数)。只需使用不同的变量名称,例如dateParts(并明显重命名其他引用)
  • 如果要保留格式,请在代码前后使用```。请参阅help
  • 好吧,那么我认为你应该通过将 Arvind 的答案分成两部分来调整它,以便尊重你的模板:在 DateUtil 的构造函数中将 LocalDate.parse(date) 分配给 theDate,并且让getDayOfWeektheDate 上拨打getDayOfWeek

标签: java date localdate


【解决方案1】:

使用LocalDate.parse 将日期字符串解析为LocalDate 实例,然后使用LocalDate#getDayOfWeekLocalDate 实例中获取星期几。

public static String dayOfWeek(String date) {
    return LocalDate.parse(date).getDayOfWeek().toString();
}

另外,使用String#equals 比较字符串,即你应该写:

System.out.println(expected.equals(theDayOfWeek));

请注意,== 比较的是引用,而不是内容。

[更新]

这就是你可以利用你的DateUtil 类来做到这一点的方法。

import java.time.LocalDate;

public class ChallengeThree {
    public static String dayOfWeek(String date) {
        return new DateUtil(date).dayOfWeek();
    }

    public static void main(String[] args) {
        String theDayOfWeek = dayOfWeek("2000-01-01");
        String expected = "SATURDAY";
        System.out.println(expected.equals(theDayOfWeek));
    }
}

class DateUtil {
    LocalDate theDate;

    public DateUtil(String date) {
        theDate = LocalDate.parse(date);
    }

    public String dayOfWeek() {
        return theDate.getDayOfWeek().toString();
    }
}

【讨论】:

  • 在练习的上下文中不起作用。不是你的解决方案有问题,而是练习有问题。感谢您对 Arvind 的帮助。
  • @Tufty - 不客气。我想了解doesn't work in the context of the exercise 是什么,以便我可以进一步帮助您。
  • 他们的编译器在它允许的方面非常具体。代码 1 块需要专门引用 DateUtil 类,我无法让它工作。 // CODE1:编写代码以返回字符串日期的星期几 // 使用此文件底部的 DateUtil 类 // =================== =================
  • @Tufty - 我已经发布了更新。我希望它有所帮助。如有任何疑问,请随时发表评论。
  • 感谢您对此的帮助。看到您的解决方案让事情变得更加清晰。
【解决方案2】:

tl;博士

    LocalDate                          // Represent a date-only, without time-of-day and without time zone.
    .parse( "2000-01-01" )             // Automatically parse a string in standard ISO 8601 format.
    .getDayOfWeek()                    // Returns `DayOfWeek` enum object.
    .equals( DayOfWeek.SATURDAY )      // Comparing `DayOfWeek` objects, returning a `boolean`. 

查看此代码run live at IdeOne.com

是的

DayOfWeek

接受的Answer by Avinash 是正确的,直接针对您的具体练习。

但我想指出,Java 提供了特定的类来帮助您完成这项工作。

具体来说,Java 提供了DayOfWeek 枚举类。如果您是新手,请参阅 Oracle tutorial on enums。您可以向LocalDate 询问代表其星期几的枚举对象:LocalDate::getDayOfWeek

使用智能对象而不是哑字符串。所以这段代码:

String theDayOfWeek = dayOfWeek("2000-01-01");
String expected = "SATURDAY";
System.out.println(theDayOfWeek == expected);

……应该是:

LocalDate ld = LocalDate.parse( "2000-01-01" ) ;  // Standard ISO 8601 format is YYYY-MM-DD. 
DayOfWeek dow = ld.getDayOfWeek() ;
DayOfWeek expectedDow = DayOfWeek.SATURDAY ; 
boolean isExpected = dow.equals( expectedDow ) ;

在实际工作中,我会将对 LocalDate.parse 的调用嵌套在 try-catch 中,以便在输入字符串错误的情况下捕获 DateTimeParseException

或者将其设为单行(我不建议这样做)。

if( 
    LocalDate                          // Represent a date-only, without time-of-day and without time zone.
    .parse( "2000-01-01" )             // Automatically parse a string in standard ISO 8601 format.
    .getDayOfWeek()                    // Returns `DayOfWeek` enum object.
    .equals( DayOfWeek.SATURDAY )      // Comparing `DayOfWeek` objects, returning a `boolean`. 
) { … }

查看此代码run live at IdeOne.com

是的

顺便说一句,我们可以得到name of the day localized

String output = 
    DayOfWeek.SATURDAY
    .getDisplayName​(
        TextStyle.FULL , 
        Locale.CANADA_FRENCH
    )
;
System.out.println( output ) ;

萨米迪

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2017-08-15
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多