Parse and Format LocalDate,LocalDateTime,LocalTime

parse method used to convert String to LocalDate,LocalDateTime,LocalTime using the DateTimeFormatter  class.

format method used to convert LocalDate,LocalDateTime,LocalTime to String using the DateTimeFormatter class.

The DateTimeFormatter class provides various predefined formatters or you can define your own pattern using ofPattern method.

1.Parse and Format LocalDate

package com.kp.work.java8;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateParseFormatExample {

	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");

	public static LocalDate convertStringToLocalDate(String formattedCurrentDate) {
		LocalDate localDate = LocalDate.parse(formattedCurrentDate, dateTimeFormatter);
		return localDate;
	}

	public static String convertLocalDateToString(LocalDate date) {
		String formattedStringDate = date.format(dateTimeFormatter);
		return formattedStringDate;
	}

	public static void main(String[] args) {

		LocalDate currentDate = LocalDate.now();
		System.out.println("LocalDate::" + currentDate);

		String formattedCurrentDate = convertLocalDateToString(currentDate);
		System.out.println("LocalDateToString::" + formattedCurrentDate);

		LocalDate localDate = convertStringToLocalDate(formattedCurrentDate);
		System.out.println("StringToLocalDate::" + localDate);

	}

}

2.Parse and Format LocalDateTime

package com.kp.work.java8;

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

public class DateTimeParseFormatExample{
	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss:SSS");

	public static LocalDateTime convertStringToLocalDateTime(String formattedCurrentDate) {
		LocalDateTime localDateTime = LocalDateTime.parse(formattedCurrentDate, dateTimeFormatter);
		return localDateTime;
	}

	public static String convertLocalDateTimeToString(LocalDateTime dateTime) {
		String formattedStringDateTime = dateTime.format(dateTimeFormatter);
		return formattedStringDateTime;
	}

	public static void main(String[] args) {
		LocalDateTime currentDateTime = LocalDateTime.now();
		System.out.println("LocalDateTime::" + currentDateTime);
		String formattedCurrentDateTime = convertLocalDateTimeToString(currentDateTime);
		System.out.println("LocalDateTimeToString::" + formattedCurrentDateTime);
		LocalDateTime localDateTime = convertStringToLocalDateTime(formattedCurrentDateTime);
		System.out.println("StringToLocalDateTime::" + localDateTime);
	}
}

3.Parse and Format LocalTime

package com.kp.work.java8;

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

public class TimeParseFormatExample {
	
	private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss:SSS");

	public static LocalTime convertStringToLocalTime(String formattedCurrentDate) {
		LocalTime localTime = LocalTime.parse(formattedCurrentDate, dateTimeFormatter);
		return localTime;
	}

	public static String convertLocalTimeToString(LocalTime localtime) {
		String formattedStringTime= localtime.format(dateTimeFormatter);
		return formattedStringTime;
	}

	public static void main(String[] args) {

		LocalTime currentDateTime = LocalTime.now();
		System.out.println("LocalTime::" + currentDateTime);

		String formattedCurrentTime = convertLocalTimeToString(currentDateTime);
		System.out.println("LocalTimeToString::" + formattedCurrentTime);

		LocalTime localTime = convertStringToLocalTime(formattedCurrentTime);
		System.out.println("StringToLocalTime::" + localTime);

	}

	

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s