import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
/**
* 获取时间段
*
* 传入枚举DayType类型:
* thisMonth本月, lastMonth上月, lastThreeMonth最近三个月, hafeYear最近半年, thisYear今年, lastYear去年
*
* @author zhanzkw
*
*/
public class TimeFormatHelper {
// 用来全局控制 上一周,本周,下一周的周数变化
private static int weeks = 0;
private static int MaxDate;// 一月最大天数
private static int MaxYear;// 一年最大天数
public static Hashtable<String, String> GetDateToDate(DayType dayType) {
Hashtable<String, String> ht = new Hashtable<String, String>();
String start = "";
String end = "";
switch (dayType) {
case thisMonth:// 本月
start = getThisMonthStart();
end = getThisMonthEnd();
break;
case lastMonth:// 上月
start = getLastMonthStart();
end = getLastMonthEnd();
break;
case lastThreeMonth:// 最近三个月(包含本月)
start = getLastThreeMonthStart(-2);
end = getThisMonthEnd();// 最近三个月的最后一天即是本月的最后一天
break;
case hafeYear:// 最近半年
start = getLastThreeMonthStart(-5);
end = getThisMonthEnd();
break;
case thisYear:// 今年
start = getThisYearStart();
end = getThisYearEnd();
break;
case lastYear:// 去年
start = getLastYearFirst();
end = getLastYearEnd();
break;
default:
start = getThisMonthStart();
end = getThisMonthEnd();
break;
}
ht.put("start", start);
ht.put("end", end);
return ht;
}
// 获取当月第一天
private static String getThisMonthStart() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
str = sdf.format(lastDate.getTime());
return str;
}
// 计算当月最后一天
private static String getThisMonthEnd() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
str = sdf.format(lastDate.getTime());
return str;
}
// 上月第一天
private static String getLastMonthStart() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
lastDate.add(Calendar.MONTH, -1);// 减一个月,变为下月的1号
// lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
str = sdf.format(lastDate.getTime());
return str;
}
// 上月最后一天
private static String getLastMonthEnd() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, -1);// 减一个月
lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
str = sdf.format(lastDate.getTime());
return str;
}
// 最近三个月第一天
private static String getLastThreeMonthStart(int monthCount) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.MONTH, monthCount);
calendar.set(Calendar.DAY_OF_MONTH, 1);
String str = DateFormat.getDateInstance(DateFormat.MEDIUM).format(
(Date) calendar.getTime().clone());
return str;
}
// 获得本年第一天
private static String getThisYearStart() {
int yearPlus = getYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, yearPlus);
Date yearDay = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preYearDay = df.format(yearDay);
return preYearDay;
}
private static int getYearPlus() {
Calendar cd = Calendar.getInstance();
int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天
cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
if (yearOfNumber == 1) {
return -MaxYear;
} else {
return 1 - yearOfNumber;
}
}
// 获得本年最后一天
private static String getThisYearEnd() {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
String years = dateFormat.format(date);
return years + "-12-31";
}
// 获得去年第一天
public static String getLastYearFirst() {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
years_value--;
return years_value + "-1-1";
}
// 获得去年最后一天
private static String getLastYearEnd() {
weeks--;
int yearPlus = getYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks
+ (MaxYear - 1));
Date yearDay = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preYearDay = df.format(yearDay);
getThisSeasonTime(11);
return preYearDay;
}
// 获得本季度
private static String getThisSeasonTime(int month) {
int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
int season = 1;
if (month >= 1 && month <= 3) {
season = 1;
}
if (month >= 4 && month <= 6) {
season = 2;
}
if (month >= 7 && month <= 9) {
season = 3;
}
if (month >= 10 && month <= 12) {
season = 4;
}
int start_month = array[season - 1][0];
int end_month = array[season - 1][2];
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
String years = dateFormat.format(date);
int years_value = Integer.parseInt(years);
int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
int end_days = getLastDayOfMonth(years_value, end_month);
String seasonDate = years_value + "-" + start_month + "-" + start_days
+ ";" + years_value + "-" + end_month + "-" + end_days;
return seasonDate;
}
/**
* 获取某年某月的最后一天
*
* @param year
* 年
* @param month
* 月
* @return 最后一天
*/
private static int getLastDayOfMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
return 0;
}
/**
* 是否闰年
*
* @param year
* 年
* @return
*/
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
enum DayType {
thisMonth, lastMonth, lastThreeMonth, hafeYear, thisYear, lastYear
}
使用方法:
import java.util.Hashtable;
public class TimerTest {
/**
* @param args
*/
public static void main(String[] args) {
DayType dayType = DayType.lastThreeMonth;
Hashtable<String, String> ht = TimeFormatHelper.GetDateToDate(dayType);
System.out.println(ht.get("start"));
System.out.println(ht.get("end"));
}
}