Java OffsetDateTime toCalendar(OffsetDateTime offsetDateTime)

Here you can find the source of toCalendar(OffsetDateTime offsetDateTime)

Description

Convert the given OffsetDateTime instance to Calendar instance with the same time and time zone

License

LGPL

Parameter

Parameter Description
offsetDateTime the date time to convert

Return

the converted calendar instance

Declaration

public static Calendar toCalendar(OffsetDateTime offsetDateTime) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.time.OffsetDateTime;

import java.time.ZonedDateTime;
import java.util.Calendar;

import java.util.Objects;
import java.util.TimeZone;

public class Main {
    /**// w ww.j  a va 2s.c om
     * Convert the given {@link OffsetDateTime} instance to {@link Calendar} instance with the same time and time zone
     *
     * @param offsetDateTime
     *            the date time to convert
     * @return the converted calendar instance
     */
    public static Calendar toCalendar(OffsetDateTime offsetDateTime) {
        Objects.requireNonNull(offsetDateTime, "Offset date time is required");

        TimeZone timeZone = TimeZone.getTimeZone(offsetDateTime.toZonedDateTime().getZone());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(offsetDateTime.toInstant().toEpochMilli());
        calendar.setTimeZone(timeZone);
        return calendar;
    }

    /**
     * Convert the given {@link ZonedDateTime} instance to {@link Calendar} instance with the same time and time zone
     *
     * @param zonedDateTime
     *            the date time to convert
     * @return the converted calendar instance
     */
    public static Calendar toCalendar(ZonedDateTime zonedDateTime) {
        Objects.requireNonNull(zonedDateTime, "Zoned date time is required");

        TimeZone timeZone = TimeZone.getTimeZone(zonedDateTime.getZone());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(zonedDateTime.toInstant().toEpochMilli());
        calendar.setTimeZone(timeZone);
        return calendar;
    }
}

Related

  1. next(OffsetDateTime date, OffsetTime time)
  2. nowAtZone(String offsetId)
  3. parseDate(String raw)
  4. stringifyDate(long timestamp)
  5. timeF(OffsetDateTime time, ZoneId zone)
  6. toRfc1123String(FileTime time)