时间日期类,JDK8时间类
时间日期类
时间概述
1.北京时间需要在世界标准时间上加8小时
2.计算机中时间原点(UNIX操作系统,C语言的诞生日)
1970年1月1日 0:0:0
3.时间换算单位
1秒 = 1000 毫秒
时间日期类-Date构造方法
1.什么是Date类
Date代表一个特定的时间,精确到毫秒
2.Date类的构造方法
方法名 | 说明 |
---|---|
public Date() | 创建Date对象,默认存储当前时间,单位毫秒 |
public Date(long date) | 创建Date对象,并将其初始化表示为从标准时间指定的毫秒数 |
示例代码
import java.util.Date; // 导包的时候注意不要导错了,有两个Date的包
public class MyDate {
public static void main(String[] args) {
// 创建Date对象,默认存储当前时间,单位毫秒
// 创建Date对象,并将其初始化表示为从标准时间指定的毫秒数
// 表示当前电脑系统时间
Date date1 = new Date();
System.out.println(date1);
// 创建了一个对象,表示从计算机原点开始吗,过了0毫秒后的时间
// 打印的时间是1970年1月1日 08:00:00
// 因为我们在中国,在东八区,需要加8个小时
Date date2 = new Date(0L);
System.out.println(date2);
}
}
Date成员方法
方法名 | 说明 |
---|---|
public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
public void setTime(long time) | 设置时间,给的是毫秒值 |
- 示例代码
import java.util.Date; // 注意别导错包,sql和util下都有Date,我们使用的是util
public class GetAndSet {
public static void main(String[] args) {
// 创建对象,默认时间是当前系统时间
Date date1 = new Date();
// 输出一次date1用作对比用,可以查看setTime方法调用是否成功
System.out.println(date1);
// 需要注意的是,如果想看到setTime修改后的效果,必须输入大于1000的值
// 因为秒与毫秒换算单位是1000,计算机控制台打印出的时间到秒,小于1秒的时间是无法打印出来的
// 将时间修改为从计算机原点后1秒的时间
date1.setTime(1000L);
System.out.println(date1);
}
private static Date method1() {
// 创建对象,默认时间是当前系统时间
Date date1 = new Date();
System.out.println(date1);
// date1.getTime();获取当前对象的毫秒值,就是把当前对象里的时间转换成毫秒值
long time = date1.getTime();
System.out.println(time);
return date1;
}
}
SimpleDateFormat类
SimpleDateFormat类有什么用
SimpleDateFormat可以对Date对象和日期字符串,进行格式化和解析
如何使用
1.常用的模式字母及应对关系如下
HH: 24小时制,hh: 12小时制
- y 年
- M 月
- d 日
- H 时
- m 分
- s 秒
常用格式
2020-11-11 13:21:03-------- yyyy-MM-dd HH:mm:ss
2020年11月11日 13:21:03 --------- yyyy年MM月dd日 HH:mm:ss
2.SimpleDateFormat类构造方法
方法名 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
public SimpleDATe Format(String pattern) | 构造一个SimpleDateFormat使用给定的模式 |
public class MySimpleDateFormat {
public static void main(String[] args) {
// 用空参的构造方法创建一个simpleDateFormat对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
// 使用有参的构造方法创建一个simpleDateFormat1对象,并使用指定的模式进行初始化
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}
3.SimpleDateFormat类的常用方法
-
格式化(从Date类型到String类型)
public final String format(Date date):将日期格式化成日期/时间字符串
-
解析(从日期/时间字符串String到Date)
public Date parse (String source):从给定字符串的开始解析文本以生成日期
-
示例代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyFormat {
// 格式化(从Date类型到String类型)
// public final String format(Date date):将日期格式化成日期/时间字符串
// 解析(从日期/时间字符串String到Date)
// public Date parse (String source):从给定字符串的开始解析文本以生成日期
public static void main(String[] args) throws ParseException {
// 1. 格式化(从Date类型到String类型)
// 创建一个Date类对象,并初始化
Date date = new Date();
// 创建SmipleDteFormat类对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
// 因为就format方法返回值是String类型,所以我先创建一个String类型的对象接收一下数据
String dateStr = simpleDateFormat.format(date);
// 打印输出一下
System.out.println(dateStr);
// 2.解析(从日期/时间字符串String到Date)
String date1 = "2020-11-03 22:11:10";
// 这里使用的pattern(模式),要跟字符串中日期格式相同(yyyy-MM-dd HH:mm:ss),不然会报错
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 调用SimpleDateFormat类下的parse方法,将字符串解析为Date类型
Date date2 = simpleDateFormat1.parse(date1); // 这里会出现一个异常,暂时不用管,用throws ParseException抛出
System.out.println(date2);
}
}
小结
1.北京时间需要在世界标准时间上加8个小时
2.计算机中时间原点是 1970年1月1日 00:00:00
3.时间单位换算
1秒=1000毫秒
4.Date类概述
Date代表了一个特定时间,精确到毫秒
5.如何创建时间对象?
通过Date的构造方法 空参和带参数 空参构造方法创建的是当前系统时间,带参构造方法创建的是从时间原点经过的毫秒数
6.如何格式化和解析日期
格式化: 通过SimpleDateFormat,创建SimpleDateFormat类:使用对象调用format方法,参数是Date类型的数据,返回值是String类型数据
解析: 通过SimpleDateFormat的有参构造方法,创建SimpleDateFormat类,构造方法的参数是String类型日期数据的模式的字符串效果,类似于(yyyy-MM-dd HH:mm:ss):使用对象调用parse方法,参数是String类型日期数据,返回值是Date类型数据
时间日期类小练习
- 需求
某平台双11开启秒杀活动,开启时间是 2020年11月11日 00:00:00,结束时间是2020年11月11日 00:10:00,用户小龙下单时间是2020年11月11日 00:04:20,用户小光下单时间是2020年11月11日 00:10:06,判断两位用户有没有成功参加秒杀活动
- 代码实现
public class MyDateTest {
/*某平台双11开启秒杀活动,开启时间是 2020年11月11日 00:00:00,
结束时间是2020年11月11日 00:10:00,用户小龙下单时间是2020年11月11日 00:04:20,
用户小光下单时间是2020年11月11日 00:10:06,判断两位用户有没有成功参加秒杀活动*/
public static void main(String[] args) throws ParseException {
// 录入秒杀开始的时间,结束的时间,用户下单的时间
String star = "2020年11月11日 00:00:00";
String end = "2020年11月11日 00:10:00";
String xiaolong = "2020年11月11日 00:04:20";
String xiaoguang = "2020年11月11日 00:10:06";
// 创建SimpleDate的对象,模式要跟字符串日期模式一样
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
// 解析日期对象,将字符串日期的String改成Date形式,然后用getTime()转换成毫秒值,类型是long类型
long starDate = simpleDateFormat.parse(star).getTime();
long endDate = simpleDateFormat.parse(end).getTime();
long xiaolongDate = simpleDateFormat.parse(xiaolong).getTime();
long xiaoguangDate = simpleDateFormat.parse(xiaoguang).getTime();
// 使用if条件判断,小龙和小光是否在秒杀时间段内
if(xiaolongDate>=starDate&&xiaolongDate<=endDate){
System.out.println("小龙参与秒杀成功");
}else{
System.out.println("小龙参与秒杀失败");
}
if(xiaoguangDate>=starDate&&xiaoguangDate<=endDate){
System.out.println("小光参与秒杀成功");
}else{
System.out.println("小光参与秒杀失败");
}
}
}
JDK8时间日期类
JDK8时间类-获取时间对象
1.jdk中新增的时间类有哪些?
- LocalDate 表示日期(年月日)
- LocalTime 表示时间(时分秒)
- LocalDateTime 表示时间+日期 (年月日时分秒) (最常用)
2.如何创建LocalDateTime对象?
方法说明
方法名 | 说明 |
---|---|
public static LocalDateTime new() | 获取当前系统时间相当于new Date() |
public static LocalDateTime of(年,月,日,时,分,秒) | 使用指定年月日和时分秒初始化一个LocalDateTime对象 |
示例代码
public class MyLocalDateTime {
public static void main(String[] args) {
// 获取当前系统时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// 使用指定年月日和时分秒初始化一个LocalDateTime对象
LocalDateTime localDateTime1 = LocalDateTime.of(2011,11,11,11,11,11);
System.out.println(localDateTime1);
}
}
LocalDateTime获取方法
- 方法说明
方法名 | 说明 |
---|---|
public int getYear() | 获取年 |
public int getMonthValue() | 获取月份(1-12) |
public int getDayOfMonth | 获取月份中的第几天(1-31) |
public int getDayOfYear() | 获取一年中的第几天(1-366) |
public DayofWeek getDayOfWeek | 获取星期 |
public int getMinute() | 获取分钟 |
public int getHour() | 获取小时 |
public int getSecond() | 获取秒 |
- 获取代码
public class MySimpleDateFormatDemo2 {
public static void main(String[] args) {
// 创建一个对象,打印当前系统的时间
LocalDateTime localDateTime = LocalDateTime.now();
// 获取年,返回值是int
System.out.println(localDateTime.getYear());
// 获取月份,返回值是int
System.out.println(localDateTime.getMonthValue());
// 获取月份中的第几天(1-31)
System.out.println(localDateTime.getDayOfMonth());
// 获取星期,返回值是DayofWeek类型
System.out.println(localDateTime.getDayOfWeek());
// 获取分钟,返回值是int
System.out.println(localDateTime.getMinute());
// 获取小时,返回值是int
System.out.println(localDateTime.getHour());
// 获取秒,返回值是int
System.out.println(localDateTime.getSecond());
}
}
LocalDateTime转换方法
方法说明
方法名 | 说明 |
---|---|
public LocalDate toLocalDate | 转换成一个LocalDate对象 |
public LocalTime toLocalTime | 转换成为一个LocalTime对象 |
- 示例代码
public class MySimpleDateFormatDemo3 {
public static void main(String[] args) {
// 创建一个对象,获取当前系统的时间
LocalDateTime localDateTime = LocalDateTime.now();
// 把对象转换成LocalDate类型,并且打印输出
System.out.println(localDateTime.toLocalDate());
// 把当前对象转换成LocalTime,并且打印输出
System.out.println(localDateTime.toLocalTime());
}
}
JDK8时间类-格式化和解析
方法说明
方法名 | 说明 |
---|---|
public String format(指定格式) | 把一个LocalDateTime格式化称为一个字符串 |
public static LocalDateTime parse(准备解析的字符串,解析格式) | 把一个日期字符串解析成为一个LocalDateTime对象 |
public static DateTimeFormatter ofPattern(String pattern) | 使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象 |
- 示例代码
public class MySimpleDateFormatDemo4 {
/*
验证这三个方法
*public String format(指定格式)
*public static LocalDateTime parse(准备解析的字符串,解析格式)
*public static DateTimeFormatter ofPattern(String pattern)
*/
public static void main(String[] args) {
// 把一个字符串解析为一个LocalDateTime对象
String dateStr = "2020-11-11 12:11:01";
// 使用指定的日期模板获取一个日期格式化器
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 创建一个LocalDateTime对象,并用格式化器,解析字符串
// LocalDateTime.parse()的第一个参数可以是字符串,但是第二个参数必须是DateTimeFormatter类型
LocalDateTime localDateTime = LocalDateTime.parse(dateStr,dateTimeFormatter);
// 打印输出验证结果,\t只是为了加几个空格而已
System.out.println("这是LocalDateTime类型的时间"+"\t"+localDateTime);
// 用不同的方法创建时间对象
LocalDateTime localDateTime1=LocalDateTime.of(2011,11,11,11,11,11);
// 创建字符串类型的变量newDateStr,用来存储format格式化后的结果,format的参数是DateTimeFormatter类型
String newDateStr = localDateTime1.format(dateTimeFormatter);
// 打印输出结果,
System.out.println("这是字符串类型的时间"+"\t"+newDateStr);
}
}
jdk8时间类-plus
方法说明
方法名 | 说明 |
---|---|
public LocalDateTime plusYears(long years) | 添加年或者减去年(这个要看传入值是正是负) |
public LocalDateTime plusMonths(long months) | 添加或减去月 |
public LocalDateTime plusDays(long days) | 添加或减去日 |
public LocalDateTime plusHours(long hours) | 添加或减去时 |
public LocalDateTime plusMinutes(long minutes) | 添加或减去分 |
public LocalDateTime plusSeconds(long seconds) | 添加或减去秒 |
public LocalDateTime plusWeeks(long weeks) | 添加或者减去周 |
- 示例代码
public class MyLocalDateFormatPlus {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,12,1,12);
// 添加年
System.out.println(localDateTime.plusYears(4)); //2024-11-11T12:01:12
// 减去年
System.out.println(localDateTime.plusYears(-1)); //2019-11-11T12:01:12
// 添加日
System.out.println(localDateTime.plusDays(32)); // 2020-12-13T12:01:12
}
}
// 方法都一样,不重复实现了
jdk8时间类-minus
方法说明
方法名 | 说明 |
---|---|
public LocalDateTime | 减去或添加年(这个要看传入值是正是负) |
public LocalDateTime minusMonths(long months) | 减去或添加月 |
public LocalDateTime minusDays(long days) | 减去或添加日 |
public LocalDateTime minusHours(long hours) | 减去或添加时 |
public LocalDateTime minusMinutes(long minutes) | 减去或添加分 |
public LocalDateTime minusSeconds(long seconds) | 减去或添加秒 |
public LocalDateTime minusWeeks(long weeks) | 减去或添加周 |
- 示例代码
public class MyLocalDateFormatminus {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,12,1,12);
// 减去年
System.out.println(localDateTime.minusYears(4)); //2016-11-11T12:01:12
// 添加年
System.out.println(localDateTime.minusYears(-1)); //2021-11-11T12:01:12
// 减去日
System.out.println(localDateTime.minusDays(32)); // 2020-10-10T12:01:12
}
}
LocalDateTime修改方法
方法说明
方法名 | 说明 |
---|---|
public LocalDateTime withYear(int year) | 直接修改年 |
public LocalDateTime withMonth(int month) | 直接修改月 |
public LocalDateTime withDayOfMonth(int dayofmonth) | 直接修改日期(一个月中的第几天) |
public LocalDateTime withDayOfYear(int dayOfYear) | 直接修改日期(一年中的第几天) |
public LocalDateTime withHour(int hour) | 直接修改小时 |
public LocalDateTime withMinute(int minute) | 直接修改分钟 |
public LocalDateTime withSecond(int second) | 直接修改秒 |
- 示例代码
public class MyLocalDateTimeWith {
public static void main(String[] args) {
// 创建一个LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.of(2011,11,11,11,11,11);
// 直接修改年
System.out.println(localDateTime.withYear(2020)); //2020-11-11T11:11:11
// 直接修改月(月份的范围在1-12)
System.out.println(localDateTime.withMonth(12)); // 2011-12-11T11:11:11
// 修改年中的第几天
System.out.println(localDateTime.withDayOfYear(1)); // 一年中的第一天 2011-01-01T11:11:11
}
}
period
- 方法说明
方法名 | 说明 |
---|---|
public static Period between(开始时间,结束时间) | 计算两个时间的时间间隔 |
public int getYears() | 获得这段时间的年数 |
public int getMonths() | 获取此期间的月数 |
public int getDays() | 获得此期间的天数 |
public long toTotalMonths() | 获取此期间的总月数 |
- 示例代码
public class MyLocalDateTimePeriod {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2011, 11, 11, 11, 12, 12);
LocalDateTime localDateTime1 = LocalDateTime.of(2022,12,30,12,22,22);
// 因为between能接收的数据类型只能是LocalDate类型,所以要使用toLocalDate方法转换下数据类型
Period period = Period.between(localDateTime.toLocalDate(),localDateTime1.toLocalDate());
System.out.println(period); // P11Y1M19D 相差11Year 1 Month 19 Date
// getYears返回值是int类型
System.out.println(period.getYears()); // 11
// 获取相差几个月
System.out.println(period.getMonths()); // 1
// 获取期间总共相差了几个月,注意与getMonths的区别
System.out.println(period.toTotalMonths()); // 133
}
}
Duration
- 方法说明
方法名 | 说明 |
---|---|
public static Duration between(开始时间,结束时间) | 计算两个时间的间隔 |
public long toSeconds() | 获得此时间间隔内的秒 |
public int toMillis() | 获得此时间间隔内的毫秒 |
public int toNanos() | 获得此时间间隔的纳秒 |
- 示例代码
public class MyLocalDateTimeDuration {
public static void main(String[] args) {
// 创建两个对象
LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,11,11,11);
localDateTime.toLocalTime();
LocalDateTime localDateTime1 = LocalDateTime.of(2020,12,12,12,12,12);
localDateTime1.toLocalTime();
// 调用between方法计算时间间隔
Duration duration = Duration.between(localDateTime,localDateTime1);
System.out.println(duration); // PT745H1M1S 相差745小时,1分钟,1s
// to开头方法,是计算的总共相差多少
System.out.println(duration.toHours());
System.out.println(duration.toDays());
System.out.println(duration.toSeconds());
}
}
jdk8小结
- 创建时间对象(new,of)
- 获取时间对象中的年月日时分秒
- 格式化(Format)
- 解析(parse)
- 增加或减少时间的方法(plus开头,minus开头)
- 修改时间(with开头)
计算时间间隔的两个类
Period
Duration