Monat und Jahr aus Datumswerten extrahieren
Du verfolgst den monatlichen Bestand für deinen Elektronikladen. Extrahiere den Monat und das Jahr aus jedem Datum, indem du die Datumsstandardisierung anwendest!
Die notwendigen Pakete wie LocalDate und DateTimeFormatter wurden bereits für dich importiert.
Diese Übung ist Teil des Kurses
<Kurs>Datenbereinigung in Java</Kurs>Übungsanweisungen
- Lege das Format von
date1mit einstelligem Monat/Tag fest. - Lege das Format von
date2mit führender Null für Monat/Tag fest. - Extrahiere den Monat und das Jahr aus
date1unddate2.
Interaktive praktische Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
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.____());
}
}