1.Date类
1.1 时间原点和毫秒值
- 时间原点(0毫秒):1970年1月1日 00:00:00(是英国格林尼治的时间)
- 中国在东八区,1970年1月1日 08:00:00
- 把当前日期/时间转换成毫秒值,就是计算当前日期/时间到时间原点经历了多少毫秒
- System.currentTimeMillis()可以获取当前系统时间到时间原点经历了多少毫秒
- 毫秒值就是我平时用的时间戳
1.2 Date类
java.util.Date类
+ Date类表示特定的瞬间,表示日期和时间
+ 精确到毫秒
1.2.1 构造方法
- 构造方法1.Date()空参数构造方法
- 获取的就是当前系统的日期和时间
public static void main(String[] args) {
Date date = new Date();
System.out.print(date); // Sun Aug 09 15:35:37 CST 2022
}
CST是中国标准时间
- 构造方法2. Date(long time)
- 参数long time是毫秒值
- 功能:把毫秒值转为Date日期
public static void main(String[] args) {
Date d1 = new Date(0L);
System.out.print(d1); // Thu Jan 01 08:00:00 CST 1970
Date d2 = new Date(158111111111L);
System.out.print(d1); // Sun Jan 05 07:45:11 CST 1975
}
1.2.2 成员方法getTime()
- getTime()方法,用于把日期转换成毫秒
public static void main(String[] args) {
Date d1 = new Date();
long time = d1.getTime();
System.out.print(time);
}
这里打印出来是毫秒值
- 小结
- 毫秒转日期: 用Date d2 = new Date(158111111111L)
- 日期转毫秒: 用long time = d1.getTime(),其中d1是Date对象
2.DateFormat类
- java.text.DateFormat是抽象类
- 可以使用DateFormat的子类,即SImpleDateFormat类
- 可以对日期/时间进行格式化,完成Date(日期/时间)和String(文本)之间的转换
- 格式化:按照指定的格式,从Date对象转换为String对象
- 解析: 按照指定的格式,从String对象转换为Date对象
2.1 DateFormat类的成员方法format()&parse()
- 成员方法1. String format(Date date)
- 按照指定的格式,把Date日期,格式化为相应格式的字符串
- 成员方法2. Date parse(String source)
- 把符合格式的字符串,解析为Date日期
3.SimpleDateFormat类
- 继承关系:java.text.SimpleDateFormat extends DateFormat
- 构造方法SimpleDateFormat(String pattern)
- 参数String pattern 指定的格式,区分大小写
- y是年,M是年中的月份,d是月份中的天数,H是小时,m分钟,s秒,S毫秒,z/Z是时区(小写、大写啥区别)
- 示例1"yyyy-MM-dd HH:mm:ss" , 示例2"yyyy年MM月dd日 HH时mm分ss秒"
- 模式中的特殊字母不能改变,但是连接字母的符号可以改变
- 参数String pattern 指定的格式,区分大小写
3.1 SimpleDateFormat类的使用
3.1.1 使用format方法把日期转换为文本
使用步骤
- 创建SimpleDateFormat对象,构造方法中传所需的模板
- 调用SimpleDateFormat对象的format(Date date)方法,就会按照模板,把Date日期格式化为字符串
public static void main(String[] args) {
Date d1 = new Date();
// 1.创建SimpleDateFormat对象,构造方法中传所需的模板
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 2.调用SimpleDateFormat对象的format方法,就会按照模板,把Date日期格式化为字符串
String text = sdf.format(d1);
System.out.println(d1); // Sun Aug 09 15:35:37 CST 2022
System.out.println(text); // 2022-08-09 15:35:37
}
3.1.2 使用parse方法把文本转换为日期
使用步骤
- 创建SimpleDateFormat对象,构造方法中传所需的模板
- 调用SimpleDateFormat对象的parse(String source)方法,把符合模板的字符串,解析为Date日期
- String source是格式化的时间字符串
public static void main(String[] args) {
// 1.创建SimpleDateFormat对象,构造方法中传所需的模板
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 2.调用SimpleDateFormat对象的parse(String source)方法,把符合模板的字符串,解析为Date日期
Date d1 = sdf.parse("2022-12-4 12:44:30");
System.out.println(d1); // Sun Aug 09 15:35:37 CST 2022
}
- 如果时间字符串与sdf构造方法中的模板不一样,就会抛出解析异常ParseException
3.1.3 练习:计算一个人从现在到出生经过了多少天
public static void main(String[] args) {
String birthdayString = "2008-06-08";
// 1.使用SimpleDateFormat对象的parse方法,把字符串的出生日期解析为Date形式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = sdf.parse(birthdayString);
// 2.把Date格式的出生日期转为毫秒值
long birthdayTime = birthdayDate.getTime();
// 3.获取当前的日期,转换成毫秒值
long todayTime = new Date().getTime();
// 4.当前日期的毫秒值-出生日期的毫秒值,只有毫秒值可以相减
long intervalTime = todayTime - birthdayTime;
// 5.把毫秒值的差值转换为天,差值/1000/60/60/24
System.out.println(intervalTime/1000/60/60/24);
}
4. 日历类Calendar
- java.util.Calendar是抽象类,取代了很多Date类的方法
- Calendar类无法直接创建对象使用,得使用静态方法Calendar.getInstance()
- static Calendar getInstance()使用默认时区和语言环境,获得一个日历对象,是Calendar类的子类对象
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); // 子类对象赋值给父类引用,是多态
System.out.println(c); // 打印出来,不是内存地址,说明Calendar类重写了toString()方法
}
打印出来的是java.util.GregorianCalendar的对象,里面有很多日历相关的数据
4.1 Calendar的成员方法&静态成员变量
- calendar的成员方法
- public int get(int field) 返回给定日历字段的值
- 参数int field是日历类的字段,可以使用Calendar的静态成员变量获取,例如Calendar.YEAR,Calendar.MONTH等
- public static final int YEAR = 1; 年
- public static final int MONTH = 2; 月
- public static final int DATE = 5; 月中的某一天
- public static final int DAY_OF_MONTH = 5; 月中的某一天
- public static final int HOUR = 10; 时
- public static final int MINUTE = 12; 分
- public static final int SECOND = 13; 秒
- 参数int field是日历类的字段,可以使用Calendar的静态成员变量获取,例如Calendar.YEAR,Calendar.MONTH等
- public void set(int field, int value) 将给定的日历字段,设置为给定的值
- public abstract void add(int field, int amount) 给日历字段添加/减去指定的时间量
- public Date getTime() 返回一个表示此时Calendar时间值的Date对象
- public int get(int field) 返回给定日历字段的值
4.1.1 使用Calendar对象的get()方法
- public int get(int field) 返回给定日历字段的值
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
System.out.println(year); // 打印出来是2022
int month = c.get(Calendar.MONTH);
System.out.println(month + 1); // 西方的月份是0-11,东方的月份1-12,所以要用month+1
int day = c.get(Calendar.Date); // 这里传入DAY_OF_MONTH也可以
System.out.println(day);
}
4.1.2 使用Calendar对象的set()方法
- public void set(int field, int value) 将给定的日历字段,设置为给定的值
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
// 设置年是9999
c.set(Calendar.YEAR, 9999);
// 设置月是9 -> 其实就是东方的十月
c.set(Calendar.MONTH, 9);
// 设置日子是8
c.set(Calendar.DATE, 8);
// 可以同时设置年月日,使用set的重载方法
c.set(8888, 2, 3);
int year = c.get(Calendar.YEAR);
System.out.println(year); // 打印出来是2022
int month = c.get(Calendar.MONTH);
System.out.println(month); // 打印出来是2
int day = c.get(Calendar.Date);
System.out.println(day); // 打印出来是3
}
4.1.3 使用Calendar对象的add方法
- public abstract void add(int field, int amount) 给日历字段添加/减去指定的时间量
- 参数int amout:正数是增加,负数是减少
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
// 把年增加2年
c.add(Calendar.YEAR, 2);
// 把月减少3个月
c.add(Calendar.MONTH, -3);
System.out.println(c);
}
4.1.4 使用Calendar对象的getTime方法
- public Date getTime() 返回一个表示此时Calendar时间值的Date对象
- Date类getTime()方法与Calendar类getTime()方法的区别
- Date类的getTime()方法是:将日期Date对象转换成毫秒值
- Calendar类的getTime()方法是:将日历Calendar对象转换成日期Date对象
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
// 将日历对象转成日期对象
Date d1 = c.getTime();
System.out.println(d1); // Sun Aug 09 20:35:21 CST 2022
}