Java localtime to localdatetime

Java Convert LocalTime to LocalDateTime

In this Java core tutorial we learn how to convert a java.time.LocaTime object with a given java.time.LocalDate value into a java.time.LocalDateTime object.

How to convert LocalTime to LocalDateTime in Java

With a given LocalTime object in Java, we can combine with a specified LocalDate object to create a LocalDateTime object by using the LocalTime.atDate(). The following Java program to show how to use the LocalTime.atDate() method to convert LocalTime object with current date of system to LocalDateTime object.

import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class ConvertLocalTimeToLocalDateTimeExample1  public static void main(String. args)  int hour = 1; int minute = 10; int second = 20; LocalTime localTime = LocalTime.of(hour, minute, second); LocalDate today = LocalDate.now(); LocalDateTime localDateTime = localTime.atDate(today); System.out.println("LocalTime: " + localTime); System.out.println("LocalDateTime: " + localDateTime); > >
LocalTime: 01:10:20 LocalDateTime: 2022-04-21T01:10:20

In the following Java code we show how to combined LocalTime object with a specified LocalDate object to create new LocalDateTime object.

import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class ConvertLocalTimeToLocalDateTimeExample2  public static void main(String. args)  int hour = 1; int minute = 10; int second = 20; int year = 2022; int month = 1; int day = 1; LocalDate localDate = LocalDate.of(year, month, day); LocalTime localTime = LocalTime.of(hour, minute, second); LocalDateTime localDateTime = localTime.atDate(localDate); System.out.println("LocalDate: " + localDate); System.out.println("LocalTime: " + localTime); System.out.println("LocalDateTime: " + localDateTime); > >
LocalDate: 2022-01-01 LocalTime: 01:10:20 LocalDateTime: 2022-01-01T01:10:20

Источник

Convert between LocalDate and LocalDateTime

Learn to convert from LocalDate to LocalDateTime and from LocalDateTime to LocalDate in Java 8.

To restate, LocalDate represents a calendar date without time and timezone. LocalDateTime stores the date and time information in the local timeline. It does not have any timezone information.

LocalDateTime = LocalDate + LocalTime

1. LocalDate -> LocalDateTime

To convert a LocalDate instance to LocalDateTime instance, we need to add only the time part in it. For this, we can use either of the given 5 methods of LocalDate class.

  • LocalDateTime atStartOfDay()
  • LocalDateTime atTime(LocalTime time)
  • LocalDateTime atTime(int hour, int minutes)
  • LocalDateTime atTime(int hour, int minutes, int seconds)
  • LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

The method atStartOfDay() returns a LocalDateTime formed from the given date at the time of midnight, 00:00, at the start of the given date.

For all other methods, we provide the specific time in method arguments.

  • 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
  • nanoOfSecond – the nano-of-second to represent, from 0 to 999,999,999

Java program to convert a LocalDate instance to LocalDateTime instance.

LocalDate localDate = LocalDate.parse("2019-01-04"); //Beginning of the day LocalDateTime localDateTime1 = localDate.atStartOfDay(); System.out.println(localDateTime1); //Current time LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now()); System.out.println(localDateTime2); //Specific time LocalDateTime localDateTime3 = localDate.atTime(04, 30, 56); System.out.println(localDateTime3);
2019-01-04T00:00 2019-01-04T18:31:21.936 2019-01-04T04:30:56

2. LocalDateTime -> LocalDate

To convert LocalDateTime to LocalDate instance, use toLocalDate() method. It returns a LocalDate with the same year, month and day as in the original localdatetime object.

LocalDateTime localDateTime = LocalDateTime.now(); LocalDate localDate = localDateTime.toLocalDate(); System.out.println(localDate);

Источник

Java Convert LocalDateTime to LocalTime

In this Java core tutorial we learn how to convert a java.time.LocalDateTime object into a java.time.LocalTime object in Java programming language.

How to convert LocalDateTime to LocalTime in Java

In Java with a given LocalDateTime object we can use the toLocalTime() method to get the time value and return a LocalTime object. In the following Java program we learn how to use the LocalDateTime.toLocalTime() method to convert a LocalDateTime object to LocalTime object.

import java.time.LocalDateTime; import java.time.LocalTime; public class ConvertLocalDateTimeToLocalTimeExample1  public static void main(String. args)  LocalDateTime localDateTime = LocalDateTime.now(); LocalTime LocalTime = localDateTime.toLocalTime(); System.out.println("LocalDateTime: " + localDateTime); System.out.println("LocalTime: " + LocalTime); > >
LocalDateTime: 2022-04-24T11:14:53.504532900 LocalTime: 11:14:53.504532900

Источник

Читайте также:  Box designs in html
Оцените статью