Pages

Wednesday, December 2, 2020

Java 8 - How To Format or Parse Date to LocalDateTime And Back?

1. Overview

In this tutorial, We'll learn in java 8 how to convert string date to LocalDateTime object and LocalDateTime back to String format.

We can parse the string date in any valid format to date object such as LocalDateTime.

Let us take the input string in "yyyy-MM-dd HH:mm:ss" format.

Java 8 - How To Format or Parse Date to LocalDateTime?


2. Parse String Date to LocalDateTime Java 8 API

Follow the below steps to convert string to LocalDateTime object.


Step 1: Create the String with the valid date.


Step 2: Create DateTimeFormatter object using DateTimeFormatter.ofPattern() which input string date pattern is. In our case, we have string date is in yyyy-MM-dd HH:mm:ss format, so we need to pass the same format to ofPattern() method.


Step 3: Use LocalDateTime.parse() method and pass string date, datetimeformatter. Finally, parse() method returns LocalDateTime object with the date string values.


Next, look at the following example code that is implemented using all these steps.

package com.javaprogramto.java8.dates.parse;

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

public class ParseStringToLocalDateTime {

	public static void main(String[] args) {
		
		// Date in string format
		String dateInStr = "2022-01-01 23:30:50";
		
		// Creating datetimeformatter object with pattern "yyyy-MM-dd HH:mm:ss"
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		
		// converting string to date time using custom formatter
		LocalDateTime datetime = LocalDateTime.parse(dateInStr, dateTimeFormatter);
		
		// printing the input and output
		System.out.println("Input date in string : "+dateInStr);
		System.out.println("Output LocalDateTime : "+datetime);
	}
}
 

Output:

Input date in string : 2022-01-01 23:30:50
Output LocalDateTime : 2022-01-01T23:30:50
 


3. Format LocalDateTime To String Using Java 8 API

Now, We need to take the LocalDateTime object and format it back to String in the different format as "yyyy-MMM-dd hh:mm a".

Next, take the output from the above section and format it to string.


Input string format: yyyy-MM-dd HH:mm:ss

Output String format: yyyy-MMM-dd hh:mm a


Now the difference is that output date string is added more descriptive month and AM/PM section.

package com.javaprogramto.java8.dates.parse;

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

public class FormatLocalDateTimeToString {

	public static void main(String[] args) {

		// Getting LocalDateTime object.
		LocalDateTime datetime = getLocalDateTime();
		
		// Creating datetimeformatter object with pattern "yyyy-MMM-dd hh:mm a"
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm a");
		
		// formatting LocalDateTime to String
		String dateInString = datetime.format(dateTimeFormatter);
		
		// printing the output string
		System.out.println("LocalDateTime back in String form : "+dateInString);
	}

	private static LocalDateTime getLocalDateTime() {
		// Date in string format
		String dateInStr = "2022-01-01 23:30:50";

		// Creating datetimeformatter object with pattern "yyyy-MM-dd HH:mm:ss"
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		// Returning LocalDateTime object.
		return LocalDateTime.parse(dateInStr, dateTimeFormatter);
	}
}
 

Output:

LocalDateTime back in String form : 2022-Jan-01 11:30 PM
 

4. Conclusion

In this article, We've seen how to convert String to LocalDateTime and back in java 8 with example programs.

GitHub

Ref

LocalDateTime

String Methods

No comments:

Post a Comment

Please do not add any spam links in the comments section.