Java LocalDateTime parse() Method

The parse() method in Java, part of the java.time.LocalDateTime class, is used to obtain an instance of LocalDateTime from a string representation using a specified formatter. This method is useful for converting date-time strings into LocalDateTime instances.

Table of Contents

  1. Introduction
  2. parse() Method Syntax
  3. Understanding parse()
  4. Examples
    • Basic Usage
    • Using parse() with Custom Formatters
  5. Real-World Use Case
  6. Conclusion

Introduction

The parse() method allows you to convert a string representation of a date-time into a LocalDateTime instance. This is particularly useful for reading date-time values from text input or files and converting them into LocalDateTime objects for further processing.

parse() Method Syntax

The parse() method has two overloads:

Overload 1: Using Default Formatter

public static LocalDateTime parse(CharSequence text)

Overload 2: Using Custom Formatter

public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)

Parameters:

  • text: The text to parse, not null.
  • formatter: The formatter to use, not null (for the second overload).

Returns:

  • A LocalDateTime parsed from the text, not null.

Throws:

  • DateTimeParseException if the text cannot be parsed.

Understanding parse()

The parse() method parses a text string to produce a LocalDateTime instance. The method can use either the default ISO date-time format or a custom format specified by a DateTimeFormatter.

Examples

Basic Usage

To demonstrate the basic usage of parse(), we will parse a date-time string using the default formatter.

Example

import java.time.LocalDateTime;

public class LocalDateTimeParseExample {
    public static void main(String[] args) {
        String dateTimeString = "2023-06-15T10:30";
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString);

        System.out.println("Parsed DateTime: " + dateTime);
    }
}

Output:

Parsed DateTime: 2023-06-15T10:30

Using parse() with Custom Formatters

This example shows how to use the parse() method with a custom formatter to parse a date-time string.

Example

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

public class LocalDateTimeParseCustomFormatterExample {
    public static void main(String[] args) {
        String dateTimeString = "15-06-2023 10:30:45";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);

        System.out.println("Parsed DateTime: " + dateTime);
    }
}

Output:

Parsed DateTime: 2023-06-15T10:30:45

Real-World Use Case

Reading Date-Time from Input

In real-world applications, the parse() method can be used to read date-time values from user input or text files and convert them into LocalDateTime instances for further processing.

Example

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

public class DateTimeInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter date and time in format dd-MM-yyyy HH:mm:ss:");
        String input = scanner.nextLine();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(input, formatter);

        System.out.println("Parsed DateTime: " + dateTime);
    }
}

Output:

Enter date and time in format dd-MM-yyyy HH:mm:ss:
15-06-2023 10:30:45
Parsed DateTime: 2023-06-15T10:30:45

Conclusion

The LocalDateTime.parse() method is used to convert a string representation of a date-time into a LocalDateTime instance. This method is particularly useful for reading date-time values from text input or files and converting them into LocalDateTime objects for further processing. By understanding and using the parse() method, you can effectively manage and manipulate date-time data in your Java applications.

Comments