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

时间:2025-04-02 15:07:52

package test;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class DateTest {

public static void main(String[] args) {
    (getYears("2016-01-01 00:00:00","2018-07-01 00:00:00"));
    (getMonths("2018-01-01 00:00:00","2018-07-01 00:00:00"));
    (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 = (startTime);
        Date end = (endTime);
        Calendar tempStart = ();
        (start);
        Calendar tempEnd = ();
        (end);
        (,1);// 日期加1(包含结束)
        while ((tempEnd)) {
            String year=(());
            String first=year+"-01-01 00:00:00";
            String last=year+"-12-31 23:59:59";
            Map<String,Object> map=new HashMap<String,Object>();
            ("year", year);
            ("first", (first));
            ("last", (last));
            (map);
            (,1);
        }
    } catch (ParseException e) {
        ();
    }
    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 = (startTime);
        Date end = (endTime);
        Calendar tempStart = ();
        (start);
        Calendar tempEnd = ();
        (end);
        (,1);// 日期加1(包含结束)
        while ((tempEnd)) {
            String month=(());
            (Calendar.DAY_OF_MONTH, 1);
            String first=(());
            (Calendar.DAY_OF_MONTH, (Calendar.DAY_OF_MONTH));  
            Map<String,Object> map=new HashMap<String,Object>();
            ("month", month);
            ("first", (first+" 00:00:00"));
            ("last", (first+" 23:59:59"));
            (map);
            (,1);
        }
    } catch (ParseException e) {
        ();
    }
    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 = (startTime);
        Date end = (endTime);
        Calendar tempStart = ();
        (start);
        Calendar tempEnd = ();
        (end);
        (, +1);// 日期加1(包含结束)
        while ((tempEnd)) {
            String day=(());
            Map<String,Object> map=new HashMap<String,Object>();
            ("day", day);
            ("first", (day+" 00:00:00"));
            ("last", (day+" 23:59:59"));
            (map);
            (Calendar.DAY_OF_YEAR, 1);
        }
    } catch (ParseException e) {
        ();
    }
    return res;
}

}