Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)

package test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DateTest {

public static void main(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"));
}

/***
 * 获取两个时间段的年份/年第一天/年最后一天
 * @param startTime
 * @param endTime
 * @return
 */
public static List<Map> getYears(String startTime, String endTime) {
    List<Map> res=new ArrayList<Map>();
    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<String,Object> map=new HashMap<String,Object>();
            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();
    }
    return res;
}

/***
 * 获取两个时间段的年份-月份/月第一天/月最后一天
 * @param startTime
 * @param endTime
 * @return
 */
public static List<Map> getMonths(String startTime, String endTime) {
    List<Map> res=new ArrayList<Map>();
    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<String,Object> map=new HashMap<String,Object>();
            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();
    }
    return res;
}

/***
 * 获取两个时间段的天数/开始时间/结束时间
 * @param startTime
 * @param endTime
 * @return
 */
public static List<Map> getDays(String startTime, String endTime) {
    // 返回的日期集合
    List<Map> res=new ArrayList<Map>();
    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<String,Object> map=new HashMap<String,Object>();
            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();
    }
    return res;
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值