Java 日期实用程序练习

Java Date Utils exercise

我目前正在参加培训课程,这给我带来了很多问题。 代码如下。 我遇到的问题是培训课程需要基于 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
    }
}

使用 LocalDate.parse to parse the date string into a LocalDate instance and then use LocalDate#getDayOfWeekLocalDate 实例中获取星期几。

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

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

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

请注意 == 比较的是参考文献,而不是内容。

[更新]

这就是您如何利用 DateUtil class 来做到这一点。

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();
    }
}

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

true

DayOfWeek

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

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

具体来说,Java 提供 DayOfWeek enum class. See Oracle tutorial on enums if you are new to that. You can ask a LocalDate for the enum object representing its day-of-week: 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 in a try-catch to trap for the 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

true

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

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

samedi