Java LocalDate Create toLocalDate(Calendar calendar)

Here you can find the source of toLocalDate(Calendar calendar)

Description

Convert a Calendar value into a LocalDate .

License

Apache License

Parameter

Parameter Description
calendar Calendar to convert (may be null)

Return

Converted , or null if given calendar was null

Declaration

public static LocalDate toLocalDate(Calendar calendar) 

Method Source Code

//package com.java2s;
/*/*from w ww.j  a  v  a2s . co 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.LocalDate;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Convert a {@link Calendar} value into a {@link LocalDate}.
     * @param calendar Calendar to convert (may be null)
     * @return Converted {@link LocalDate}, or <code>null</code> if given calendar was <code>null</code>
     */
    public static LocalDate toLocalDate(Calendar calendar) {
        if (calendar != null) {
            return LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

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

    /**
     * Convert a {@link Date} value into a {@link LocalDate}, using default calendar.
     * @param date Date to convert (may be null)
     * @param timeZone The time zone to use
     * @return Converted {@link LocalDate}, or <code>null</code> if given date was <code>null</code>
     */
    public static LocalDate toLocalDate(Date date, TimeZone timeZone) {
        if (date != null) {
            final Calendar c = Calendar.getInstance();
            if (timeZone != null) {
                c.setTimeZone(timeZone);
            }
            c.setTime(date);
            return LocalDate.of(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }
}

Related

  1. getCurrentLocalDate()
  2. getCurrentLocalDate()
  3. getLocalDate(int year, int atDay)
  4. getLocalDate(java.util.Date date)
  5. getLocalDate(long timeInMillis)
  6. toLocalDate(final Date date)