Here you can find the source of toLocalDateTime(Date dateTime)
Parameter | Description |
---|---|
dateTime | Date to convert (may be null) |
null
if given date was null
public static LocalDateTime toLocalDateTime(Date dateTime)
//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; } }