Java Date to LocalDateTime toLocalDateTime(Date dateTime)

Here you can find the source of toLocalDateTime(Date dateTime)

Description

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

License

Apache License

Parameter

Parameter Description
dateTime Date to convert (may be null)

Return

Converted , or null if given date was null

Declaration

public static LocalDateTime toLocalDateTime(Date dateTime) 

Method Source Code

//package com.java2s;
/*/*w w w  . j  av  a  2 s. c  o m*/
 * 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.LocalDateTime;

import java.util.Calendar;

import java.util.Date;

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

public class Main {
    /**
     * Convert a {@link Calendar} value into a {@link LocalDateTime}.
     * @param calendar Calendar to convert (may be null)
     * @return Converted {@link LocalDateTime}, or <code>null</code> if given calendar was <code>null</code>
     */
    public static LocalDateTime toLocalDateTime(Calendar calendar) {
        if (calendar != null) {
            return LocalDateTime.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
                    (int) TimeUnit.MILLISECONDS.toNanos(calendar.get(Calendar.MILLISECOND)));
        }
        return null;
    }

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

    /**
     * Convert a {@link Date} value into a {@link LocalDateTime}, using default calendar.
     * @param dateTime Date to convert (may be null)
     * @param timeZone The time zone to use
     * @return Converted {@link LocalDateTime}, or <code>null</code> if given date was <code>null</code>
     */
    public static LocalDateTime toLocalDateTime(Date dateTime, TimeZone timeZone) {
        if (dateTime != null) {
            Calendar c = Calendar.getInstance();
            if (timeZone != null) {
                c.setTimeZone(timeZone);
            }
            c.setTime(dateTime);
            return LocalDateTime.of(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH),
                    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. toLocalDateTime(BigDecimal comTime)
  2. toLocalDateTime(Date date)
  3. toLocalDateTime(Date date)
  4. toLocalDateTime(Date date)
  5. toLocalDateTime(Date utilDate)
  6. toLocalDateTime(long timestamp)
  7. toLocalDateTime(String date, int time)
  8. toLocalDateTime(String dateValue, String timeValue)