常用类:
ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
ZoneId zoneId=ZoneId.systemDefault();
System.out.println(zoneId);
Asia/Shanghai
Instant: 用来表示时间线上的一个点(瞬时)
Instant instant=Instant.now();
System.out.println(instant.getEpochSecond());
System.out.println(System.currentTimeMillis());
1609750218(以秒为单位输出)
1609750218026(以毫秒为单位输出)
Clock: 用于访问当前时刻、日期、时间,用到时区
Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
Period: 用于计算两个“日期”间隔
LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的
LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的
LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的
以上的内容实际上还是以下边的加粗的这三个为主题进行操作的。
使用方法:
- 获取当前的时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
2021-01-04
16:41:08.547
2021-01-04T16:41:08.547
2.LocalDateTime类型转换为String类型
LocalDateTime localDateTime=LocalDateTime.now();
System.out.println("为转换之前的时间为:"+localDateTime.toString());
DateTimeFormatter dateTimeFormat= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormat.format(localDateTime);
System.out.println("转换为String类型之后的时间为:"+format);
输出:
为转换之前的时间为:2021-01-04T16:50:18.017
转换为String类型之后的时间为:2021-01-04 16:50:18
3.根据指定日期/时间创建对象(of()方法的使用)
LocalDate localDate = LocalDate.of(2018, 1, 13);
LocalTime localTime = LocalTime.of(9, 43, 20);
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
2021-01-04
17:35
2021-01-04T17:35:20
4.日期时间的加减
对于LocalDate,只有精度大于或等于日的加减,如年、月、日;
对于LocalTime,只有精度小于或等于时的加减,如时、分、秒、纳秒;
对于LocalDateTime,则可以进行任意精度的时间相加减;
LocalDateTime localDateTime = LocalDateTime.now();
//以下方法的参数都是long型,返回值都是LocalDateTime
LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
LocalDateTime plusMonthsResult = localDateTime.plusMonths(3L);
LocalDateTime plusDaysResult = localDateTime.plusDays(7L);
LocalDateTime plusHoursResult = localDateTime.plusHours(2L);
LocalDateTime plusMinutesResult = localDateTime.plusMinutes(10L);
LocalDateTime plusSecondsResult = localDateTime.plusSeconds(10L);
System.out.println("当前时间是 : " + localDateTime + "\n"
+ "当前时间加2年后为 : " + plusYearsResult + "\n"
+ "当前时间加3个月后为 : " + plusMonthsResult + "\n"
+ "当前时间加7日后为 : " + plusDaysResult + "\n"
+ "当前时间加2小时后为 : " + plusHoursResult + "\n"
+ "当前时间加10分钟后为 : " + plusMinutesResult + "\n"
+ "当前时间加10秒后为 : " + plusSecondsResult + "\n"
);
//也可以以另一种方式来相加减日期,即plus(long amountToAdd, TemporalUnit unit)
// 参数1 : 相加的数量, 参数2 : 相加的单位
LocalDateTime nextMonth = localDateTime.plus(1, ChronoUnit.MONTHS);
LocalDateTime nextYear = localDateTime.plus(1, ChronoUnit.YEARS);
LocalDateTime nextWeek = localDateTime.plus(1, ChronoUnit.WEEKS);
System.out.println("now : " + localDateTime + "\n"
+ "nextYear : " + nextYear + "\n"
+ "nextMonth : " + nextMonth + "\n"
+ "nextWeek :" + nextWeek + "\n"
);
//日期的减法用法一样,在此不再举例
当前时间是 : 2021-01-04T17:40:32.217
当前时间加2年后为 : 2023-01-04T17:40:32.217
当前时间加3个月后为 : 2021-04-04T17:40:32.217
当前时间加7日后为 : 2021-01-11T17:40:32.217
当前时间加2小时后为 : 2021-01-04T19:40:32.217
当前时间加10分钟后为 : 2021-01-04T17:50:32.217
当前时间加10秒后为 : 2021-01-04T17:40:42.217
now : 2021-01-04T17:40:32.217
nextYear : 2022-01-04T17:40:32.217
nextMonth : 2021-02-04T17:40:32.217
nextWeek :2021-01-11T17:40:32.217
5.获取日期的年月日周时分秒
LocalDateTime localDateTime = LocalDateTime.now();
int dayOfYear = localDateTime.getDayOfYear();
int dayOfMonth = localDateTime.getDayOfMonth();
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("今天是" + localDateTime + "\n"
+ "本年当中第" + dayOfYear + "天" + "\n"
+ "本月当中第" + dayOfMonth + "天" + "\n"
+ "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n");
//获取当天时间的年月日时分秒
int year = localDateTime.getYear();
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
System.out.println("今天是" + localDateTime + "\n"
+ "年 : " + year + "\n"
+ "月 : " + month.getValue() + "-即 "+ month + "\n"
+ "日 : " + day + "\n"
+ "时 : " + hour + "\n"
+ "分 : " + minute + "\n"
+ "秒 : " + second + "\n"
);
今天是2021-01-04T17:42:33.989
本年当中第4天
本月当中第4天
本周中星期1-即MONDAY
今天是2021-01-04T17:42:33.989
年 : 2021
月 : 1-即 JANUARY
日 : 4
时 : 17
分 : 42
秒 : 33
6.判断是否为闰年
LocalDate now = LocalDate.now();
System.out.println("now : " + now + ", is leap year ? ");
System.out.println("This year is " + now.getYear()+"\n"
+ (now.getYear() % 4 == 0 || now.getYear() % 100 == 0 ?
"and yes,The year is a leap year" :
"and The year is not a leap year"));
结果:
now : 2021-01-04, is leap year ?
This year is 2021
and The year is not a leap year
7.计算时间、日期间隔
//计算两个日期的日期间隔-年月日
LocalDate date1 = LocalDate.of(2018, 2, 13);
LocalDate date2 = LocalDate.of(2017, 3, 12);
//内部是用date2-date1,所以得到的结果是负数
Period period = Period.between(date1, date2);
System.out.println("相差年数 : " + period.getYears());
System.out.println("相差月数 : " + period.getMonths());
System.out.println("相差日数 : " + period.getDays());
//还可以这样获取相差的年月日
System.out.println("-------------------------------");
long years = period.get(ChronoUnit.YEARS);
long months = period.get(ChronoUnit.MONTHS);
long days = period.get(ChronoUnit.DAYS);
System.out.println("相差的年月日分别为 : " + years + "," + months + "," + days);
//注意,当获取两个日期的间隔时,并不是单纯的年月日对应的数字相加减,而是会先算出具体差多少天,在折算成相差几年几月几日的
//计算两个时间的间隔
System.out.println("-------------------------------");
LocalDateTime date3 = LocalDateTime.now();
LocalDateTime date4 = LocalDateTime.of(2008, 1, 13, 22, 30, 10);
Duration duration = Duration.between(date3, date4);
System.out.println(date3 + " 与 " + date4 + " 间隔 " + "\n"
+ " 天 :" + duration.toDays() + "\n"
+ " 时 :" + duration.toHours() + "\n"
+ " 分 :" + duration.toMinutes() + "\n"
+ " 毫秒 :" + duration.toMillis() + "\n"
+ " 纳秒 :" + duration.toNanos() + "\n"
);
//注意,并没有获得秒差的,但既然可以获得毫秒,秒就可以自行获取了
相差年数 : 0
相差月数 : -11
相差日数 : -1
-------------------------------
相差的年月日分别为 : 0,-11,-1
-------------------------------
2021-01-04T17:58:27.673 与 2008-01-13T22:30:10 间隔
天 :-4739
时 :-113755
分 :-6825328
毫秒 :-409519697673
纳秒 :-409519697673000000
8.两个时间点之间的间隔
Instant ins1 = Instant.now();
long k = 0;
for (long i = 0; i < 100000000; i++) {
//循环一百万次
k++;
}
Instant ins2 = Instant.now();
System.out.println(k);
Duration duration = Duration.between(ins1, ins2);
System.out.println("程序运行耗时为 : " + duration.toMillis() + "毫秒");
100000000
程序运行耗时为 : 28毫秒
9.时间戳转换为时间格式
System.out.println("---------long毫秒值转换为日期---------");
DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String longToDateTime = df.format(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(
System.currentTimeMillis()),
ZoneId.of("Asia/Shanghai"))
);
System.out.println(longToDateTime);
---------long毫秒值转换为日期---------
2021-01-04 18:05:26
接下来的是时间格式之间的转换,下一章进行总结
以上内容都是小七自己学习工作总结出来的,有不足之处,请各位看官批评指出,我将及时改正,以提高知识总结的正确性和严谨性,为大家学习提供方便!!! 如若转载,请注明出处!!!