Java – Format LocalDateTime to dd/MM/yyyy HH:mm:ss

Introduction

When working with date and time values in Java, you might need to format a LocalDateTime object into a specific string format, such as dd/MM/yyyy HH:mm:ss. This format is commonly used in many applications, especially in regions that follow the day-month-year convention. Java 8’s java.time package provides a powerful DateTimeFormatter class that makes formatting date-time values straightforward and efficient.

In this guide, we’ll explore how to format a LocalDateTime to the dd/MM/yyyy HH:mm:ss pattern using Java 8’s DateTimeFormatter class.

Table of Contents

  • Problem Statement
  • Solution Steps
  • Java Program
    • Formatting LocalDateTime to dd/MM/yyyy HH:mm:ss
    • Parsing a dd/MM/yyyy HH:mm:ss String Back to LocalDateTime
  • Advanced Considerations
  • Conclusion

Problem Statement

The task is to create a Java program that:

  • Converts a LocalDateTime object to a string formatted as dd/MM/yyyy HH:mm:ss.
  • Optionally, parses a string in the dd/MM/yyyy HH:mm:ss format back into a LocalDateTime object.

Example:

  • Input: A LocalDateTime object representing 2024-08-30T14:30:45.
  • Output: A string formatted as 30/08/2024 14:30:45.

Solution Steps

  1. Use DateTimeFormatter.ofPattern(): Define a formatter with the dd/MM/yyyy HH:mm:ss pattern.
  2. Format the LocalDateTime: Use the format() method of LocalDateTime with the defined formatter.
  3. Parse the String: Convert a dd/MM/yyyy HH:mm:ss formatted string back to a LocalDateTime using the same formatter.

Java Program

Formatting LocalDateTime to dd/MM/yyyy HH:mm:ss

You can format a LocalDateTime to the dd/MM/yyyy HH:mm:ss format using the DateTimeFormatter class.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Java - Format LocalDateTime to dd/MM/yyyy HH:mm:ss
 * Author: https://www.rameshfadatare.com/
 */
public class FormatLocalDateTime {

    public static void main(String[] args) {
        // Step 1: Create a LocalDateTime object
        LocalDateTime dateTime = LocalDateTime.of(2024, 8, 30, 14, 30, 45);

        // Step 2: Define the DateTimeFormatter with the desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");

        // Step 3: Format the LocalDateTime to a string
        String formattedDateTime = dateTime.format(formatter);

        // Step 4: Display the formatted date-time
        System.out.println("Formatted Date-Time: " + formattedDateTime);
    }
}

Output

Formatted Date-Time: 30/08/2024 14:30:45

Explanation

  • DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss") creates a formatter that follows the dd/MM/yyyy HH:mm:ss pattern.
  • dateTime.format(formatter) converts the LocalDateTime into a string formatted as dd/MM/yyyy HH:mm:ss.

Parsing a dd/MM/yyyy HH:mm:ss String Back to LocalDateTime

If you have a date-time string in the dd/MM/yyyy HH:mm:ss format, you can parse it back into a LocalDateTime object using the same formatter.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Java - Parse String in dd/MM/yyyy HH:mm:ss to LocalDateTime
 * Author: https://www.rameshfadatare.com/
 */
public class ParseDateTimeString {

    public static void main(String[] args) {
        // Step 1: Define a date-time string in the dd/MM/yyyy HH:mm:ss format
        String dateTimeString = "30/08/2024 14:30:45";

        // Step 2: Define the DateTimeFormatter with the desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");

        // Step 3: Parse the string to a LocalDateTime
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);

        // Step 4: Display the LocalDateTime
        System.out.println("Parsed LocalDateTime: " + dateTime);
    }
}

Output

Parsed LocalDateTime: 2024-08-30T14:30:45

Explanation

  • DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss") is used to define the pattern that matches the input string.
  • LocalDateTime.parse(dateTimeString, formatter) converts the string back to a LocalDateTime object using the formatter.

Advanced Considerations

  • Locale Handling: If your date-time strings are locale-specific, you can pass a Locale object to DateTimeFormatter.ofPattern() to handle locale-specific formatting and parsing.

  • Validation: Ensure that your input strings strictly follow the dd/MM/yyyy HH:mm:ss pattern. Any deviation in the format will result in a DateTimeParseException.

  • Immutability: The LocalDateTime and DateTimeFormatter classes are immutable and thread-safe, making them ideal for use in concurrent applications.

Conclusion

This guide provides methods for formatting a LocalDateTime to the dd/MM/yyyy HH:mm:ss pattern in Java, as well as parsing a string in this format back to a LocalDateTime. The DateTimeFormatter class offers a flexible and powerful way to handle custom date-time formats, ensuring that your Java applications can easily convert between LocalDateTime objects and formatted date-time strings.

Leave a Comment

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

Scroll to Top