Java ZonedDateTime Calculate addOffset(ZonedDateTime time, String offset)

Here you can find the source of addOffset(ZonedDateTime time, String offset)

Description

Applies an ISO 8601 Duration to a ZonedDateTime .

License

Apache License

Parameter

Parameter Description
time A zoned date time to apply the offset to
offset The offset in ISO 8601 Duration format

Return

A zoned date time with the offset applied

Declaration

public static ZonedDateTime addOffset(ZonedDateTime time, String offset) 

Method Source Code


//package com.java2s;
/*-//  w ww .j a  v a  2s. c  om
 * -\-\-
 * Spotify Styx Common
 * --
 * Copyright (C) 2016 - 2017 Spotify AB
 * --
 * 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.Duration;

import java.time.Period;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAmount;

public class Main {
    /**
     * Applies an ISO 8601 Duration to a {@link ZonedDateTime}.
     *
     * <p>Since the JDK defined different types for the different parts of a Duration
     * specification, this utility method is needed when a full Duration is to be applied to a
     * {@link ZonedDateTime}. See {@link Period} and {@link Duration}.
     *
     * <p>All date-based parts of a Duration specification (Year, Month, Day or Week) are parsed
     * using {@link Period#parse(CharSequence)} and added to the time. The remaining parts (Hour,
     * Minute, Second) are parsed using {@link Duration#parse(CharSequence)} and added to the time.
     *
     * @param time   A zoned date time to apply the offset to
     * @param offset The offset in ISO 8601 Duration format
     * @return A zoned date time with the offset applied
     */
    public static ZonedDateTime addOffset(ZonedDateTime time, String offset) {
        final int tPos = offset.indexOf('T');
        final String periodOffset;
        final String durationOffset;

        switch (tPos) {
        case -1:
            periodOffset = offset;
            break;

        case 1:
            periodOffset = "P0D";
            break;

        default:
            periodOffset = offset.substring(0, tPos);
        }

        if (tPos == -1) {
            durationOffset = "PT0S";
        } else {
            durationOffset = "P" + offset.substring(tPos);
        }

        final TemporalAmount dateAmount = Period.parse(periodOffset);
        final TemporalAmount timeAmount = Duration.parse(durationOffset);

        return time.plus(dateAmount).plus(timeAmount);
    }
}

Related

  1. addRandomDeltaDays(ZonedDateTime ZDT, int minDays, int maxDays, int minHour, int maxHour)
  2. addRandomDeltaMinutes(ZonedDateTime ZDT, int minMinutes, int maxMinutes)
  3. asZonedDateTime(Date date)
  4. calculateTimeForSunrise(ZonedDateTime from, ZonedDateTime to)