切割时间工具类

说明:这是一个关于将两个时间段的按照月进行切分,和将两个日期段分成每一天进行操作,这个经常被使用在实际项目中的统计报表中。此处有需要的,可以借鉴。

下面说一下这个类的全部代码:

package com.ly.util;

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.List;

/**
 * 日期分割类
 * 
 * @author ljl 日期:2017-6-27
 */
public class SplitDateUtil {
	/**
	 * 根据一段时间区间,按月份拆分成多个时间段
	 * 
	 * @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;
	}
	
	public static void main(String[] args) {
		/*List<KeyValueForDate> lst = SplitDateUtil.getKeyValueForDate("2017-06-01", "2017-06-27");
		for (KeyValueForDate keyValueForDate : lst) {
			System.out.println(keyValueForDate.getStartDate() + "------>" + keyValueForDate.getEndDate());
		}*/
		/* Calendar start = Calendar.getInstance();  
		    start.set(2014, 6, 11);  
		    Long startTIme = start.getTimeInMillis();  
		  
		    Calendar end = Calendar.getInstance();  
		    end.set(2014, 6, 25);  
		    Long endTime = end.getTimeInMillis();  
		  
		    Long oneDay = 1000 * 60 * 60 * 24l;  
		  
		    Long time = startTIme;  
		    while (time <= endTime) {  
		        Date d = new Date(time);  
		        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
		        System.out.println(df.format(d)); 
		        System.out.println(df.format(d.getTime() + new Date(oneDay).getTime())); 
		        System.out.println("---------------------------");
		        time += oneDay;  
		    } */ 
		//将一个时间段分成每一天进行处理
		    
		    try {
				Calendar start = Calendar.getInstance();  
				start.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2017-06-01"));
				Long startTIme = start.getTimeInMillis();  
  
				Calendar end = Calendar.getInstance();  
				end.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2017-06-27"));
				Long endTime = end.getTimeInMillis();  
  
				Long oneDay = 1000 * 60 * 60 * 24l;  
  
				Long time = startTIme;  
				while (time <= endTime) {  
				    Date d = new Date(time);  
				    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
				    System.out.println(df.format(d)); 
				    System.out.println(df.format(d.getTime() + new Date(oneDay).getTime()));
				    System.out.println("---------------------------");
				    time += oneDay;  
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
}

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;
	}

}

 允许效果就不截图了,自己拿过去一试,就知道干啥了!

posted @ 2017-06-27 23:33  懒得烧蛇吃  阅读(393)  评论(0编辑  收藏  举报