package com.jason.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
public class DateUtil {
/**
* 获取本月第一天和最后一天日期
* @return MAP{beginTime:本月的第一天}{endTime:本月的最后一天}
*/
public static Map<String, String> findThisMonth(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat datef=new SimpleDateFormat("yyyy-MM-dd");
//获取本月的第一天
cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
Date beginTime1=cal.getTime();
String beginTime=datef.format(beginTime1);
//获取本月的最后一天
cal.set( Calendar.DATE, 1 );
cal.roll(Calendar.DATE, - 1 );
Date endTime1=cal.getTime();
String endTime=datef.format(endTime1);
Map<String, String> map = new HashMap<String, String>();
map.put("beginTime", beginTime);
map.put("endTime", endTime);
return map;
}
/**
* 获取上个月的第一天和最后一天带时间
*
* @return MAP{prevMonthFD:当前日期上个月的第一天}{prevMonthPD:当前日期上个月的最后一天}
*/
@SuppressWarnings("static-access")
public static Map<String, String> findLastMonth() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -1);
Date theDate = calendar.getTime();
gcLast.setTime(theDate);
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String day_first_prevM = df.format(gcLast.getTime());
//带时间
// StringBuffer str = new StringBuffer().append(day_first_prevM).append(
// " 00:00:00");
// day_first_prevM = str.toString();
calendar.add(cal.MONTH, 1);
calendar.set(cal.DATE, 1);
calendar.add(cal.DATE, -1);
String day_end_prevM = df.format(calendar.getTime());
//带时间
// StringBuffer endStr = new StringBuffer().append(day_end_prevM).append(
// " 23:59:59");
// day_end_prevM = endStr.toString();
Map<String, String> map = new HashMap<String, String>();
map.put("prevMonthFD", day_first_prevM);
map.put("prevMonthPD", day_end_prevM);
return map;
}
/**
* 获取三个月前的日期和当天日期
* @return MAP{nowDate:当前日期}{threeMonthsAgoDate:3个月前的 日期}
*/
public static Map<String, String> findThreeMonthsAgo() {
// 取当前日期
Date date = new Date();
// 实例化日历类
Calendar cal = Calendar.getInstance();
// 设置日期
cal.setTime(date);
// 取3个月前的 日期
cal.add(Calendar.MONTH,-3);
Date threeMonthAgoDate1 = cal.getTime();
// 以下是Date类型转换String类型
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String nowDate=dateFormat.format(date);
String threeMonthsAgoDate = dateFormat.format(threeMonthAgoDate1);
Map<String, String> map = new HashMap<String, String>();
map.put("nowDate", nowDate);
map.put("threeMonthsAgoDate", threeMonthsAgoDate);
return map;
}
public static void main(String[] args) {
Map<String, String> map =findThisMonth();
System.out.println("本月第一天日期:" + map.get("beginTime"));
System.out.println("本月最后一天日期:" + map.get("endTime"));
Map<String, String> map1=findLastMonth();
System.out.println("上月第一天日期:" + map1.get("prevMonthFD"));
System.out.println("上月最后一天日期:" + map1.get("prevMonthPD"));
Map<String, String> map2=findThreeMonthsAgo();
System.out.println("当天日期:" + map2.get("nowDate"));
System.out.println("三个月前日期:" + map2.get("threeMonthsAgoDate"));
}
}