Convert LocalDateTime to LocalTime in Java

This example shows how to convert LocalDateTime to LocalTime instance with an example.

Convert LocalDateTime to LocalDate Example

LocalDateTime class provides below APIs to convert from LocalDateTime to LocalTime in Java.
    LocalTime toLocalTime() - Gets the LocalTime part of this date-time. 
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    
    /**
     * Program to demonstrate conversion from LocalDateTime Class to LocalTime Class.
     * @author javaguides.net
     *
     */
    
    public class LocalDateTimeExample {
    
        public static void main(String[] args) {
             convertLocalDateTimeToLocalTime();
        }
    
        private static void convertLocalDateTimeToLocalTime() {
             LocalDateTime dateTime = LocalDateTime.now();
             System.out.println(dateTime);
    
             LocalTime localDate = dateTime.toLocalTime();
             System.out.println(localDate);
        }
    }

    Output:
    2018-08-10T18:17:08.598
    18:17:08.598
To convert LocalDateTime to LocalTime instance, use toLocalTime() method.


Comments