public static JSONObject getCurrentDateBeginAndEnd(int years, int type){
Calendar begin = Calendar.getInstance();
Calendar end = Calendar.getInstance();
//年份
int year = begin.get(Calendar.YEAR)+years;
//月份
int month = begin.get(Calendar.MONTH);
//今天
if(type == 1){
begin.add(Calendar.DATE,0);
end.add(Calendar.DATE,0);
}
//昨天
if(type == 2){
begin.add(Calendar.DATE,-1);
end.add(Calendar.DATE,-1);
}
//计算周
if(type == 3) {
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
begin.setWeekDate(year, week, 2);
end.setWeekDate(year, week+1, 1);
}
//计算上周
if(type == 4) {
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
begin.setWeekDate(year, week-1, 2);
end.setWeekDate(year, week, 1);
}
//本月
if(type == 5){
begin.set(year, month, 1);
//月的结束时间
end.set(year, month + 1, 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//上月
if(type == 6){
begin.set(year, month-1, 1);
//月的结束时间
end.set(year, month, 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//季度
if(type == 7) {
//开始月
int startMonth = month - month % 3;
//结束月
int endMonth = startMonth + 3;
//月的开始时间
begin.set(year, startMonth, 1);
//月的结束时间
end.set(year, endMonth, 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//上季
if(type == 8) {
//开始月
int startMonth = month - month % 3;
//结束月
int endMonth = startMonth + 3;
//月的开始时间
begin.set(year, startMonth-3, 1);
//月的结束时间
end.set(year, endMonth-3 , 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//前天
if(type == 11){
begin.add(Calendar.DATE,-2);
end.add(Calendar.DATE,-2);
}
//上周今天
if(type == 12) {
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
Integer day = begin.get(Calendar.DAY_OF_WEEK);
begin.setWeekDate(year, week-1, day);
end.setWeekDate(year, week-1, day);
}
//上月本周
if(type == 13){
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
begin.setWeekDate(year, week-5, 2);
end.setWeekDate(year, week-4, 1);
}
//前季
if(type == 14) {
//开始月
int startMonth = month - month % 3;
//结束月
int endMonth = startMonth + 3;
//月的开始时间
begin.set(year, startMonth-6, 1);
//月的结束时间
end.set(year, month-5 , 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//年
if(type == 15){
begin.set(year, 0, 1);
end.set(year, 11, 31);
}
//前月
if(type == 16){
begin.set(year, month-2, 1);
//月的结束时间
end.set(year, month-1, 1);
end.add(Calendar.DAY_OF_YEAR, -1);
}
//计算前周
if(type == 17) {
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
begin.setWeekDate(year, week-2, 2);
end.setWeekDate(year, week-1, 1);
}
//上周昨天
if(type == 18) {
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
Integer day = begin.get(Calendar.DAY_OF_WEEK);
begin.setWeekDate(year, week-1, day-1);
end.setWeekDate(year, week-1, day-1);
}
//上月上周
if(type == 19){
Integer week = begin.get(Calendar.WEEK_OF_YEAR);
begin.setWeekDate(year, week-6, 2);
end.setWeekDate(year, week-5, 1);
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//开始日期
String beginStr = simpleDateFormat.format(begin.getTime());
String endStr = simpleDateFormat.format(end.getTime());
String[] result = {beginStr, endStr};
JSONObject jsonObject = new JSONObject();
jsonObject.put("startDate",beginStr);
jsonObject.put("endDate",endStr);
return jsonObject;
}
版权声明:本文为qq_36005400原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。