com.baidu.rigel.biplatform.ac.util.TimeRangeDetail.java Source code

Java tutorial

Introduction

Here is the source code for com.baidu.rigel.biplatform.ac.util.TimeRangeDetail.java

Source

/**
 * Copyright (c) 2014 Baidu, Inc. All Rights Reserved.
 *
 * 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 com.baidu.rigel.biplatform.ac.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;

/**
 * 2014982014914TimeRangestart20140908end20140914
 * ???
 * 
 *
 * @author david.wang
 * @version 1.0.0.1
 */
public class TimeRangeDetail {

    /**
     * ?
     */
    public static final String FORMAT_STRING = "yyyyMMdd";

    private SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STRING);
    /**
     * 
     */
    private final String start;

    /**
     * 
     */
    private final String end;

    /**
     * 
     * @param start
     * @param end
     * TimeRange
     */
    public TimeRangeDetail(String start, String end) {
        super();
        this.start = start.replace("-", "");
        this.end = end.replace("-", "");
    }

    /**
     * @return the start
     */
    public String getStart() {
        return start;
    }

    /**
     * @return the end
     */
    public String getEnd() {
        return end;
    }

    /**
     * ??
     * @return ??
     * @throws Exception ?
     */
    public Date getStartTime() throws Exception {
        return getTime(start);
    }

    /**
     * ???
     * @return ??
     * @throws Exception ?
     */
    public Date getEndTime() throws Exception {
        return getTime(end);
    }

    /**
     * ??
     * @param timeStr ?yyyyMMdd?
     * @return ??
     * @throws Exception ?
     */
    public static Date getTime(String timeStr) throws Exception {
        SimpleDateFormat format = initDateFormat();
        return format.parse(timeStr);
    }

    /**
     * ?
     * @param date 
     * @return ??
     */
    public static String toTime(Date date) {
        if (date == null) {
            date = new GregorianCalendar().getTime();
        }
        SimpleDateFormat format = initDateFormat();
        return format.format(date);
    }

    /**
     * ???
     * @return 
     */
    public String[] getDays() {

        if (StringUtils.isBlank(this.start)) {
            throw new IllegalArgumentException("start can not be null for timerange");
        }
        if (StringUtils.isBlank(this.end)) {
            throw new IllegalArgumentException("end can not be null for timerange");
        }

        Calendar startCalen = Calendar.getInstance();
        Calendar endCalen = Calendar.getInstance();
        try {
            startCalen.setTime(sdf.parse(start));
            endCalen.setTime(sdf.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> days = Lists.newArrayList();
        do {
            days.add(sdf.format(startCalen.getTime()));
            startCalen.add(Calendar.DAY_OF_MONTH, 1);
        } while (endCalen.equals(startCalen) || endCalen.after(startCalen));

        return days.toArray(new String[0]);
    }

    /**
     * 
     * @return
     * 
     */
    private static SimpleDateFormat initDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat();
        format.applyPattern(FORMAT_STRING);
        return format;
    }

}