java 每日、月、季度的开始时间和结束时间
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Test {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar now = Calendar.getInstance();
Calendar startDate = new GregorianCalendar();//开始时间
Calendar endDate = new GregorianCalendar();//结束时间
int minDay = now.getActualMinimum(Calendar.DAY_OF_MONTH);
int maxDay = now.getActualMaximum(Calendar.DAY_OF_MONTH);
//时段,一小时为一个时段
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);
System.out.println("当前时段开始时间:"+sdf.format(startDate.getTime()));
endDate.set(Calendar.MINUTE, 59);
endDate.set(Calendar.SECOND, 59);
System.out.println("当前时段结束时间:"+sdf.format(endDate.getTime()));
// 当天开始时间
startDate.set(Calendar.HOUR_OF_DAY, 0);
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);
System.out.println("当天开始时间:"+sdf.format(startDate.getTime()));
//当天结束时间
endDate.set(Calendar.HOUR_OF_DAY, 23);
endDate.set(Calendar.MINUTE, 59);
endDate.set(Calendar.SECOND, 59);
System.out.println("当天结束时间:"+sdf.format(endDate.getTime()));
// 当周开始时间
startDate.setFirstDayOfWeek(Calendar.MONDAY);
startDate.set(Calendar.HOUR_OF_DAY, 0);
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);
startDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("当周开始时间:"+sdf.format(startDate.getTime()));
//当周结束时间
endDate.setFirstDayOfWeek(Calendar.MONDAY);
endDate.set(Calendar.HOUR_OF_DAY, 23);
endDate.set(Calendar.MINUTE, 59);
endDate.set(Calendar.SECOND, 59);
endDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("当周结束时间:"+sdf.format(endDate.getTime()));
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), minDay, 00, 00, 00);
System.out.println("当月的第一天:" + sdf.format(now.getTime()));
System.out.println("maxDay:"+maxDay+"Calendar.DAY_OF_MONTH:"+Calendar.DAY_OF_MONTH);
//按你的要求设置时间
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), maxDay, 23, 59, 59);
System.out.println("当月的最后一天:" + sdf.format(now.getTime()));
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("-----------------------");
System.out.println(sdf.format(now.getTime()));
int month = now.get(Calendar.MONTH) + 1;
if(month == 1 || month ==2 || month ==3){
now.set(now.get(Calendar.YEAR), 0, minDay, 00, 00, 00);
System.out.println("一季度的第一天:" + sdf.format(now.getTime()));
now.set(now.get(Calendar.YEAR), 2, maxDay, 23, 59, 59);
System.out.println("一季度的最后一天:" + sdf.format(now.getTime()));
}
else if(month == 4 || month ==5 || month ==6){
now.set(now.get(Calendar.YEAR), 3, minDay, 00, 00, 00);
System.out.println("二季度的第一天:" + sdf.format(now.getTime()));
now.set(now.get(Calendar.YEAR), 5, maxDay, 23, 59, 59);
System.out.println("二季度的最后一天:" + sdf.format(now.getTime()));
}
else if(month == 7 || month ==8 || month ==9){
now.set(now.get(Calendar.YEAR), 6, minDay, 00, 00, 00);
System.out.println("三季度的第一天:" + sdf.format(now.getTime()));
now.set(now.get(Calendar.YEAR), 8, maxDay, 23, 59, 59);
System.out.println("三季度的最后一天:" + sdf.format(now.getTime()));
}
else if(month == 10 || month ==11 || month ==12){
now.set(now.get(Calendar.YEAR), 9, minDay, 00, 00, 00);
System.out.println("四季度的第一天:" + sdf.format(now.getTime()));
now.set(now.get(Calendar.YEAR), 11, maxDay, 23, 59, 59);
System.out.println("四季度的最后一天:" + sdf.format(now.getTime()));
}
}