Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, December 6, 2020

Java Convert LocalDate to LocalDateTime

1. Overview

In this tutorial, We'll learn how to convert LocalDate to LocalDateTime in java 8. LocalDate class stores only year, month and day. But, this does not store the time part and timezone. Whereas LocalDateTime holds the date and time part with naoseoncds.

You might have doubt by this time, how to convert the LocalDate to LocalDateTime because LocalDate is not having the time part but LocalDateTime does have. In order to convert localdate to localdatetime, we need to explicitly append the time part as needed.

This conversion can be done in two ways. Use atStartOfDay() or atTime() methods to append the time part to localdate and create a new LocalDateTime object.

Syntax:

public LocalDateTime atStartOfDay()

public LocalDateTime atTime(int hour, int minute)
public LocalDateTime atTime(int hour,
                            int minute,
                            int second)
public LocalDateTime atTime(int hour,
                            int minute,
                            int second,
                            int nanoOfSecond)							
public LocalDateTime atTime(LocalTime time)

Java Convert LocalDate to LocalDateTime


2. LocalDate to LocalDateTime using atStartOfDay() - Start Of Day


First we will look at atStartOfDay() method and how conversion is done into LocalDateTime with time part because this method is not taking any argument.

atStartOfDay() method combines localdate with the time of midnight to create a LocalDateTime at the start of this date.

This method returns a LocalDateTime formed from this date at the time of midnight, 00:00, at the start of this date.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 
 * Example to convert from LocalDate to LocalDateTime using atStartOfDay() method.
 * 
 * @author javaprogramto.com
 *
 */
public class LocalDateAtStartOfDayExample {

	public static void main(String[] args) {
		
		// creating localdate object
		LocalDate localDate = LocalDate.now();
		
		// Getting the LocalDateTime from LocalDate with atStartOfDay()
		LocalDateTime localDateTime = localDate.atStartOfDay();
		
		// printing the dates
		System.out.println("LocalDate : "+localDate);
		System.out.println("LocalDateTime : "+localDateTime);
	}
}

Output:
LocalDate : 2020-12-06
LocalDateTime : 2020-12-06T00:00

From the above output, we can see that Localdatetime is appended with hours and minutes as 00. That is LocalDateTime object is set to the start of LocalDate. This method is useful if you want to just convert LocalDate to LocalDateTime with time part as 0.


3. LocalDate to LocalDateTime using atTime() 


Next, Let us look at another method atTime() with time parameters. atTime() method is an overloaded method with different type of time arguments.

atTime() method combines this date with a time to create a LocalDateTime. The returned LocalDateTime object is added with the given time values.  But there is another overloaded method that returns ZonedLocalDate.

hour - the hour-of-day to use, from 0 to 23
minute - the minute-of-hour to use, from 0 to 59
second - the second-of-minute to represent, from 0 to 59

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateToLocalDateTimeAtTimeExample {

	public static void main(String[] args) {
		// localdate
		LocalDate localDate = LocalDate.now();

		int hour = 10;
		int minute = 20;
		int second = 30;
		
		// setting time details and converting LocalDate to LocalDateTime. Remaining parts are set to 0.
		LocalDateTime dateAndTime1 = localDate.atTime(hour, minute);

		// this produces the output as same as atStartOfDay()
		LocalDateTime dateAndTime2 = localDate.atTime(0, 0);

		// with custom time parts
		LocalDateTime dateAndTime3 = localDate.atTime(hour, minute, second);
		
		// From LocalTime
		LocalDateTime dateAndTime4 = localDate.atTime(LocalTime.now());

		// printing the LocalDateTime values
		System.out.println("LocalDate : " + localDate);
		System.out.println("LocalDateTime with hour, minute : " + dateAndTime1);
		System.out.println("LocalDateTime with hour minute as 00 : " + dateAndTime2);
		System.out.println("LocalDateTime with hour, minute, second : " + dateAndTime3);
		System.out.println("LocalDateTime 4 with LocalTime including nano seconds : " + dateAndTime4);
	}
}

Output:

LocalDate : 2020-12-06
LocalDateTime with hour, minute : 2020-12-06T10:20
LocalDateTime with hour minute as 00 : 2020-12-06T00:00
LocalDateTime with hour, minute, second : 2020-12-06T10:20:30
LocalDateTime 4 with LocalTime including nano seconds : 2020-12-06T23:33:53.337752


4. Conclusion


In this article, we've seen how to convert LocalDate to LocalDateTime in java 8 date time api and shown the example programs in 2 ways.



No comments:

Post a Comment

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