日期工具新增。

This commit is contained in:
Renpc-kilig 2021-01-04 17:45:34 +08:00
parent fb6db0c1c0
commit af9268a56c
2 changed files with 245 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.cm.bigdata.utils;
import java.util.Date;
/**
* @author 29492
*/
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;
}
}

View File

@ -0,0 +1,215 @@
package com.cm.bigdata.utils;
/**
* Calendar操作Date
* @author renpc
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils{
/**设置初始值*/
private static Calendar cal;
/**
* 根据当前系统时区定义Calendar
*/
static {
cal = Calendar.getInstance();
}
/**
* 获取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());
}
/**
* 根据指定日期获取当前属于第几个季度
* @param date
*/
public static int getNowQuarter(Date date){
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;
int quarter = 0;
//判断季度
if (month >= 1 && month <= 3) {
quarter = 1;
} else if (month >= 4 && month <= 6) {
quarter = 2;
} else if (month >= 7 && month <= 9) {
quarter = 3;
} else {
quarter = 4;
}
return quarter;
}
/**
* 获取当前季度的时间范围
* @return current quarter
*/
public static DateRange returnQuarterCurrent(int year, int quarter) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
if(quarter == 1) {
startCalendar.set(year, 1, 1, 00, 00, 00);
endCalendar.set(year, 3,31, 23,59,59);
}else if(quarter == 2) {
startCalendar.set(year, 4, 1, 00, 00, 00);
endCalendar.set(year, 6,30, 23,59,59);
}else if(quarter == 3) {
startCalendar.set(year, 7, 1, 00, 00, 00);
endCalendar.set(year, 9,30, 23,59,59);
}else {
startCalendar.set(year, 10, 1, 00, 00, 00);
endCalendar.set(year, 12,31, 23,59,59);
}
setMinTime(startCalendar);
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 获取当前季度的时间范围
* @return current quarter
*/
public static DateRange getThisQuarter(int quarter) {
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()));
}
}