从日期中提取月份和年份
您正在为一家电子产品商店按月跟踪库存。请通过应用日期标准化,从每个日期中提取月份和年份!
所需的包(如 LocalDate 和 DateTimeFormatter)已为您导入。
本练习是课程的一部分
Java 数据清洗
练习说明
- 使用一位数的月/日格式指定
date1的格式。 - 使用补零的月/日格式指定
date2的格式。 - 从
date1和date2中提取月份和年份。
交互式实操练习
通过完成这段示例代码来试试这个练习。
public class DateStandardizationExample {
public static void main(String[] args) {
// Specify the format of date1 with single-digit month/day
LocalDate date1 = LocalDate.parse("1/1/23", DateTimeFormatter.ofPattern("____/____/____"));
// Specify the format of date2 with zero-padded month/day
LocalDate date2 = LocalDate.parse("07-01-2023", DateTimeFormatter.ofPattern("____-____-____"));
// Extract the month and year from date1 and date2
System.out.println("Month and year of " + date1 + ": " + date1.____() + " " + date1.____());
System.out.println("Month and year of " + date2 + ": " + date2.____() + " " + date2.____());
}
}