-->
Skip to main content

LocalDate to LocalDateTime Conversion in Java [2021]

LocalDate to LocalDateTime Conversion in Java [2021]

In this tutorial, we will see how to Convert LocalDate to LocalDateTime in Java. In the earlier tutorial, we have covered how we can Convert LocalDateTime to LocalDate in Java 

LocalDate to LocalDateTime in Java


Here is the brief intro about what is LocalDateTime and LocalDate class in java.

LocalDateTime

  • LocalDateTime is an instance of date-time without a time-zone in the ISO-8601 calendar system, such as 2017-12-03T10:15:30.
  • LocalDateTime is an immutable date-time object that represents a date-time, often viewed as a year-month-day-hour-minute-second.

LocalDate

  1. LocalDate provides date format as YYYY-MM-dd such as 2017-12-03.
  2. LocalDate is a value-based class thus use of equals method is recommended for comparisons.
  3. The LocalDate class does not have time or timezone data, So LocalDate is preferable for Birthday, National Holiday representation.
As we can see from the above introduction LocalDate only have date part so to Convert LocalDate instance to LocalDateTime instance we have to add time part in LocalDate instance.

So we Can Say LocalDateTime = LocalDate + time information.

LocalDate to LocalDateTime Conversion

There are 5 methods provided by LocalDate class to Convert LocalDate instance to LocalDateTime instance.
  1. public LocalDateTime atStartOfDay()
  2. public LocalDateTime atTime(LocalTime time)
  3. public LocalDateTime atTime(int hour, int minute)
  4. public LocalDateTime atTime(int hour, int minute, int second)
  5. public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)
We will now see each method one by one 

1. public LocalDateTime atStartOfDay()

This method combines LocalDate passed with the time of midnight to create a new LocalDateTime at the start of the passed date.

Returns:

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

Example: 

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

 public class LocalDateToLocalDateTimeExample1 {
    public static void main(String[] args) {
	// Gives LocalDate instance
	LocalDate todaysDate = LocalDate.now();

	// atStartOfDay will give date with timestamp 00:00 in LocalDateTimeFormat
	LocalDateTime atStartOfDay = todaysDate.atStartOfDay();
	System.out.println("LocalDateTime atStartOfDay : " + atStartOfDay);
	// Prints LocalDateTime atStartOfDay : 2020-06-30T00:00
    }
 }

Output:

LocalDateTime atStartOfDay : 2020-06-30T00:00

2. public LocalDateTime atTime(LocalTime time)

This method combines given Localdate with a time to create a LocalDateTime instance.
This returns a LocalDateTime formed from this date at the specified time. 

Specified by:

atTime method is present in interface ChronoLocalDate which is implemented by LocalDate Class.

Parameters:

time - the time to combine with which is not null.

Returns:

the local date-time formed from the given date and the specified time which is not null.

Example: 

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

 public class LocalDateToLocalDateTimeExample2 {
    public static void main(String[] args) {
	// Gives LocalDate instance
	LocalDate todaysDate = LocalDate.now();

	// Gives LocalDateTime using LocalTime.
	LocalDateTime atLocalTime = todaysDate.atTime(LocalTime.now());
	System.out.println("LocalDateTime using LocalTime : " + atLocalTime);
	// Prints LocalDateTime using LocalTime : 2020-06-30T14:08:18.484

    }
 }   

Output:

LocalDateTime using LocalTime : 2020-06-30T14:08:18.484

3. public LocalDateTime atTime(int hour, int minute)

  • This method combines a given date with a time to create a LocalDateTime.
  • The hour and minute field must be within their valid range. 

Parameters:

hour - the hour-of-day to use ranges from 0 to 23.
minute - the minute-of-hour to use ranges from 0 to 59.

Returns:

the local date-time formed from this date and the specified time which is not null.

Throws:

DateTimeException is thrown if the value of any field is out of range.

Example: 

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

 public class LocalDateToLocalDateTimeExample3 {
    public static void main(String[] args) {
	// Gives LocalDate instance
	LocalDate todaysDate = LocalDate.now();

	// Gives LocalDateTime at specific hour and minute
	LocalDateTime atTime = todaysDate.atTime(23, 00);
	System.out.println("LocalDateTime using specific hour and minute : " + atTime);
	// Prints LocalDateTime using specific hour and minute : 2020-06-30T23:00

    }
 }

Output:

   LocalDateTime using specific hour and minute : 2020-06-30T23:00

4. public LocalDateTime atTime(int hour, int minute, int second)

  • This method combines this date with a time to create a LocalDateTime.
  • This returns a LocalDateTime instance formed from this date at the specified hour, minute and second. 
  • The individual time fields must be within their valid range.

Parameters:

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

Returns:

the local date-time formed from this date and the specified time which is not null.

Throws:

DateTimeException is thrown if the value of any field is out of range.

Example:

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

 public class LocalDateToLocalDateTimeExample4 {
     public static void main(String[] args) {
	 //Gives LocalDate instance
	 LocalDate todaysDate = LocalDate.now();
		
	 //Gives LocalDateTime at specific hour,minute and seconds
	 LocalDateTime atTimeWithSeconds = todaysDate.atTime(23, 00, 15);
	 System.out.println("LocalDateTime using specific hour, minute, seconds : " + atTimeWithSeconds);
	 // Prints LocalDateTime using specific hour, minute, seconds :
	 // 2020-06-30T23:00:15

     }
 }

Output:

LocalDateTime using specific hour, minute, seconds : 2020-06-30T23:00:15

5. public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

  • This method combines this date with a time to create a LocalDateTime.
  • This returns a LocalDateTime formed from this date at the specified hour, minute, second and nanosecond. 
  • The individual time fields must be within their valid range.

Parameters:

hour - the hour-of-day to use ranges from 0 to 23.
minute - the minute-of-hour to use ranges from 0 to 59.
second - the second-of-minute to represent ranges from 0 to 59.
nanoOfSecond - the nano-of-second to represent ranges from 0 to 999,999,999.

Returns:

the local date-time formed from this date and the specified time which is not null.

Throws:

DateTimeException is thrown if the value of any field is out of range.

Example:

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

 public class LocalDateToLocalDateTimeExample5 {
    public static void main(String[] args) {
	//Gives LocalDate instance
	LocalDate todaysDate = LocalDate.now();
		
	//Gives LocalDateTime at specific hour,minute,second,nanoseconds
	LocalDateTime atTimeWithNanoSeconds = todaysDate.atTime(23, 00, 15, 2345);
	System.out.println("LocalDateTime using specific hour, minute, seconds, nanoseconds : " + atTimeWithNanoSeconds);
	// Prints LocalDateTime using specific hour, minute, seconds, nanoseconds :
	// 2020-06-30T23:00:15.000002345

     }
 }

Output:

LocalDateTime using specific hour, minute, seconds, nanoseconds : 2020-06-30T23:00:15.000002345

Conclusion:

Comments