介绍
在这个实验(Lab)中,你将学习如何在 Java 中使用 LocalDate 类的 get() 方法,从日期中检索特定的字段。当你只需要从给定的日期中检索特定字段时,例如月份中的某一天或星期几,这个方法非常有用。你可以使用 ChronoField 枚举来指定要检索的字段。
创建 Java 文件
在这一步中,你将创建一个名为 LocalDateGet.java 的新 Java 文件,放在你的项目目录中。这个文件将包含演示 LocalDate 类 get() 方法的代码。
在你的 WebIDE 中打开终端。默认目录是
~/project。使用
touch命令创建LocalDateGet.java文件:touch LocalDateGet.java这个命令会在你当前的目录中创建一个空文件。你可以通过列出文件来验证它的创建:
ls你应该在输出中看到
LocalDateGet.java。
添加必要的导入和类结构
现在,你将为你的 LocalDateGet.java 文件添加基本结构,包括所需的导入语句和主类定义。这些导入对于在 Java 中使用日期和时间 API 至关重要。
在 WebIDE 的代码编辑器中打开
LocalDateGet.java文件。将以下代码添加到文件中:
import java.time.LocalDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; public class LocalDateGet { public static void main(String[] args) { // This is where we will call our methods later } }java.time.LocalDate:这个类表示没有时区的日期。java.time.temporal.ChronoField:这个枚举提供了日期和时间的标准字段集合,例如DAY_OF_MONTH、DAY_OF_WEEK、DAY_OF_YEAR等。java.time.temporal.TemporalAccessor:这个接口提供了对时间对象(例如LocalDate)的只读访问。get()方法在这个接口中定义。java.time.temporal.TemporalField:这个接口表示日期 - 时间的一个字段,例如年、月或日。ChronoField实现了这个接口。
添加代码后,保存
LocalDateGet.java文件。
获取当月日期
在这一步中,你将向你的 LocalDateGet 类添加一个方法,该方法演示如何使用 get() 方法和 ChronoField.DAY_OF_MONTH 从 LocalDate 对象中检索月份中的日期。
在 WebIDE 的代码编辑器中打开
LocalDateGet.java文件。在
LocalDateGet类中添加getDayOfMonth()方法,但要放在main方法之外:import java.time.LocalDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; public class LocalDateGet { public static void getDayOfMonth() { // Define the field to retrieve: DAY_OF_MONTH TemporalField field = ChronoField.DAY_OF_MONTH; // Create a LocalDate object for May 17, 2021 TemporalAccessor date = LocalDate.of(2021, 5, 17); // Display the original date System.out.println("Date : " + date); // Retrieve the day of the month using the get() method int val = date.get(field); // Display the retrieved day of the month System.out.println("Day of the month: " + val); System.out.println(); // Add a blank line for better readability } public static void main(String[] args) { // This is where we will call our methods later } }LocalDate.of(2021, 5, 17):创建一个LocalDate对象,表示 2021 年 5 月 17 日。ChronoField.DAY_OF_MONTH:指定我们要检索月份中的日期。date.get(field):这是操作的核心。它在date对象上调用get()方法,传递field(即ChronoField.DAY_OF_MONTH)以提取相应的整数值。
保存
LocalDateGet.java文件。现在,让我们从
main中调用此方法,看看它的实际效果。修改你的main方法,如下所示:public static void main(String[] args) { getDayOfMonth(); // Other methods will be called here later }再次保存文件。
打开终端并编译你的 Java 代码:
javac LocalDateGet.java如果没有错误,编译将静默完成。
运行编译后的 Java 程序:
java LocalDateGet你应该看到类似于以下的输出:
Date : 2021-05-17 Day of the month: 17这确认了
getDayOfMonth()方法正确地提取了月份中的日期。
获取星期几
接下来,你将添加另一个方法来检索星期几。ChronoField.DAY_OF_WEEK 字段表示星期几,其中 1 代表星期一,2 代表星期二,以此类推,7 代表星期日。
在 WebIDE 的代码编辑器中打开
LocalDateGet.java文件。在
LocalDateGet类中添加getDayOfWeek()方法,放在getDayOfMonth()方法的下面:import java.time.LocalDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; public class LocalDateGet { // ... (getDayOfMonth method as defined previously) ... public static void getDayOfWeek() { // Define the field to retrieve: DAY_OF_WEEK TemporalField field = ChronoField.DAY_OF_WEEK; // Create a LocalDate object for May 17, 2021 (which was a Monday) TemporalAccessor date = LocalDate.of(2021, 5, 17); // Display the original date System.out.println("Date : " + date); // Retrieve the day of the week using the get() method int val = date.get(field); // Display the retrieved day of the week System.out.println("Day of the week: " + val); System.out.println(); // Add a blank line for better readability } public static void main(String[] args) { // ... (main method as defined previously) ... } }保存
LocalDateGet.java文件。现在,从你的
main方法中调用这个新方法。更新main方法,如下所示:public static void main(String[] args) { getDayOfMonth(); getDayOfWeek(); // Call the new method // Other methods will be called here later }再次保存文件。
从终端编译你的 Java 代码:
javac LocalDateGet.java运行编译后的 Java 程序:
java LocalDateGet你应该看到类似于以下的输出:
Date : 2021-05-17 Day of the month: 17 Date : 2021-05-17 Day of the week: 1由于 2021 年 5 月 17 日是星期一,并且
ChronoField.DAY_OF_WEEK将星期一表示为 1,因此输出Day of the week: 1是正确的。
获取一年中的第几天
最后,你将添加一个方法来检索一年中的第几天。ChronoField.DAY_OF_YEAR 字段表示一年中的第几天,其中 1 代表 1 月 1 日,以此类推。
在 WebIDE 的代码编辑器中打开
LocalDateGet.java文件。在
LocalDateGet类中添加getDayOfYear()方法,放在getDayOfWeek()方法的下面:import java.time.LocalDate; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; public class LocalDateGet { // ... (getDayOfMonth and getDayOfWeek methods as defined previously) ... public static void getDayOfYear() { // Define the field to retrieve: DAY_OF_YEAR TemporalField field = ChronoField.DAY_OF_YEAR; // Create a LocalDate object for May 17, 2021 TemporalAccessor date = LocalDate.of(2021, 5, 17); // Display the original date System.out.println("Date : " + date); // Retrieve the day of the year using the get() method int val = date.get(field); // Display the retrieved day of the year System.out.println("Day of the year: " + val); System.out.println(); // Add a blank line for better readability } public static void main(String[] args) { // ... (main method as defined previously) ... } }保存
LocalDateGet.java文件。现在,从你的
main方法中调用这个最终方法。更新main方法,如下所示:public static void main(String[] args) { getDayOfMonth(); getDayOfWeek(); getDayOfYear(); // Call the new method }再次保存文件。
从终端编译你的 Java 代码:
javac LocalDateGet.java运行编译后的 Java 程序:
java LocalDateGet你应该看到完整的输出,包括一年中的第几天:
Date : 2021-05-17 Day of the month: 17 Date : 2021-05-17 Day of the week: 1 Date : 2021-05-17 Day of the year: 1372021 年 5 月 17 日是一年中的第 137 天,因此输出
Day of the year: 137是正确的。
总结
在这个实验中,你学习了如何使用 Java LocalDate 类的 get() 方法从日期中检索特定字段。你创建了一个程序,该程序检索了同一日期(2021 年 5 月 17 日)的月份中的日期、星期几和一年中的第几天。通过使用 get() 方法以及 ChronoField 枚举,你可以在处理时间数据时有效地提取特定的日期组件,从而使你的 Java 程序更加精确。



