org.flowable.dmn.engine.impl.el.util.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.flowable.dmn.engine.impl.el.util.DateUtil.java

Source

/* 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.
 */
package org.flowable.dmn.engine.impl.el.util;

import java.util.Date;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * @author Yvo Swillens
 */
public class DateUtil {

    public static Date toDate(Object dateString) {

        if (dateString == null) {
            throw new IllegalArgumentException("date string cannot be empty");
        }

        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
        LocalDate dateTime = dtf.parseLocalDate((String) dateString);

        return dateTime.toDate();
    }

    public static Date addDate(Object startDate, Object years, Object months, Object days) {

        LocalDate currentDate = new LocalDate(startDate);

        currentDate = currentDate.plusYears(intValue(years));
        currentDate = currentDate.plusMonths(intValue(months));
        currentDate = currentDate.plusDays(intValue(days));

        return currentDate.toDate();
    }

    public static Date subtractDate(Object startDate, Object years, Object months, Object days) {

        LocalDate currentDate = new LocalDate(startDate);

        currentDate = currentDate.minusYears(intValue(years));
        currentDate = currentDate.minusMonths(intValue(months));
        currentDate = currentDate.minusDays(intValue(days));

        return currentDate.toDate();
    }

    public static Date now() {

        return new LocalDate().toDate();
    }

    protected static Integer intValue(Object value) {
        Integer intValue = null;
        if (value instanceof Integer) {
            intValue = (Integer) value;
        } else {
            intValue = Integer.valueOf(value.toString());
        }

        return intValue;
    }
}