Java 8 – How to form LocalDateTime passing Date and Time fields ?

In this article, we will learn how to form LocalDateTime passing Date & Time fields using different methods provided in Java 1.8 version

Form LocalDateTime passing Date & Time Fields :

  • LocalDateTime.of() method returns LocalDateTime passing Date (Day/Month/Year) and Time (Hour/Minute/Second/Nanosecond) fields
  • There are 7 variants of LocalDateTime.of() methods –
    1. LocalDateTime.of(year, month, dayOfMonth, hour, minute) –
      • Obtains an instance of LocalDateTime from year, month, day, hour and minute
      • Setting the second and nanosecond to zero
    2. LocalDateTime.of(year, month, dayOfMonth, hour, minute, second) –
      • Obtains an instance of LocalDateTime from year, month, day, hour, minute and second
      • Setting the nanosecond to zero
    3. LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond) –
      • Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond
    4. LocalDateTime.of(year, Month, dayOfMonth, hour, minute) –
      • Obtains an instance of LocalDateTime from year, month, day, hour and minute
      • Setting the second and nanosecond to zero
    5. LocalDateTime.of(year, Month, dayOfMonth, hour, minute, second) –
      • Obtains an instance of LocalDateTime from year, month, day, hour, minute and second
      • Setting the nanosecond to zero
    6. LocalDateTime.of(year, Month, dayOfMonth, hour, minute, second, nanoOfSecond) –
      • Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond
    7. LocalDateTime.of(LocalDate, LocalTime) –
      • Obtains an instance of LocalDateTime from a date/LocalDate and time/LocalTime
  • Finally, print LocalDateTime in different formatted style like SHORT & MEDIUM to the console

FormLocalDateTime.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class FormLocalDateTime {

	public static void main(String[] args) {

		// 1. form LocalDateTime passing Date(year/month/day) & Time(hour/minute)
		LocalDateTime localDateTime1 = LocalDateTime.of(2019, 9, 26, 11, 35);
		System.out.println("LocalDateTime :- " + localDateTime1);


		// 1.1 format LocalDateTime1 in FormatStyle.SHORT
		String formattedStr1 = localDateTime1.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
		System.out.println("LocalDateTime in FormatStyle.SHORT :- " + formattedStr1);



		// 2. form LocalDateTime passing Date(year/month/day) & Time(hour/minute/second)
		LocalDateTime localDateTime2 = LocalDateTime.of(2019, 9, 26, 11, 35, 26);
		System.out.println("\nLocalDateTime :- " + localDateTime2);


		// 2.1 format LocalDateTime2 in FormatStyle.MEDIUM
		String formattedStr2 = localDateTime2.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
		System.out.println("LocalDateTime in FormatStyle.MEDIUM :- " + formattedStr2);



		// 3. form LocalDateTime passing Date(year/month/day) & Time(hour/minute/second/nano)
		LocalDateTime localDateTime3 = LocalDateTime.of(2019, 9, 26, 11, 35, 26, 425);
		System.out.println("\nLocalDateTime :- " + localDateTime3);


		// 3.1 format localDateTime3 in FormatStyle.MEDIUM
		String formattedStr3 = localDateTime3.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
		System.out.println("LocalDateTime in FormatStyle.MEDIUM :- " + formattedStr3);



		// 4. form LocalDateTime passing Date(year/Month/day) & Time(hour/minute)
		LocalDateTime localDateTime4 = LocalDateTime.of(2019, Month.SEPTEMBER, 26, 11, 35);
		System.out.println("\nLocalDateTime :- " + localDateTime4);


		// 4.1 format LocalDateTime4 in FormatStyle.SHORT
		String formattedStr4 = localDateTime4.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
		System.out.println("LocalDateTime in FormatStyle.SHORT :- " + formattedStr4);



		// 5. form LocalDateTime passing Date(year/Month/day) & Time(hour/minute/second)
		LocalDateTime localDateTime5 = LocalDateTime.of(2019, Month.SEPTEMBER, 26, 11, 35, 26);
		System.out.println("\nLocalDateTime :- " + localDateTime5);


		// 5.1 format LocalDateTime5 in FormatStyle.MEDIUM
		String formattedStr5 = localDateTime5.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
		System.out.println("LocalDateTime in FormatStyle.MEDIUM :- " + formattedStr5);



		// 6. form LocalDateTime passing Date(year/Month/day) & Time(hour/minute/second/nano)
		LocalDateTime localDateTime6 = LocalDateTime.of(2019, Month.SEPTEMBER, 26, 11, 35, 26, 425);
		System.out.println("\nLocalDateTime :- " + localDateTime6);


		// 6.1 format LocalDateTime6 in FormatStyle.SHORT
		String formattedStr6 = localDateTime6.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
		System.out.println("LocalDateTime in FormatStyle.SHORT :- " + formattedStr6);



		// 7. form LocalDateTime passing LocalDate(year/Month/day) & LocalTime(hour/minute/second/nano)
		LocalDate localDate = LocalDate.now();
		LocalTime localTime = LocalTime.now();
		LocalDateTime localDateTime7 = LocalDateTime.of(localDate, localTime);
		System.out.println("\nLocalDateTime :- " + localDateTime7);


		// 7.1 format LocalDateTime7 in FormatStyle.MEDIUM
		String formattedStr7 = localDateTime7.format(
				DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
		System.out.print("LocalDateTime in FormatStyle.MEDIUM :- " + formattedStr7);
	}
}

Output:

LocalDateTime :- 2019-09-26T11:35
LocalDateTime in FormatStyle.SHORT :- 26/09/19, 11:35 am

LocalDateTime :- 2019-09-26T11:35:26
LocalDateTime in FormatStyle.MEDIUM :- 26-Sep-2019, 11:35:26 am

LocalDateTime :- 2019-09-26T11:35:26.000000425
LocalDateTime in FormatStyle.MEDIUM :- 26-Sep-2019, 11:35:26 am

LocalDateTime :- 2019-09-26T11:35
LocalDateTime in FormatStyle.SHORT :- 26/09/19, 11:35 am

LocalDateTime :- 2019-09-26T11:35:26
LocalDateTime in FormatStyle.MEDIUM :- 26-Sep-2019, 11:35:26 am

LocalDateTime :- 2019-09-26T11:35:26.000000425
LocalDateTime in FormatStyle.SHORT :- 26/09/19, 11:35 am

LocalDateTime :- 2022-08-08T17:22:15.816547500
LocalDateTime in FormatStyle.MEDIUM :- 08-Aug-2022, 5:22:15 pm

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
Java 8 – How to get Date and Time fields from LocalDateTime ?