Java tutorial
/* * Copyright 2014 the original author or authors. * * 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 cn.cuizuoli.appranking.util; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.apache.commons.lang.StringUtils; import org.joda.time.DateTime; /** * DateRangeUtil * @author cuizuoli */ public class DateRangeUtil { private final static int MAX_RANGE = 30; /** * getHourListOfDay * @param date * @return */ public static List<String> getHourListOfDay(String date) { List<String> dateHourList = new ArrayList<String>(); DateTime datetime = DateUtil.fromDay(date).withHourOfDay(0); while (StringUtils.equals(DateUtil.toDay(datetime), date)) { dateHourList.add(DateUtil.toHour(datetime)); datetime = datetime.plusHours(1); } return dateHourList; } /** * getDayListOfMonth * @param month * @return */ public static List<String> getDayListOfMonth(String month) { List<String> dateList = new ArrayList<String>(); DateTime datetime = DateUtil.fromMonth(month).withDayOfMonth(1); while (StringUtils.equals(DateUtil.toMonth(datetime), month)) { dateList.add(DateUtil.toDay(datetime)); datetime = datetime.plusDays(1); } return dateList; } /** * getHourList * @param dateHour * @return */ public static List<String> getHourList(String dateHour) { List<String> dateHourList = new ArrayList<String>(); DateTime datetime = DateUtil.fromHour(dateHour); for (int i = 0; i < MAX_RANGE; i++) { dateHourList.add(DateUtil.toHour(datetime)); datetime = datetime.plusHours(1); } return dateHourList; } /** * getDayList * @param day * @return */ public static List<String> getDayList(String day) { List<String> dateList = new ArrayList<String>(); DateTime datetime = DateUtil.fromDay(day); for (int i = 0; i < MAX_RANGE; i++) { dateList.add(DateUtil.toDay(datetime)); datetime = datetime.plusDays(1); } return dateList; } /** * getWeekList * @param day * @return */ public static List<String> getWeekList(String day) { List<String> weekList = new ArrayList<String>(); DateTime datetime = DateUtil.fromDay(day); for (int i = 0; i < MAX_RANGE; i++) { weekList.add(DateUtil.toDay(datetime)); datetime = datetime.plusWeeks(1); } return weekList; } /** * getDateMap * @param dateList * @return */ public static Map<String, Integer> getDateMap(List<String> dateList) { Map<String, Integer> dateMap = new TreeMap<String, Integer>(); int i = 1; for (String date : dateList) { dateMap.put(date, i); i++; } return dateMap; } }