Software Development

How can I efficiently change a date string from one format to another using Java 8 or later?

JO Asked by Joseph Harris · 15-05-2025
0 upvotes 11,496 views 0 comments
The question

I have a set of date strings currently in the format "dd-MM-yyyy" and I need to transform them into "yyyy/MM/dd" for a database migration. I am looking for a modern approach that doesn't involve the old, thread-unsafe SimpleDateFormat. How can I use the java.time package to parse the original string and then reformat it without running into common parsing exceptions?

3 answers

0
KI
Answered on 17-05-2025

The most reliable way to do this in modern Java is using the DateTimeFormatter class from the java.time package. First, you define a formatter that matches your input pattern, then parse the string into a LocalDate object. After that, you create a second formatter for your desired output pattern and format the date object back into a string. Unlike the old SimpleDateFormat, DateTimeFormatter is immutable and thread-safe, making it ideal for multi-threaded applications or high-concurrency web servers. This approach also provides much better error messaging if the input string doesn't match the expected pattern, which helps a lot during the debugging process of large data migrations.

0
MA
Answered on 19-05-2025

This approach works great for standard dates, but what if the input string contains an unexpected time component or an offset? Would I need to use LocalDateTime instead of LocalDate to avoid a DateTimeParseException?

RI 21-05-2025

You hit the nail on the head, Matthew. If your string has a time element like "15-05-2024 10:30", LocalDate will throw an error because it can't find the time fields. In that case, you should parse it with LocalDateTime.parse(str, inputFormatter). If you only care about the date for the final output, you can then call .toLocalDate() on that object before formatting it back to your target string. This ensures your code is robust enough to handle varying levels of detail in your source data.

0
BA
Answered on 10-06-2025

You can achieve this in two lines: LocalDate date = LocalDate.parse(input, inputFormatter); followed by String output = date.format(outputFormatter);. It’s clean and very readable.

JO 12-06-2025

I agree with Barbara. Since I switched to the java.time API, I've had significantly fewer production bugs related to date parsing, especially when dealing with different time zones and leap years.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session