Java中获取当前季度的时间范围, 上个季度的时间范围, 昨天的时间范围, 当前月的时间范围, 上个月的时间范围:
DateUtils.java
package top.itart.blog.common.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import top.itart.blog.common.bean.DateRange;
public class DateUtils{
/**
* 获取date的月份的时间范围
* @param date
* @return
*/
public static DateRange getMonthRange(Date date) {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(date);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMaxTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(date);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取当前季度的时间范围
* @return current quarter
*/
public static DateRange getThisQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取昨天的时间范围
* @return
*/
public static DateRange getYesterdayRange() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.add(Calendar.DAY_OF_MONTH, -1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.add(Calendar.DAY_OF_MONTH, -1);
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取当前月份的时间范围
* @return
*/
public static DateRange getThisMonth(){
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取上个月的时间范围
* @return
*/
public static DateRange getLastMonth(){
Calendar startCalendar = Calendar.getInstance();
startCalendar.add(Calendar.MONTH, -1);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.add(Calendar.MONTH, -1);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取上个季度的时间范围
* @return
*/
public static DateRange getLastQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
private static void setMinTime(Calendar calendar){
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
private static void setMaxTime(Calendar calendar){
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
}
public final static String DEFAULT_PATTERN = "MM/dd/yyyy HH:mm:ss";
public static String format(Date date){
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
return sdf.format(date);
}
public static void main(String[] args) {
DateRange currentQuarter = getThisQuarter();
System.out.println("当前季度的时间范围: "+DateUtilsTemp.format(currentQuarter.getStart())+" - "+DateUtilsTemp.format(currentQuarter.getEnd()));
DateRange yesterdayRange = getYesterdayRange();
System.out.println("昨天的时间范围: "+DateUtilsTemp.format(yesterdayRange.getStart())+" - "+DateUtilsTemp.format(yesterdayRange.getEnd()));
DateRange thisMonth = getThisMonth();
System.out.println("当前月份的时间范围: "+DateUtilsTemp.format(thisMonth.getStart())+" - "+DateUtilsTemp.format(thisMonth.getEnd()));
DateRange lastMonth = getLastMonth();
System.out.println("上个月的时间范围: "+DateUtilsTemp.format(lastMonth.getStart())+" - "+DateUtilsTemp.format(lastMonth.getEnd()));
DateRange lastQuarter = getLastQuarter();
System.out.println("上个季度的时间范围: "+DateUtilsTemp.format(lastQuarter.getStart())+" - "+DateUtilsTemp.format(lastQuarter.getEnd()));
}
}
DateRange.java
package top.itart.blog.common.bean;
import java.util.Date;
public class DateRange {
private Date start;
private Date end;
public DateRange(Date start, Date end) {
this.start = start;
this.end = end;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
}
如果当前时间是: 2015/09/06, 则运行结果如下:
当前季度的时间范围: 07/01/2015 00:00:00 - 09/30/2015 23:59:59
昨天的时间范围: 09/05/2015 00:00:00 - 09/05/2015 23:59:59
当前月份的时间范围: 09/01/2015 00:00:00 - 09/30/2015 23:59:59
上个月的时间范围: 08/01/2015 00:00:00 - 08/31/2015 23:59:59
上个季度的时间范围: 04/01/2015 00:00:00 - 06/30/2015 23:59:59