Java之时间处理(当前年的上一年、上一季度、当月、当季)

一、当前年的上一年

核心代码:

1
2
3
4
5
6
7
public static String getYearBefore() {
SimpleDateFormat formats = new SimpleDateFormat("yyyy");
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
Date date = c.getTime();
return formats.format(date);
}

二、上一季度

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 获取上一季度 开始和结束时间
*
* @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());
}


/**
* 最小时间
*
* @param calendar
*/
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);
}

/**
* 最大时间
*
* @param calendar
*/
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));
}

DateRange.java:

1
2
3
4
5
6
7
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DateRange {
private Date start;
private Date end;
}

三、当月

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 获取当月(开始时间)
*
* @return
*/
public static String getCurrentMonthStartDate() {

// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月第一天
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String firstday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime());
return firstday;
}


/**
* 获取当月(结束时间)
*
* @return
*/
public static String getCurrentMonthEndDate() {

// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月最后一天
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String lastday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
return lastday;
}

四、当季

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 获取当季
*
* @return
*/
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());
}

文章目录
  1. 一、当前年的上一年
  2. 二、上一季度
  3. 三、当月
  4. 四、当季