Get startedGet started for free

Standardizing dates

You're tracking manufacturing dates in your electronics inventory, but the dates occur in different formats. Standardize the date formats, and format a date for display.

Product Name Manufacturing Date
Laptop 3/1/25
Smartphone 04-01-2025
Monitor 2025.06.01

The necessary packages such as LocalDate, and DateTimeFormatter have been imported for you.

This exercise is part of the course

Cleaning Data in Java

View Course

Exercise instructions

  • Specify the input date formats.
  • Parse each string in dates as a date.
  • Display manufacturingDate as full month name, day without leading zeros, and full year.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class DateStandardizationExample {
    public static void main(String[] args) {
    	String[] dates = { "3/1/25", "04-01-2025", "2025.06.01" };
        // Specify the input date formats
        DateTimeFormatter[] formatters = {
            ____.____("M/d/yy"),
            ____.____("MM-dd-yyyy"),
            ____.____("yyyy.MM.dd")
        };

        System.out.println("Standardized manufacturing dates:");
        for (int i = 0; i < dates.length; i++) {
        	// Parse each string in dates as a date
            LocalDate productDate = ____.____(dates[i], formatters[i]);
            System.out.println("Date " + dates[i] + " is standardized as " + productDate);
        }

        LocalDate manufacturingDate = LocalDate.parse("2023-01-01");
        // Display date as full month name, day without leading zeros, and full year
        DateTimeFormatter displayFormat = DateTimeFormatter.ofPattern("____ ____, ____");
        String formattedDate = manufacturingDate.format(displayFormat);
        
        System.out.println("\nFormatted manufacturing date: " + formattedDate);
    }
}
Edit and Run Code