package com.changchong.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.junit.Test; /** * 根据一段时间区间,按月份拆分成多个时间段 * @author lxg * * 2016年9月5日下午6:18:36 */ public class SplitDateUtil { @Test public void demo(){ List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2015-08-23","2016-06-10"); System.out.println("开始日期--------------结束日期"); for(KeyValueForDate date : list){ System.out.println(date.getStartDate()+"-----"+date.getEndDate()); } } /** * 根据一段时间区间,按月份拆分成多个时间段 * @param startDate 开始日期 * @param endDate 结束日期 * @return */ @SuppressWarnings("deprecation") public static List<KeyValueForDate> getKeyValueForDate(String startDate,String endDate) { List<KeyValueForDate> list = null; try { list = new ArrayList<KeyValueForDate>(); String firstDay = ""; String lastDay = ""; Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期 Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期 Calendar dd = Calendar.getInstance();// 定义日期实例 dd.setTime(d1);// 设置日期起始时间 Calendar cale = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTime(d2); int startDay = d1.getDate(); int endDay = d2.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); KeyValueForDate keyValueForDate = null; while (dd.getTime().before(d2)) {// 判断是否到结束日期 keyValueForDate = new KeyValueForDate(); cale.setTime(dd.getTime()); if(dd.getTime().equals(d1)){ cale.set(Calendar.DAY_OF_MONTH, dd .getActualMaximum(Calendar.DAY_OF_MONTH)); lastDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(sdf.format(d1)); keyValueForDate.setEndDate(lastDay); }else if(dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){ cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(sdf.format(d2)); }else { cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); cale.set(Calendar.DAY_OF_MONTH, dd .getActualMaximum(Calendar.DAY_OF_MONTH)); lastDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(lastDay); } list.add(keyValueForDate); dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1 } if(endDay<startDay){ keyValueForDate = new KeyValueForDate(); cale.setTime(d2); cale.set(Calendar.DAY_OF_MONTH,1);//取第一天 firstDay = sdf.format(cale.getTime()); keyValueForDate.setStartDate(firstDay); keyValueForDate.setEndDate(sdf.format(d2)); list.add(keyValueForDate); } } catch (ParseException e) { return null; } return list; } } class KeyValueForDate{ private String startDate; private String endDate; public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } }
解决方案 »
- 关于springMVC+freemarker,求指点
- 急急急急急急急急急急急急!!!!请问各位学长,谁有加密与解密的源代码。急用谢谢!!!
- jsp页面判断空值
- weblogic+eclipse出现的问题
- 这么多工作流。用哪个好?
- jasperReport问题?
- 关于tomcat中ThreadPool中一段代码的疑问,恳请大家解答!
- 碰到一个怪怪得问题.
- TOMCAT ECLIPSE JBOSS专家们快来看呀,到底是怎么了。(在线等)
- 只知道类名字符串(不知路径)如何转换为Class 类型
- jar包运行找不到主类 Error: Could not find or load main class xx
- Java中典型的文件操作
看看这个:
public int getCLASSIFY_ID() {
return CLASSIFY_ID;
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Test;/** * 根据一段时间区间,按月份拆分成多个时间段 * @author lxg * * 2016年9月5日下午6:18:36 */
public class SplitDateUtil {
@Test
public void demo() {
List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2015-08-23", "2016-06-10");
System.out.println("开始日期--------------结束日期");
for (KeyValueForDate date : list) {
System.out.println(date.getStartDate() + "-----" + date.getEndDate());
}
}/** * 根据一段时间区间,按月份拆分成多个时间段 * @param startDate 开始日期 * @param endDate 结束日期 * @return */
@SuppressWarnings("deprecation")
public static List<KeyValueForDate> getKeyValueForDate(String startDate,String endDate) {
List<KeyValueForDate> list = null;
try {
list = new ArrayList<KeyValueForDate>();
String firstDay = "";
String lastDay = "";
Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期
Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期
Calendar dd = Calendar.getInstance();// 定义日期实例
dd.setTime(d1);// 设置日期起始时间
Calendar cale = Calendar.getInstance();
Calendar c = Calendar.getInstance();
c.setTime(d2);
int startDay = d1.getDate();
int endDay = d2.getDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
KeyValueForDate keyValueForDate = null;
while (dd.getTime().before(d2)) {// 判断是否到结束日期
keyValueForDate = new KeyValueForDate();
cale.setTime(dd.getTime());
if(dd.getTime().equals(d1)){ c
ale.set(Calendar.DAY_OF_MONTH, dd .getActualMaximum(Calendar.DAY_OF_MONTH));
lastDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(sdf.format(d1));
keyValueForDate.setEndDate(lastDay);
}else if(
dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){
cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(sdf.format(d2));
}else {
cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
cale.set(Calendar.DAY_OF_MONTH,
dd .getActualMaximum(Calendar.DAY_OF_MONTH));
lastDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(lastDay);
}
list.add(keyValueForDate);
dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1
} if(endDay<startDay){
keyValueForDate = new KeyValueForDate();
cale.setTime(d2); cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
firstDay = sdf.format(cale.getTime());
keyValueForDate.setStartDate(firstDay);
keyValueForDate.setEndDate(sdf.format(d2));
list.add(keyValueForDate);
} } catch (ParseException e) {
return null;
}
return list;
}
}class KeyValueForDate{
private String startDate;
private String endDate;
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
} }}}}}}
/**
* 获取开始时间结束时间按照 日期单位 形成多个日期区间
* 第一个区间开始时间为传入开始时间
* 最后一个区间结束时间为传入结束时间
* @param startDate
* @param endDate
* @param unit 支持 ChronoUnit.DAYS,ChronoUnit.WEEKS,ChronoUnit.MONTHS
* @param zoneId 时区
* @return 每一个数组第一个为开始时间,第二个为结束时间;开始时间为当天0.0.0,结束时间为当天23.59.59
*/
public static List<Date[]> rangeDate(Date startDate, Date endDate, ChronoUnit unit,ZoneId zoneId){
List<Date[]> returnList=new ArrayList<>();
ZonedDateTime zdt1= ZonedDateTime.ofInstant(startDate.toInstant(),zoneId);
ZonedDateTime zdt2= ZonedDateTime.ofInstant(endDate.toInstant(),zoneId);
switch (unit){
case DAYS:{
ZonedDateTime start= zdt1.with(ChronoField.SECOND_OF_DAY,0);
ZonedDateTime end= zdt1.with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds()-1);
while(true){
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
start=start.plusDays(1);
end=end.plusDays(1);
}
} break;
}
case WEEKS:{
int dayOfWeek=zdt1.get(ChronoField.DAY_OF_WEEK);
ZonedDateTime start= zdt1.plusDays(1-dayOfWeek).with(ChronoField.SECOND_OF_DAY,0);
ZonedDateTime end= zdt1.plusDays(7-dayOfWeek).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds()-1);
while(true){
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
start=start.plusWeeks(1);
end=end.plusWeeks(1);
}
}
if(!returnList.isEmpty()){
Date[] firstEle=returnList.get(0);
Date[] lastEle=returnList.get(returnList.size()-1);
firstEle[0]=Date.from(zdt1.with(ChronoField.SECOND_OF_DAY,0).toInstant());
lastEle[1]=Date.from(zdt2.with(ChronoField.SECOND_OF_DAY,0).toInstant());
}
break;
}
case MONTHS:{
ZonedDateTime temp=zdt1;
while(true) {
int dayOfMonth = temp.get(ChronoField.DAY_OF_MONTH);
int max = temp.getMonth().maxLength();
ZonedDateTime start = temp.plusDays(1 - dayOfMonth).with(ChronoField.SECOND_OF_DAY, 0);
ZonedDateTime end = temp.plusDays(max - dayOfMonth).with(ChronoField.SECOND_OF_DAY, ChronoUnit.DAYS.getDuration().getSeconds() - 1);
returnList.add(new Date[]{Date.from(start.toInstant()),Date.from(end.toInstant())});
if(!zdt2.isBefore(start)&&!zdt2.isAfter(end)){
break;
}else{
temp=temp.plusMonths(1);
}
}
if(!returnList.isEmpty()){
Date[] firstEle=returnList.get(0);
Date[] lastEle=returnList.get(returnList.size()-1);
firstEle[0]=Date.from(zdt1.with(ChronoField.SECOND_OF_DAY,0).toInstant());
lastEle[1]=Date.from(zdt2.with(ChronoField.SECOND_OF_DAY,0).toInstant());
}
break;
}
default:{
throw BaseRuntimeException.getException("[DateUtil.rangeDate],ChronoUnit["+unit.toString()+"] Not Support!");
}
}
return returnList;
}