欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java根据当前日期获取年初,年末,季初,季末,月初,月末日期的方法

程序员文章站 2022-05-15 09:49:57
...

年初

	public static Date getYearFirst(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.clear(Calendar.MONTH);
		c.add(Calendar.YEAR, 0);
		/*c.add(Calendar.MONTH, 0);*/
		c.set(Calendar.DAY_OF_MONTH, 1);
		//设置为1号,当前日期既为本月第一天
		//c.set(Calendar.DAY_OF_MONTH, 1);
		//将小时至0
		c.set(Calendar.HOUR_OF_DAY, 0);
		//将分钟至0
		c.set(Calendar.MINUTE, 0);
		//将秒至0
		c.set(Calendar.SECOND, 0);
		//将毫秒至0
		c.set(Calendar.MILLISECOND, 0);
		String first = format.format(c.getTime());
		return 	DateUtils.parseDate(first);
	}

年末

public static Date getYearLast(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar ca = Calendar.getInstance();
		ca.clear(Calendar.MONTH);
		ca.set(Calendar.MONTH, 11);
		ca.set(Calendar.DAY_OF_MONTH,
				ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		//将小时至23
		ca.set(Calendar.HOUR_OF_DAY, 23);
		//将分钟至59
		ca.set(Calendar.MINUTE, 59);
		//将秒至59
		ca.set(Calendar.SECOND, 59);
		//将毫秒至999
		ca.set(Calendar.MILLISECOND, 999);
		String last = format.format(ca.getTime());
		return 	DateUtils.parseDate(last);
	}

月初

	public static Date getMonthFirst(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		//设置为1号,当前日期既为本月第一天
		c.set(Calendar.DAY_OF_MONTH, 1);
		//将小时至0
		c.set(Calendar.HOUR_OF_DAY, 0);
		//将分钟至0
		c.set(Calendar.MINUTE, 0);
		//将秒至0
		c.set(Calendar.SECOND, 0);
		//将毫秒至0
		c.set(Calendar.MILLISECOND, 0);
		String first = format.format(c.getTime());
		return 	DateUtils.parseDate(first);

	}

月末

public static Date getMonthlast(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH,
				ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		//将小时至23
		ca.set(Calendar.HOUR_OF_DAY, 23);
		//将分钟至59
		ca.set(Calendar.MINUTE, 59);
		//将秒至59
		ca.set(Calendar.SECOND, 59);
		//将毫秒至999
		ca.set(Calendar.MILLISECOND, 999);
		String last = format.format(ca.getTime());
	return 	DateUtils.parseDate(last);


	}

季度问题

public class DateRange implements Serializable {
    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;
    }

}
	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 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());
	}
相关标签: 后端 技术