package com.sangyee.point.common;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class DateTools {
//存储每一天的日期
private List<Date> dayList = new ArrayList<Date>();
public List getDayTime(int beginYears,int beginMonths, int beginDay, int endYears,int endMonths, int endDay)
{
//获取时间对象
Calendar calendar1 =Calendar.getInstance();
//设置开始时间(beginYears/beginMonths/beginDay)
calendar1.set(Calendar.YEAR,beginYears);
calendar1.set(Calendar.MONTH,beginMonths-1); //月份默认从0开始 1月为0
calendar1.set(Calendar.DATE,beginDay-1);
// 设置结束时间(endYears/endMonths/endDay)
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.YEAR, endYears);
calendar2.set(Calendar.MONTH,endMonths-1);
calendar2.set(Calendar.DATE, endDay+1);
//获取时间
Date startDate = calendar1.getTime();
Date endDate = calendar2.getTime();
this.countDays(startDate, endDate);
List<Date> list = this.getDayList();
System.out.println("1:"+list.size());
//格式化时间
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
//
// for (Date date : list) {
// System.out.println(dateFormat.format(date));
//}
return list;
}
/**
public static void main(String[] args) {
DateTools tools = new DateTools();
List<Date> list = tools.getDayTime(2010,7,15,2010,12,15);
System.out.println(list.size());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
for(Date date: list)
{
System.out.println(dateFormat.format(date));
}
}
**/
//返回集合(集合中包含每一天得日期)
public long countDays(Date startDate, Date endDate) {
long startValue = startDate.getTime();
long endValue = endDate.getTime();
if (startValue > endValue) {
long temp = endValue;
endValue = startValue;
startValue = temp;
}
// 计算天数差
long result = (endValue - startValue) / 1000 / 60 / 60 / 24;
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
// 计算期间的每一天
for (int index = 1; index < result; index++) {
cal.add(Calendar.DATE, 1);
dayList.add(cal.getTime());
}
return result;
}
public List<Date> getDayList() {
return dayList;
}
//按周显示。获取当月每周得起止日期
public List<Date> getWeek(int years ,int Months )
{
boolean isWeekBegin=false;
List<Date> dateList = new ArrayList();
SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd ");
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR,years);
c.set(Calendar.MONTH,Months-1);
c.set(Calendar.DAY_OF_MONTH,1);
boolean isHalfWeekAhead=false;
if(c.get(c.DAY_OF_WEEK)!=c.getFirstDayOfWeek()){
isHalfWeekAhead=true;
}
while(c.get(Calendar.MONTH)==Months-1){
if(c.get(Calendar.DAY_OF_WEEK)==c.getFirstDayOfWeek()){
Date start=c.getTime();
c.add(c.DAY_OF_MONTH,6);
Date end=c.getTime();
if(isHalfWeekAhead){
Date fsd=new Date(start.getTime()-7*24L*3600L*1000L);
Date fed=new Date(end.getTime()-7*24L*3600L*1000L);
if(isWeekBegin==false)
{
dateList.add(fsd);
isWeekBegin=true;
}
if(isWeekBegin==true)
{
dateList.add(fed);
}
// System.out.println(sdf.format(fsd)+ "--- "+sdf.format(fed));
isHalfWeekAhead=false;
}
dateList.add(start);
dateList.add(end);
//System.out.println(sdf.format(start)+ "--- "+sdf.format(end));
}
c.add(c.DAY_OF_MONTH,1);
}
return dateList;
}
//按月显示,默认显示一年中12个月,获取每月的第一天和最后一天的日期
public List<Date> TimeMonths(int year) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<Date> datelist = new ArrayList();
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, year);
for (int i = 0; i < 12; i++) {
date.set(Calendar.MONTH, i);
int minMonthDate = date.getActualMinimum(Calendar.DAY_OF_MONTH);
date.set(Calendar.DATE, minMonthDate);
String startMonth = dateFormat.format(date.getTime());
//保存开始时间
datelist.add(date.getTime());
int maxMonthDate = date.getActualMaximum(Calendar.DAY_OF_MONTH);
date.set(Calendar.DATE, maxMonthDate);
//保存结束时间
String endMonth = dateFormat.format(date.getTime());
datelist.add(date.getTime());
}
return datelist;
}
public static void main(String[] args) {
DateTools tools = new DateTools();
List list =tools.getDayTime(2010, 8, 7, 2010, 9, 30);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
if(list!=null)
{
Iterator it = list.iterator();
while(it.hasNext())
{
System.out.println(dateFormat.format(it.next()));
}
}
}
}