Java LocalDateTime Calculate relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add)

Here you can find the source of relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add)

Description

relative Date Time

License

Apache License

Declaration

static LocalDateTime relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add) 

Method Source Code

//package com.java2s;
/*// w w  w  . ja v  a  2  s  . c om
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.StringTokenizer;

public class Main {
    static LocalDateTime relativeDateTime(final LocalDateTime contextDate, final String str, final boolean add) {
        LocalDateTime relativeDate = contextDate;
        if (str.equals("")) {
            return contextDate;
        }

        try {
            final StringTokenizer st = new StringTokenizer(str.substring(1), " ");
            while (st.hasMoreTokens()) {
                final String token = st.nextToken();
                relativeDate = adjustDateTime(relativeDate, token, add);
            }
            return relativeDate;
        } catch (final Exception e) {
            return contextDate;
        }
    }

    private static LocalDateTime adjustDateTime(final LocalDateTime contextDateTime, String str,
            final boolean add) {
        int hours = 0;
        int minutes = 0;
        int days = 0;
        int months = 0;
        int years = 0;

        if (str.endsWith("H")) {
            str = str.substring(0, str.length() - 1);
            hours = Integer.valueOf(str).intValue();
        } else if (str.endsWith("M")) {
            str = str.substring(0, str.length() - 1);
            minutes = Integer.valueOf(str).intValue();
        } else if (str.endsWith("w")) {
            str = str.substring(0, str.length() - 1);
            days = 7 * Integer.valueOf(str).intValue();
        } else if (str.endsWith("y")) {
            str = str.substring(0, str.length() - 1);
            years = Integer.valueOf(str).intValue();
        } else if (str.endsWith("m")) {
            str = str.substring(0, str.length() - 1);
            months = Integer.valueOf(str).intValue();
        } else if (str.endsWith("d")) {
            str = str.substring(0, str.length() - 1);
            days = Integer.valueOf(str).intValue();
        } else {
            days = Integer.valueOf(str).intValue();
        }

        if (add) {
            return add(contextDateTime, years, months, days, hours, minutes);
        } else {
            return add(contextDateTime, -years, -months, -days, -hours, -minutes);
        }
    }

    private static LocalDateTime add(final LocalDateTime original, final int years, final int months,
            final int days, final int hours, final int minutes) {
        return original.plusYears(years).plusMonths(months).plusDays(days).plusHours(hours).plusMinutes(minutes);
    }
}

Related

  1. ms2LocalDateTime(final long ms)
  2. oldDateToISO8601LocalDateTime(Date nextColumnDate)
  3. parseStringToLocalDateTimeWithSpecifiedTime(String strDate, String time)
  4. parseTimeStamp(LocalDateTime actualDate, LocalDateTime checkedDate, boolean isDateFrom)
  5. printDateTime(LocalDateTime dateTime)
  6. safeCreateFromValue(LocalDateTime datetime, int year, int month, int day, int hour, int minute, int second)
  7. secondsBetween(LocalDateTime date1, LocalDateTime date2)
  8. setTime(LocalDateTime source, String time)
  9. shiftDateTime(String timex, LocalDateTime reference, boolean future)