5.5 日期时间类

系列 - Java API
目录
本教学实验手册介绍了Java中日期时间类的使用,包括LocalDate、LocalTime、LocalDateTime、Duration和Period。学生将学习如何获取当前时间,创建指定日期时间对象,进行格式化输出,以及计算时间间隔。这些类提供了处理日期和时间的便捷方法。
任务一:日期时间类
1.1 LocalDate
1.2 LocalTime
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Example22 {
public static void main(String[] args) {
// 1. 获取当前时间
LocalTime now = LocalTime.now();
// 2. 表示过去时间
LocalTime of = LocalTime.of(9, 23, 23);
// 3. 三个getter
System.out.println("时:" + now.getHour());
System.out.println("分:" + now.getMinute());
System.out.println("秒:" + now.getSecond());
// 4. 格式转换,借助 DateTimeFormatter System.out.println("\nnow默认格式:" + now);
System.out.println("不显示微秒:" + now.withNano(0));
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH点mm分ss秒")));
// 5. 判断
System.out.println("\nof在now之前吗?" + of.isBefore(now));
// 6. 解析字符串
LocalTime time = LocalTime.parse("12:15:30");
System.out.println("将字符串转换成时间对象:" + time);
}
}
1.3 LocalDateTime
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Example23 {
public static void main(String[] args) {
// 1. 获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("原格式:" + now);
System.out.println("只有日期:" + now.toLocalDate());
System.out.println("只有时间:" + now.toLocalTime());
// 2. getter 方法
System.out.println(now.getYear() +"年" + now.getMonthValue() + "月" + now.getDayOfMonth() + "号 " + now.getHour() + "点" + now.getMinute() + "分" + now.getSecond() + "秒");
// 3. 指定格式
System.out.println("指定格式:" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
1.4 Duration
import java.time.Duration;
import java.time.LocalTime;
public class Example24 {
public static void main(String[] args) {
LocalTime start = LocalTime.now();
LocalTime end = LocalTime.of(21, 53, 0);
Duration duration = Duration.between(start, end);
System.out.println("时间间隔(纳秒)" + duration.toNanos());
System.out.println("时间间隔(毫秒)" + duration.toMillis());
System.out.println("时间间隔(秒)" + duration.toSeconds());
System.out.println("时间间隔(分钟)" + duration.toMinutes());
System.out.println("时间间隔(小时)" + duration.toHours());
}
}
1.5 Period
public class Example25 {
public static void main(String[] args) {
LocalDate birthday = LocalDate.of(2018, 12, 12);
LocalDate now = LocalDate.of(2020, 11, 13);
Period period = Period.between(birthday, now);
System.out.println("时间间隔(年)" + period.getYears());
System.out.println("时间间隔(月)" + period.getMonths());
System.out.println("时间间隔(天)" + period.getDays());
}
}
作业
1. LocaTime
课堂练习:计算上课时间 要求:模仿1.2的代码,完成以下功能:
- 设定上课时间为 10:30:00
- 获取现在的时间
- 输出: - 上课时间的时、分、秒 - 把上课时间格式化成"XX点XX分"的形式 - 判断现在是否已经迟到(现在的时间是否在上课时间之后)
2. LocalDateTime
课堂练习:生日信息查看器 要求:
- 设定一个生日,例如:2000年1月1日 12:00:00
- 获取现在的日期时间
- 分别输出: - 你的生日的完整日期时间 - 只显示生日的日期部分 - 只显示生日的时间部分 - 用中文格式显示你的生日(比如:2000年1月1号 12点00分00秒) - 用yyyy-MM-dd HH:mm:ss的格式显示你的生日
3. Duration
课堂练习:计算上课时间 要求:
- 设定上课时间为上午10:30:00
- 设定下课时间为上午12:00:00
- 计算并显示:
- 这节课的时长是多少秒
- 这节课的时长是多少分钟
- 这节课的时长是多少小时