首页 > 代码库 > java 每个月的第一天和最后一天

java 每个月的第一天和最后一天


private static List<List<String>> get12MonthDate(String year){
String dateStr = "";
if(year!=null&&!year.equals("")){
dateStr = year+"-01-01";
}else{
dateStr=new DateTime(System.currentTimeMillis()).toString("yyyy")+"-01-01";
}
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new DateTime(dateStr).getMillis());
List<List<String>> result = Lists.newArrayList();
for(int i=0;i<12;i++){
result.add(getFirstAndLastDate(c.getTimeInMillis()));
c.add(Calendar.MONTH, 1);
}
return result;
}

/**
* 获取给定时间的每月的第一天和最后一天
* @param date
* @return
*/
private static List<String> getFirstAndLastDate(Long date){
List<String> list = Lists.newArrayList();
Calendar c = Calendar.getInstance();
c.setTimeInMillis(date);
c.add(Calendar.MONTH,0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
list.add(new DateTime(c.getTimeInMillis()).toString("yyyy-MM-dd"));
c.set(Calendar.DAY_OF_MONTH,c.getActualMaximum(Calendar.DAY_OF_MONTH));//当前日期既为本月最后一天
list.add(new DateTime(c.getTimeInMillis()).toString("yyyy-MM-dd")+" 23:59:59");
return list;
}

java 每个月的第一天和最后一天