Java LocalTime toLocalTime(Date date)

Here you can find the source of toLocalTime(Date date)

Description

Convert a Date value into a LocalTime , using default calendar and default time zone.

License

Apache License

Parameter

Parameter Description
date Date to convert (may be null)

Return

Converted , or null if given date was null

Declaration

public static LocalTime toLocalTime(Date date) 

Method Source Code

//package com.java2s;
/*//www  .j  a v a 2s .c om
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.time.LocalTime;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Convert a {@link Date} value into a {@link LocalTime}, using default calendar and default time zone.
     * @param date Date to convert (may be null)
     * @return Converted {@link LocalTime}, or <code>null</code> if given date was <code>null</code>
     */
    public static LocalTime toLocalTime(Date date) {
        return toLocalTime(date, null);
    }

    /**
     * Convert a {@link Date} value into a {@link LocalTime}, using default calendar.
     * @param date Date to convert (may be null)
     * @param timeZone The time zone to use
     * @return Converted {@link LocalTime}, or <code>null</code> if given date was <code>null</code>
     */
    public static LocalTime toLocalTime(Date date, TimeZone timeZone) {
        if (date != null) {
            final Calendar c = Calendar.getInstance();
            if (timeZone != null) {
                c.setTimeZone(timeZone);
            }
            c.setTime(date);
            return LocalTime.of(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND),
                    (int) TimeUnit.MILLISECONDS.toNanos(c.get(Calendar.MILLISECOND)));
        }
        return null;
    }
}

Related

  1. isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime)
  2. isLocalTimeInRange(LocalTime value, LocalTime optionalMinimum, LocalTime optionalMaximum, boolean inclusiveOfEndpoints)
  3. parseLocalTime(String string)
  4. repDiff(LocalTime start, LocalTime end, byte type)
  5. reportTimeDiff(LocalTime start, LocalTime end)