Java关于日期的计算持续汇总

0描述:传入Date date.返回String yyyyMMdd.
1
2
3
4
5
6
7
8
9
10
/**
* 00
* 描述:传入Date date.转为 String yyyyMMdd.
* 【时间 2019-04-18 15:41:12 作者 陶攀峰】
*/
public static String getDateToString(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
}
1描述:获取传入日期前一天 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 01
* 描述:获取传入日期前一天 .
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:20:44 作者 陶攀峰】
*/
public static String getYesterday(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, - 1);
return simpleDateFormat.format(calendar.getTime());
}
2描述:获取传入日期前七天 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 02
* 描述:获取传入日期前七天 .
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:20:56 作者 陶攀峰】
*/
public static String getBeforeSevenDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, - 7);
return simpleDateFormat.format(calendar.getTime());
}
3描述:获取传入日期前六天 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 03
* 描述:获取传入日期前六天 .
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:21:07 作者 陶攀峰】
*/
public static String getBeforeSixDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, - 6);
return simpleDateFormat.format(calendar.getTime());
}
4描述:获取传入日期所在月的第一天.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 04
* 描述:获取传入日期所在月的第一天.
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:21:17 作者 陶攀峰】
*/
public static String getNowMonthFirstDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(Calendar.DAY_OF_MONTH, 1);
return simpleDateFormat.format(calendar.getTime());
}
5描述:获取传入日期所在月的最后一天.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 05
* 描述:获取传入日期所在月的最后一天.
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:21:28 作者 陶攀峰】
*/
public static String getNowMonthFinallyDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return simpleDateFormat.format(calendar.getTime());
}
6描述:获取传入日期上个月的第一天.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 06
* 描述:获取传入日期上个月的第一天.
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:21:38 作者 陶攀峰】
*/
public static String getLastMonthFirstDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -1);// 月份减一
calendar.set(Calendar.DAY_OF_MONTH, 1);
return simpleDateFormat.format(calendar.getTime());
}
7描述:获取传入日期上个月的最后一天.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 07
* 描述:获取传入日期上个月的最后一天.
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 【时间 2019-04-15 16:21:47 作者 陶攀峰】
*/
public static String getLastMonthFinallyDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.MONTH, -1);// 月份减一
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return simpleDateFormat.format(calendar.getTime());
}
8描述:获取传入时间 + i 天 后的日期.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 08
* 描述:获取传入时间 + i 天 后的日期.
* 参数格式String yyyyMMdd,int .返回String yyyyMMdd .
* i=0表示今天.i=1表示明天.i=-1表示昨天.
* 【时间 2019-04-15 16:21:57 作者 陶攀峰】
*/
public static String getDayByI(String y_date, int i) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.DATE, i);
return simpleDateFormat.format(calendar.getTime());
}
9描述:获取传入时间 + i 月 后的日期.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 09
* 描述:获取传入时间 + i 月 后的日期.
* 参数格式String yyyyMMdd,int .返回String yyyyMMdd .
* i=0 表示本月. i=1表示下个月 .i=-1表示上个月
* 【时间 2019-04-15 16:22:06 作者 陶攀峰】
*/
public static String getMonthByI(String y_date,int i) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
calendar.add(Calendar.MONTH, i);// i为0 表示本月 i为1表示下个月 i为-1表示上个月
return simpleDateFormat.format(calendar.getTime()).substring(0, 6);
}
10描述:获取当前系统时间.
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 10
* 描述:获取当前系统时间.
* 得到String yyyyMMdd .
* 【时间 2019-04-15 16:22:15 作者 陶攀峰】
*/
public static String getNowSystemDay() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
return simpleDateFormat.format(calendar.getTime());
}
11描述:获取当前系统时间的小时数(16:12 就返回16).
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 11
* 描述:获取当前系统时间的小时数(16:12 就返回16).
* 返回int.
* 【时间 2019年4月15日下午4:11:30 作者 陶攀峰】
*/
public static int getNowSystemHour() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
return hour;
}
12描述:根据传进来的时间获取年份.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 12
* 描述:根据传进来的时间获取年份.
* 参数格式String yyyyMMdd.返回int.
* 【时间 2019-04-16 08:28:21 作者 陶攀峰】
*/
public static int getYear(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
int year = calendar.get(Calendar.YEAR);
return year;
}
13描述:根据传进来的时间获取月份.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 13
* 描述:根据传进来的时间获取月份.
* 参数格式String yyyyMMdd.返回int.
* 【时间 2019-04-16 08:30:45 作者 陶攀峰】
*/
public static int getMonth(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
int month = calendar.get(Calendar.MONTH) + 1;
return month;
}
14描述:根据传进来的时间获取天数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 14
* 描述:根据传进来的时间获取天数
* 参数格式String yyyyMMdd.返回int.
* 【时间 2019-04-16 08:31:19 作者 陶攀峰】
*/
public static int getDay(String y_date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(simpleDateFormat.parse(y_date));
} catch (ParseException e) {
e.printStackTrace();
}
int day = calendar.get(Calendar.DATE);
return day;
}
15描述:根据指定年和月 得到指定月的天数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 15
* 描述:根据指定年和月 得到指定月的天数.
* 传入int year,int month. 返回 int.
* 【时间 2019-04-16 08:31:59 作者 陶攀峰】
*/
public static int getDayByYearMonth(int year, int month) {
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
16描述:把yyyyMMdd格式改为yyyy-MM-dd格式.
1
2
3
4
5
6
7
8
9
10
/**
* 16
* 描述:把yyyyMMdd格式改为yyyy-MM-dd格式.
* 参数格式String yyyyMMdd. 返回String yyyy-MM-dd.
* 【时间 2019-04-16 08:33:20 作者 陶攀峰】
*/
public static String ChangeDateFormat(String y_date) {
y_date = y_date.substring(0, 4) + "-" + y_date.substring(4, 6) + "-" + y_date.substring(6, 8);
return y_date;
}
17描述:获取下个月一号的数据.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 17
* 描述:获取下个月一号的数据.
* 传入String yyyyMMdd.返回String yyyyMMdd.
* 例如:20181005 >>> 20181101 .
* 【时间 2019-04-16 08:34:43 作者 陶攀峰】
*/
public static String getNextMonthFirstDay(String y_date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = null;
try {
date = sdf.parse(y_date);// 把设置的日期转换为日期格式
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();// 日历对象
calendar.setTime(date);// 设置日期
calendar.set(Calendar.DAY_OF_MONTH, 1);// 把当前日期天数设置为01
calendar.add(Calendar.MONTH, 1);// 把月份减1
return sdf.format(calendar.getTime());
}
18描述:获取周一. i=0 表示传入时间所在周的周一,i=1表示下周一,i=-1表示上周一.
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
/**
* 18
* 描述:获取周一.
* i=0 表示传入时间所在周的周一,i=1表示下周一,i=-1表示上周一.
* 传入String yyyyMMdd,int. 返回 String yyyyMMdd.
* 【时间 2019-04-16 08:45:09 作者 陶攀峰】
*/
public static String getMondayByIWeek(String y_date,int i) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = null;
try {
date = sdf.parse(y_date);// 把设置的日期转换为日期格式
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);

// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
cal.add(Calendar.DATE, 7 * i);
return sdf.format(cal.getTime());
}
19描述:返回日期所在星期几.星期一 为1、星期日为7. ​```java /** * 19 * 描述:返回日期所在星期几.星期一 为1、星期日为7. * 传入日期String yyyyMMdd. 返回int . * 【时间 2019-04-16 08:48:30 作者 陶攀峰】 */ public static int getWeekNumber(String y_date) { Calendar cal = Calendar.getInstance(); try { cal.setTime(new SimpleDateFormat("yyyyMMdd").parse(y_date)); } catch (ParseException e) { e.printStackTrace(); } return cal.get(Calendar.DAY_OF_WEEK)-1; }
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
</details>
<details>
<summary>20描述:周同期【开始>>以周一为开始】.【传入时间的上一年>>所在周的第一天>>(距离年周数相同>>一年第几周)】.例【2018-01-10>>2017-01-02】.</summary>
```java
/**
* 20
* 描述:周同期【开始>>以周一为开始】.
* 【传入时间的上一年>>所在周的第一天>>(距离年周数相同>>一年第几周)】.
* 例【2018-01-10>>2017-01-02】.
* 【时间 2019年3月5日下午3:46:07 作者 陶攀峰】
*/
public static String getWeekSamePhaseStart(String y_date) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd");
Date date=null;
try {
date = sdf.parse(y_date);//把设置的日期转换为日期格式
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);//设置日期
int week_of_year=calendar.get(Calendar.WEEK_OF_YEAR);//当前日期是今年的第多少周【2】
int year=calendar.get(Calendar.YEAR)-1;//得到今年年份、减去1得到去年年份【2018-1】
calendar.set(year, 0, 1);//设置日期为2017-01-01
int dayofweek=7-calendar.get(Calendar.DAY_OF_WEEK)+1;//【7-1+1】
week_of_year=week_of_year-2;//【2-2】
calendar.add(Calendar.DAY_OF_YEAR, week_of_year*7+dayofweek);//【0*7+7】【此时日期由2017-01-01>>2017-01-08】
int day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【1-1】
if (day_of_week == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
day_of_week = 7 ;
}
calendar.add(Calendar.DATE , -day_of_week + 1 );//【-7+1】【此时日期由2017-01-08>>2017-01-02】
return sdf.format(calendar.getTime());
}
21描述:周同期【结束>>以周一为开始】.【上一年和传入时间所在同一个周的第一天>>(距离年周数相同>>一年第几周)(距离周天数相同>>一周第几天)】.例【2018-01-10>>2017-01-04】.
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
/**
* 21
* 描述:周同期【结束>>以周一为开始】.
* 【上一年和传入时间所在同一个周的第一天>>(距离年周数相同>>一年第几周)(距离周天数相同>>一周第几天)】.
* 例【2018-01-10>>2017-01-04】.
* 【时间 2019年3月5日下午3:47:26 作者 陶攀峰】
*/
public static String getWeekSamePhaseEnd(String y_date) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");
Date date=null;
try {
date = sdf.parse(y_date);//把设置的日期转换为日期格式
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);//设置日期
int day_of_week0 = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【4-1】
if (day_of_week0 == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
day_of_week0 = 7 ;
}
int week_of_year=calendar.get(Calendar.WEEK_OF_YEAR);//当前日期是今年的第多少周【2】
int year=calendar.get(Calendar.YEAR)-1;//得到今年年份、减去1得到去年年份【2018-1】
calendar.set(year, 0, 1);//设置日期为2017-01-01
int dayofweek=7-calendar.get(Calendar.DAY_OF_WEEK)+1;//【7-1+1】
week_of_year=week_of_year-2;//【2-2】
calendar.add(Calendar.DAY_OF_YEAR, week_of_year*7+dayofweek);//【0*7+7】【此时日期由2017-01-01>>2017-01-08】
int day_of_week = calendar.get(Calendar. DAY_OF_WEEK) - 1;//传入时间是本周的第几天【因为DAY_OF_WEEK以周日开始、所以减1】【1-1】
if (day_of_week == 0 ) {//因为DAY_OF_WEEK以周日开始、所以原本的周日是第一天 改为第七天
day_of_week = 7 ;
}
calendar.add(Calendar.DATE , -day_of_week + 1 );//【-7+1】【此时日期由2017-01-08>>2017-01-02】
calendar.add(Calendar.DATE, day_of_week0-1);//【3-1】【此时日期由2017-01-02>>2017-01-04】
return sdf.format(calendar.getTime());
}