Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.codelabor.common.calendar.services; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Map; import org.apache.commons.lang.time.DateUtils; import org.codelabor.common.calendar.exceptions.DateOutOfRangeException; import org.codelabor.common.calendar.exceptions.NoSuchDateException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; /** * ? ?, ?? * * <p> * ? , ? ?? . ? holidayMap ?? ?. holidayMap ?? * ? ?? ? . ?, ?? dateRangeFrom, * dateRangeTo ? . ?? ? ?? <a * href="http://www.w3.org/TR/NOTE-datetime">ISO 8601 </a>(YYYY-MM-DD)? . * (java ? yyyy-MM-dd ) * </p> * * @author Shin Sangjae */ public abstract class AbstractCalendarServiceImpl implements CalendarService, InitializingBean { private final Logger logger = LoggerFactory.getLogger(AbstractCalendarServiceImpl.class); /** * ? Properties ? ? */ protected Map<Date, String> holidayMap = null; /** * ?? ? ? ? ? */ protected String formatPattern; /** * formatPattern? ? SimpleDateFormat */ protected SimpleDateFormat dateFormat; /** * ?, ??? ? ?? */ protected Date dateRangeTo = null; /** * ?, ??? ?? */ protected Date dateRangeFrom = null; /** * ?, ??? ?, ? , ?? , , ?, ( ? * 1 ? , 1 ? ?) */ protected int dateRangeByYears = 1; /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getBusinessdayDate * (java.util.Date, int, boolean) */ abstract public Date getBusinessdayDate(Date date, int amount, boolean isBaseDateIncluded) throws ParseException, NoSuchDateException, DateOutOfRangeException; /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getHolidayDate * (java.util.Date, int, boolean) */ abstract public Date getHolidayDate(Date date, int amount, boolean isBaseDateIncluded) throws ParseException, NoSuchDateException, DateOutOfRangeException; /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#isBusinessday( * java.lang.String) */ public boolean isBusinessday(String date) { return !holidayMap.containsKey(date); } /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#isBusinessday( * java.util.Date) */ public boolean isBusinessday(Date date) throws ParseException, DateOutOfRangeException { return !isHoliday(date); } /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#isHoliday(java * .util.Date) */ public boolean isHoliday(Date date) throws ParseException, DateOutOfRangeException { return this.isHoliday(dateFormat.format(date)); } /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#isHoliday(java * .lang.String) */ public boolean isHoliday(String dateString) throws ParseException, DateOutOfRangeException { logger.debug("date: {}", dateString); if (!this.isInRange(dateString)) { throw new DateOutOfRangeException(); } return holidayMap.containsKey(dateString); } /** * ?? ?, ?? ? ?. * * @param date * ? * @return ? */ protected boolean isInRange(Date date) { Assert.notNull(date); return ((date.equals(dateRangeFrom) || date.after(dateRangeFrom)) && (date.equals(dateRangeTo) || date.before(dateRangeTo))); } /** * @param dateString * ? * @return ? * @throws ParseException */ protected boolean isInRange(String dateString) throws ParseException { return isInRange(this.dateFormat.parse(dateString)); } /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#getHolidayDescription * (java.util.Date) */ public String getHolidayDescription(Date date) throws NoSuchDateException, ParseException { return getHolidayDescription(dateFormat.format(date)); } /* * (non-Javadoc) * * @see * org.codelabor.system.calendar.services.CalendarService#getHolidayDescription * (java.lang.String) */ public String getHolidayDescription(String date) throws NoSuchDateException, ParseException { if (holidayMap.containsKey(date)) { return holidayMap.get(date); } else { throw new NoSuchDateException(); } } /** * ? ? ?? . * * @param formatPattern * ? ? ? (<a * href="http://download.oracle.com/javase/1.4.2/docs/api * /java/text/SimpleDateFormat.html">Date and Time Patterns</a>) */ public void setFormatPattern(String formatPattern) { this.formatPattern = formatPattern; } /** * java.util.Date ? ?? . * * @param holidayMap */ public void setHolidayMap(Map<Date, String> holidayMap) { this.holidayMap = holidayMap; } /** * ?, ?? ? ? . * * @param dateRangeTo */ public void setDateRangeTo(Date dateRangeTo) { this.dateRangeTo = dateRangeTo; } /** * ?, ?? ? . * * @param dateRangeFrom */ public void setDateRangeFrom(Date dateRangeFrom) { this.dateRangeFrom = dateRangeFrom; } /** * ?, ?? ? ?, ? , , . ( ? 1 , ) * * @param dateRangeByYears */ public void setDateRangeByYears(int dateRangeByYears) { this.dateRangeByYears = dateRangeByYears; } /* * (non-Javadoc) * * @see * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() { this.dateFormat = new SimpleDateFormat(formatPattern); Date currentDate = Calendar.getInstance().getTime(); if (dateRangeFrom == null) { dateRangeFrom = DateUtils.addYears(currentDate, -dateRangeByYears); } if (dateRangeTo == null) { dateRangeTo = DateUtils.addYears(currentDate, dateRangeByYears); } logger.debug("dateRangeFrom: {}", dateRangeFrom); logger.debug("currentDate: {}", currentDate); logger.debug("dateRangeTo: {}", dateRangeTo); Assert.isTrue(currentDate.after(dateRangeFrom)); Assert.isTrue(currentDate.before(dateRangeTo)); if (logger.isDebugEnabled()) { Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(dateRangeTo); int yearRemains = calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR); int monthRemains = calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH); int dateRemains = calendar2.get(Calendar.DATE) - calendar1.get(Calendar.DATE); logger.debug("dateRangeTo remains: year: {}, month: {}, day: {}", new Object[] { yearRemains, monthRemains, dateRemains }); } Date beforeOneWeekDateRangeTo = DateUtils.addWeeks(dateRangeTo, -1); if (currentDate.after(beforeOneWeekDateRangeTo)) { logger.warn("please renewer date range."); } } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getCurrentDate() */ public Date getCurrentDate() throws Exception { return Calendar.getInstance().getTime(); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextBusinessdayDate * (java.util.Date) */ public Date getNextBusinessdayDate(Date date) throws Exception { return this.getBusinessdayDate(date, 1); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextHolidayDate * (java.util.Date) */ public Date getNextHolidayDate(Date date) throws Exception { return this.getHolidayDate(date, 1); } /* * (non-Javadoc) * * @seeorg.codelabor.common.calendar.services.CalendarService# * getPreviousBusinessDate(java.util.Date) */ public Date getPreviousBusinessDate(Date date) throws Exception { return this.getBusinessdayDate(date, -1); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getPreviousHolidayDate * (java.util.Date) */ public Date getPreviousHolidayDate(Date date) throws Exception { return this.getHolidayDate(date, -1); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextBusinessdayDate * () */ public Date getNextBusinessdayDate() throws Exception { return this.getNextBusinessdayDate(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextHolidayDate * () */ public Date getNextHolidayDate() throws Exception { return this.getNextHolidayDate(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @seeorg.codelabor.common.calendar.services.CalendarService# * getPreviousBusinessDate() */ public Date getPreviousBusinessDate() throws Exception { return this.getPreviousBusinessDate(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getPreviousHolidayDate * () */ public Date getPreviousHolidayDate() throws Exception { return this.getPreviousHolidayDate(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#isBusinessday() */ public boolean isBusinessday() throws Exception { return this.isBusinessday(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @see org.codelabor.common.calendar.services.CalendarService#isHoliday() */ public boolean isHoliday() throws Exception { return this.isHoliday(Calendar.getInstance().getTime()); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getBusinessdayDate * (java.util.Date, int) */ public Date getBusinessdayDate(Date date, int amount) throws Exception { return this.getBusinessdayDate(date, amount, false); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getHolidayDate * (java.util.Date, int) */ public Date getHolidayDate(Date date, int amount) throws Exception { return this.getHolidayDate(date, amount, false); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextBusinessdayDate * (boolean) */ public Date getNextBusinessdayDate(boolean isBaseDateInclude) throws Exception { return this.getPreviousBusinessDate(Calendar.getInstance().getTime(), isBaseDateInclude); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextBusinessdayDate * (java.util.Date, boolean) */ public Date getNextBusinessdayDate(Date date, boolean isBaseDateIncluded) throws Exception { return this.getBusinessdayDate(date, 1, isBaseDateIncluded); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextHolidayDate * (boolean) */ public Date getNextHolidayDate(boolean isBaseDateIncluded) throws Exception { return this.getNextHolidayDate(Calendar.getInstance().getTime(), isBaseDateIncluded); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getNextHolidayDate * (java.util.Date, boolean) */ public Date getNextHolidayDate(Date date, boolean isBaseDateIncluded) throws Exception { return this.getHolidayDate(date, 1, isBaseDateIncluded); } public Date getPreviousBusinessDate(boolean isBaseDateIncluded) throws Exception { return this.getPreviousBusinessDate(Calendar.getInstance().getTime(), isBaseDateIncluded); } /* * (non-Javadoc) * * @seeorg.codelabor.common.calendar.services.CalendarService# * getPreviousBusinessDate(java.util.Date, boolean) */ public Date getPreviousBusinessDate(Date date, boolean isBaseDateIncluded) throws Exception { return this.getBusinessdayDate(date, -1, isBaseDateIncluded); } public Date getPreviousHolidayDate(boolean isBaseDateIncluded) throws Exception { return this.getPreviousHolidayDate(Calendar.getInstance().getTime(), isBaseDateIncluded); } /* * (non-Javadoc) * * @see * org.codelabor.common.calendar.services.CalendarService#getPreviousHolidayDate * (java.util.Date, boolean) */ public Date getPreviousHolidayDate(Date date, boolean isBaseDateIncluded) throws Exception { return this.getHolidayDate(date, -1, isBaseDateIncluded); } }