Java获取时间范围: 当前季度,上个季度,昨天,当前月,上个月

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值