jp.co.nemuzuka.utils.DateTimeChecker.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.utils.DateTimeChecker.java

Source

/*
 * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka)
 *
 * 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 jp.co.nemuzuka.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * ?.
 * @author k-katagiri
 */
public class DateTimeChecker {

    /**
     * ??????.
     * yyyymmdd???????
     * @param target 
     * @return true:????? false:?????
     */
    public static boolean isDay(String target) {
        return executeCheck(target, "yyyyMMdd");
    }

    /**
     * ??????.
     * yyyymm???????
     * @param target 
     * @return true:????? false:?????
     */
    public static boolean isMonth(String target) {
        return executeCheck(target, "yyyyMM");
    }

    /**
     * ??????.
     * hhmm??????
     * @param target 
     * @return ???????true
     */
    public static boolean isHourMinute(String target) {
        return executeCheck(target, "HHmm");
    }

    /**
     * ??.
     * ??????????
     * @param refreshStartTime 
     * @return ????true
     */
    public static boolean isOverRefreshStartTime(Date refreshStartTime) {

        if (refreshStartTime == null) {
            return true;
        }

        long cuurentTime = CurrentDateUtils.getInstance().getCurrentDateTime().getTime();
        long targetTime = refreshStartTime.getTime();
        if (cuurentTime > targetTime) {
            return true;
        }
        return false;
    }

    /**
     * ?.
     * @param target 
     * @param patterns ?
     * @return true:?? false:?????
     */
    private static boolean executeCheck(String target, String pattern) {
        if (StringUtils.isEmpty(target)) {
            return false;
        }

        if (target.length() != pattern.length()) {
            return false;
        }

        try {
            Integer.valueOf(target);
        } catch (NumberFormatException e) {
            return false;
        }

        SimpleDateFormat format = DateTimeUtils.createSdf(pattern);
        format.setLenient(false);
        try {
            format.parse(target);
        } catch (ParseException e) {
            return false;
        }

        return true;
    }

}