Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Formatting and parsing dates and times

Header Image

Ahoy matey! Welcome to another exciting adventure in the world of Java Date and Time! Today we will be talking about formatting and parsing dates and times. Aye, this topic may seem dry and boring, but trust me, with a little creativity and imagination, we can make it fun and exciting.

Using patterns to format dates and times

Before we dive into the deep end, let’s start with the basics. What does it mean to format a date or time? When we format a date or time, we are essentially taking a raw date or time object and converting it into a human-readable string format.

In Java, we can format dates and times using the DateTimeFormatter class. The DateTimeFormatter class allows us to specify a pattern that tells Java how to format the date or time.

For example, let’s say we have a LocalDateTime object representing the current date and time. We can format this object into a string using the DateTimeFormatter class like this:

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDateTime = now.format(formatter);

In this example, we have created a DateTimeFormatter object with a pattern of “dd-MM-yyyy HH:mm:ss”. This pattern tells Java to format the date as “day-month-year hour:minute:second”. We then use the format method of the LocalDateTime object to format the date and time into a string.

Now, if we were to print out the formattedDateTime string, we would get something like this:

26-04-2023 11:32:45

Pretty cool, eh? With just a few lines of code, we have taken a boring LocalDateTime object and turned it into a human-readable string that we can use to impress our pirate friends.

But wait, there’s more! The DateTimeFormatter class also allows us to format dates and times in different locales. For example, let’s say we want to format the date and time in French. We can do that like this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss", Locale.FRANCE);
String formattedDateTime = now.format(formatter);

In this example, we have added a second parameter to the ofPattern method to specify the Locale. We have set the Locale to Locale.FRANCE, which tells Java to format the date and time using French language conventions.

Now, if we were to print out the formattedDateTime string, we would get something like this:

26-04-2023 11:32:45

Okay, okay, I know what you’re thinking. “That’s not French!” Well, actually, it is. In France, they use the same date and time format as we do in the US. But trust me, if we were formatting dates and times in Japanese or Arabic, it would look very different.

Conclusion

That’s it for now, me hearties! In this article, we learned how to use patterns to format dates and times in Java. We also learned how to format dates and times in different locales. In the next article, we will cover how to parse strings to create dates and times. Until then, happy formatting!

Parsing strings to create dates and times

Now that we’ve covered how to format dates and times, it’s time to learn how to do the opposite: parsing strings to create date and time objects.

In Java, we can parse strings into date and time objects using the DateTimeFormatter class. The DateTimeFormatter class allows us to specify a pattern that tells Java how to parse the string into a date or time object.

For example, let’s say we have a string representing a date and time in the format “26-04-2023 11:32:45”. We can parse this string into a LocalDateTime object using the DateTimeFormatter class like this:

String dateTimeString = "26-04-2023 11:32:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);

In this example, we have created a DateTimeFormatter object with a pattern of “dd-MM-yyyy HH:mm:ss”. This pattern tells Java how to parse the string into a LocalDateTime object. We then use the parse method of the LocalDateTime class to parse the string into a date and time object.

Now, if we were to print out the parsedDateTime object, we would get something like this:

2023-04-26T11:32:45

Again, pretty cool, eh? With just a few lines of code, we have taken a string and turned it into a LocalDateTime object that we can use in our Java code.

But wait, there’s more! The DateTimeFormatter class also allows us to parse strings in different locales. For example, let’s say we want to parse a date and time string in French. We can do that like this:

String dateTimeString = "26-04-2023 11:32:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss", Locale.FRANCE);
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);

In this example, we have added a second parameter to the ofPattern method to specify the Locale. We have set the Locale to Locale.FRANCE, which tells Java how to parse the date and time string using French language conventions.

Now, if we were to print out the parsedDateTime object, we would get the same result as before:

2023-04-26T11:32:45

Conclusion

Ahoy, me hearties! In this article, we learned how to use the DateTimeFormatter class to parse strings into date and time objects in Java. We also learned how to parse strings in different locales.

With the knowledge we’ve gained in this article, we can now confidently format and parse dates and times in our Java code like true pirate programmers. Until next time, happy coding!