jp.co.nemuzuka.service.impl.CalendarServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.service.impl.CalendarServiceImpl.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.service.impl;

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

import jp.co.nemuzuka.service.CalendarService;
import jp.co.nemuzuka.utils.CurrentDateUtils;
import jp.co.nemuzuka.utils.DateTimeUtils;

import org.apache.commons.lang.StringUtils;

/**
 * CalendarService?.
 * @author kazumune
 */
public class CalendarServiceImpl implements CalendarService {

    private static CalendarServiceImpl impl = new CalendarServiceImpl();

    /**
     * ?.
     * @return 
     */
    public static CalendarServiceImpl getInstance() {
        return impl;
    }

    /**
     * .
     */
    private CalendarServiceImpl() {
    }

    /* (non-Javadoc)
     * @see jp.co.nemuzuka.service.CalendarService#getViewDates(java.lang.String, java.lang.String)
     */
    @Override
    public Result getViewDates(String targetYyyyMM, String type) {

        String target = getYyyyMM(targetYyyyMM);

        if ("target".equals(type)) {
            //??????
        } else if ("next".equals(type)) {
            //?
            target = DateTimeUtils.addMonth(targetYyyyMM, 1);

        } else if ("prev".equals(type)) {
            //??
            target = DateTimeUtils.addMonth(targetYyyyMM, -1);

        } else {
            throw new IllegalArgumentException("type error.");
        }

        //?????
        return createResult(target);
    }

    /**
     * ?.
     * @param target (yyyyMM?)
     * @return 
     */
    private Result createResult(String target) {

        Result result = new Result();
        result.targetYyyyMM = target;
        SimpleDateFormat sdf = DateTimeUtils.createSdf("d");
        List<Date> targetDates = DateTimeUtils.getStartEndDate4SunDayList(target);
        result.viewDates = new ArrayList<String>();
        for (Date targetDate : targetDates) {
            result.viewDates.add(sdf.format(targetDate));
        }

        //?
        sdf = DateTimeUtils.createSdf("yyyyMMdd");
        result.currentDate = sdf.format(CurrentDateUtils.getInstance().getCurrentDate());
        return result;
    }

    /**
     * ??.
     * ?????????????
     * ??????????????????
     * @param targetYyyyMM ?
     * @return ?(yyyyMM?)
     */
    private String getYyyyMM(String targetYyyyMM) {

        if (StringUtils.isNotEmpty(targetYyyyMM)) {
            //????
            SimpleDateFormat sdf = DateTimeUtils.createSdf("yyyyMM");
            sdf.setLenient(false);
            try {
                sdf.parse(targetYyyyMM);
                //????????
                return targetYyyyMM;
            } catch (ParseException e) {
            }
        }

        //??
        return DateTimeUtils.getMonth();
    }
}