0%

日期的使用

Calendar 类

参考文档:https://blog.csdn.net/weixin_44259720/article/details/86669177

概述

1
2
public abstract class Calendar
extends Objectimplements Serializable, Cloneable, Comparable<Calendar>{}

该类由 abstract 修饰,是一个抽象类,因此无法通过 new 来实现。它提供了 getInstance()方法来获取由当前日期及时间初始化的子类。

常用方法

获取时间

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//获取时间:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testGetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化

//当前系统时间
Date date = cal.getTime(); //date=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27

//当前年
int year = cal.get(Calendar.YEAR); //year=2019

//当前月 Calendar.MONTH从0开始 ,使用时通常会+1
int month = cal.get(Calendar.MONTH) + 1; //month=1

//当前日:两种方法等价
int day_of_month = cal.get(Calendar.DAY_OF_MONTH); //day_of_month=27
int day = cal.get(Calendar.DATE); //day=27

//获取当月day的最大值!!
int max_day_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //max_day_of_month=31

//当前时钟:24小时制
int hour24 = cal.get(Calendar.HOUR_OF_DAY); //hour24=16

//当前时钟:12小时制
int hour12 = cal.get(Calendar.HOUR); //hour12=4

//当前:分钟
int minute = cal.get(Calendar.MINUTE); //minute=27

//当前秒
int second = cal.get(Calendar.SECOND); //second=27

//星期几:用数字(1~7)表示(星期日~星期六),使用时通常会-1
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1; //day_of_week=0

//上午-0;下午-1
int amOrPm = cal.get(Calendar.AM_PM); //amOrPm=1

//当前年的第几周
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR); //week_of_year=5

//当前月的星期数
int week_of_month = cal.get(Calendar.WEEK_OF_MONTH); //week_of_month=5

//当前月中的第几个星期
int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); //day_of_week_in_month=4

//当前年的第几天
int day_of_year = cal.get(Calendar.DAY_OF_YEAR); //day_of_year=27
}
}

设置时间

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
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//设置时间:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testSetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化

//1.当前系统时间
Date date = cal.getTime(); //data=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27

//2.设置时间:不设置项,默认Calendar已经存在日期
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 30);
String date1 = sdf.format(cal.getTime()); //time=2019-01-27 11:30:30

//3.设置时间:全部设置,则输出自定义时间
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.MARCH, 7);
cal.set(Calendar.DATE, 21);
String date2 = sdf.format(cal.getTime()); //time=2018-08-21 11:30:30

//4.时间重置:Date date = new date();
String newDate1 = sdf.format(cal.getTime()); //newDate1=2018-08-21 11:30:30
String newDate2 = sdf.format(new Date()); //newDate2=2019:01:27 16:27:27
}
}

时间运算

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
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//时间运算:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testAddCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化

String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27

cal.add(Calendar.YEAR, 1);
String time1 = sdf.format(cal.getTime()); //年:2020-01-27 16:27:27

cal.add(Calendar.MONTH, 2);
String time2 = sdf.format(cal.getTime()); //月:2020-03-27 16:27:27

cal.add(Calendar.DATE, 5);
String time3 = sdf.format(cal.getTime()); //日:2020-04-01 16:27:27

cal.add(Calendar.HOUR_OF_DAY, -1);
String time4 = sdf.format(cal.getTime()); //时:2020-04-01 15:27:27

cal.add(Calendar.MINUTE, 1);
String time5 = sdf.format(cal.getTime()); //分:2020-04-01 15:28:27

cal.add(Calendar.SECOND, 1);
String time6 = sdf.format(cal.getTime()); //秒:2020-04-01 15:27:28
}
}