java中获取特定时间段_Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)...

packagetest;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.TreeMap;public classDateTest {public static voidmain(String[] args) {

System.out.println(getYears("2016-01-01 00:00:00","2018-07-01 00:00:00"));

System.out.println(getMonths("2018-01-01 00:00:00","2018-07-01 00:00:00"));

System.out.println(getDays("2018-06-01 00:00:00","2018-07-01 00:00:00"));

}/***

* 获取两个时间段的年份/年第一天/年最后一天

*@paramstartTime

*@paramendTime

*@return

*/

public static ListgetYears(String startTime, String endTime) {

List res=new ArrayList();

DateFormat dateFormat= new SimpleDateFormat("yyyy");

DateFormat dateFormat2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date start=dateFormat.parse(startTime);

Date end=dateFormat.parse(endTime);

Calendar tempStart=Calendar.getInstance();

tempStart.setTime(start);

Calendar tempEnd=Calendar.getInstance();

tempEnd.setTime(end);

tempEnd.add(Calendar.YEAR,1);//日期加1(包含结束)

while(tempStart.before(tempEnd)) {

String year=dateFormat.format(tempStart.getTime());

String first=year+"-01-01 00:00:00";

String last=year+"-12-31 23:59:59";

Map map=new HashMap();

map.put("year", year);

map.put("first", dateFormat2.parse(first));

map.put("last", dateFormat2.parse(last));

res.add(map);

tempStart.add(Calendar.YEAR,1);

}

}catch(ParseException e) {

e.printStackTrace();

}returnres;

}/***

* 获取两个时间段的年份-月份/月第一天/月最后一天

*@paramstartTime

*@paramendTime

*@return

*/

public static ListgetMonths(String startTime, String endTime) {

List res=new ArrayList();

DateFormat dateFormat= new SimpleDateFormat("yyyy-MM");

DateFormat dateFormat2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

DateFormat dateFormat3= new SimpleDateFormat("yyyy-MM-dd");try{

Date start=dateFormat.parse(startTime);

Date end=dateFormat.parse(endTime);

Calendar tempStart=Calendar.getInstance();

tempStart.setTime(start);

Calendar tempEnd=Calendar.getInstance();

tempEnd.setTime(end);

tempEnd.add(Calendar.MONTH,1);//日期加1(包含结束)

while(tempStart.before(tempEnd)) {

String month=dateFormat.format(tempStart.getTime());

tempStart.set(Calendar.DAY_OF_MONTH,1);

String first=dateFormat3.format(tempStart.getTime());

tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH));

Map map=new HashMap();

map.put("month", month);

map.put("first", dateFormat2.parse(first+" 00:00:00"));

map.put("last", dateFormat2.parse(first+" 23:59:59"));

res.add(map);

tempStart.add(Calendar.MONTH,1);

}

}catch(ParseException e) {

e.printStackTrace();

}returnres;

}/***

* 获取两个时间段的天数/开始时间/结束时间

*@paramstartTime

*@paramendTime

*@return

*/

public static ListgetDays(String startTime, String endTime) {//返回的日期集合

List res=new ArrayList();

DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");

DateFormat dateFormat2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date start=dateFormat.parse(startTime);

Date end=dateFormat.parse(endTime);

Calendar tempStart=Calendar.getInstance();

tempStart.setTime(start);

Calendar tempEnd=Calendar.getInstance();

tempEnd.setTime(end);

tempEnd.add(Calendar.DATE,+1);//日期加1(包含结束)

while(tempStart.before(tempEnd)) {

String day=dateFormat.format(tempStart.getTime());

Map map=new HashMap();

map.put("day", day);

map.put("first", dateFormat2.parse(day+" 00:00:00"));

map.put("last", dateFormat2.parse(day+" 23:59:59"));

res.add(map);

tempStart.add(Calendar.DAY_OF_YEAR,1);

}

}catch(ParseException e) {

e.printStackTrace();

}returnres;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值