Java ZonedDateTime Calculate preProcess(String Val, ZonedDateTime BaseTimeMarker)

Here you can find the source of preProcess(String Val, ZonedDateTime BaseTimeMarker)

Description

If the string passed in is not null and of exactly 16 characters in the form of "(+|-)(\d{2})y(\d{2})m(\d{2})d\s([01][0-9]|2[0-4])\:([0-5][0-9])", will return a JSON text representation of the baseTimeMarker with the delta specified by the pattern.

License

Apache License

Parameter

Parameter Description
Val a parameter
baseTimeMarker a parameter

Declaration

public static String preProcess(String Val, ZonedDateTime BaseTimeMarker) 

Method Source Code


//package com.java2s;
/* ===========================================================================
 * Copyright (C) 2015 CapsicoHealth Inc.
 *
 * 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./*from  w  ww .j  a  v  a  2  s .c o m*/
 */

import java.time.Period;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static Pattern _DELTA_DATETIME = Pattern
            .compile("(\\+|\\-)(\\d{2})y(\\d{2})m(\\d{2})d\\s([01][0-9]|2[0-4])\\:([0-5][0-9])");

    /**
     * If the string passed in is not null and of exactly 16 characters in the form of "(+|-)(\d{2})y(\d{2})m(\d{2})d\s([01][0-9]|2[0-4])\:([0-5][0-9])",
     * will return a JSON text representation of the baseTimeMarker with the delta specified by the pattern. For example if the date time passed is
     * <B>2015-02-10</B> (irrespective of the time specified)<BR>
     * <UL><LI>"-01y00m05d 10:50" -> 2014-02-05 10:50:00</LI>
     *     <LI>"+00y00m04d 14:50" -> 2015-02-14 14:50:00</LI>
     * </UL>
     * @param Val
     * @param baseTimeMarker
     * @return
     */
    public static String preProcess(String Val, ZonedDateTime BaseTimeMarker) {
        if (Val == null || Val.length() != 16)
            return Val;

        Matcher M = _DELTA_DATETIME.matcher(Val);
        if (M.matches() == false)
            return Val;

        boolean Plus = M.group(1).equals("+");
        int years = Integer.parseInt(M.group(2));
        int months = Integer.parseInt(M.group(3));
        int days = Integer.parseInt(M.group(4));
        int hour = Integer.parseInt(M.group(5));
        int minute = Integer.parseInt(M.group(6));
        Period P = Period.of(years, months, days);
        BaseTimeMarker = Plus ? BaseTimeMarker.plus(P) : BaseTimeMarker.minus(P);
        BaseTimeMarker = BaseTimeMarker.withHour(hour).withMinute(minute);

        return printDateTimeForJSON(BaseTimeMarker);
    }

    public static String printDateTimeForJSON(ZonedDateTime ZDT) {
        return ZDT == null ? null : ZDT.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
    }
}

Related

  1. mapToZonedDateTime(final Date date)
  2. max(ZonedDateTime a, ZonedDateTime b)
  3. oneDayLater(ZonedDateTime baseDate)
  4. parseZdtToDate(ZonedDateTime zdt)
  5. parseZonedDateTime(String zonedDateTimeString, String variableName)
  6. printDateTimeCompact(ZonedDateTime ZDT, boolean PrintTime, boolean PrintSeconds)
  7. printDateTimeSuperCompact(ZonedDateTime ZDT)
  8. roundTimeMinQuarter(ZonedDateTime dateTime)
  9. roundUp(ZonedDateTime date)