JDK8时间相关类超详细总结 2(含多个实例)
创始人
2024-05-12 03:52:51
0

一、带时区的时间

1.获取当前时间对象(带时区)

import java.time.ZonedDateTime;
public class demo1 {public static void main(String[] args) {ZonedDateTime now = ZonedDateTime.now();System.out.println(now);}
}

2023-01-13T19:24:18.389+08:00[Asia/Shanghai]

2.获取指定的时间对象(带时区)1/年月日时分秒纳秒方式指定

import java.time.Instant;
public class demo1 {public static void main(String[] args) {ZonedDateTime time1 = ZonedDateTime.of(2023, 1, 1, 8, 30, 0, 0, ZoneId.of("Asia/Shanghai"));System.out.println(time1);}
}

2023-01-01T08:30+08:00[Asia/Shanghai]

3.通过Instant + 时区的方式指定获取时间对象

import java.time.Instant;
public class demo1 {public static void main(String[] args) {Instant instant = Instant.ofEpochMilli(0L);ZoneId zoneId = ZoneId.of("Asia/Shanghai");ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);System.out.println(time2);}
}

1970-01-01T08:00+08:00[Asia/Shanghai]

4.修改时间

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class Demo8 {public static void main(String[] args) {Instant instant = Instant.ofEpochMilli(0L);ZoneId zoneId = ZoneId.of("Asia/Shanghai");ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);ZonedDateTime time3 = time2.withYear(2000);System.out.println(time3);ZonedDateTime time4 = time3.minusYears(1);System.out.println(time4);ZonedDateTime time5 = time4.plusYears(1);System.out.println(time5);}
}

2000-01-01T08:00+08:00[Asia/Shanghai]
1999-01-01T08:00+08:00[Asia/Shanghai]
2000-01-01T08:00+08:00[Asia/Shanghai]

二、DateTimeFormatter

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
//获取时间对象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));// 解析/格式化器
DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a");
// 格式化
System.out.println(dtf1.format(time));

2023-01-14 23:55;55 星期六 下午

三、LocalDate

1. 获取当前时间的日历对象(包含年月日)

//1.获取当前时间的日历对象(包含 年月日)
LocalDate nowDate = LocalDate.now();
//System.out.println("今天的日期:" + nowDate);

2.获取指定的时间的日历对象

LocalDate ldDate = LocalDate.of(2023, 1, 1);
System.out.println("指定日期:" + ldDate);System.out.println("=============================");//3.get系列方法获取日历中的每一个属性值//获取年
int year = ldDate.getYear();
System.out.println("year: " + year);
//获取月//方式一:
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());//方式二:
int month = ldDate.getMonthValue();
System.out.println("month: " + month);//获取日
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);//获取一年的第几天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);//获取星期
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());//is开头的方法表示判断
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));//with开头的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);//minus开头的方法表示减少,只能减少年月日
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);//plus开头的方法表示增加,只能增加年月日
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println(plusLocalDate);

四、LocalTime

1.获取本地时间的日历对象(包含时分秒)


LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);int hour = nowTime.getHour();//时
System.out.println("hour: " + hour);int minute = nowTime.getMinute();//分
System.out.println("minute: " + minute);int second = nowTime.getSecond();//秒
System.out.println("second:" + second);int nano = nowTime.getNano();//纳秒
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));//时分
System.out.println(LocalTime.of(8, 20, 30));//时分秒
System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
LocalTime mTime = LocalTime.of(8, 20, 30, 150);

2.is系列的方法

System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));

3.with系列的方法

这个系列的方法有局限性,只能修改时、分、秒

System.out.println(nowTime.withHour(10));

4.plus系列的方法

这个系列的方法有局限性,只能修改时、分、秒

System.out.println(nowTime.plusHours(10));

五、LocalDateTime

1.当前时间的的日历对象(包含年月日时分秒)


LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime);//今天是:
System.out.println(nowDateTime.getYear());//年
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//时
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//纳秒

2.获取日:当年的第几天

System.out.println("dayofYear:" + nowDateTime.getDayOfYear());

3.获取星期

System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());

4.获取月份

System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());

六、结语

还有一部分会在下一篇文章中讲述

相关内容

热门资讯

春到福来春晚主持词 春到福来春晚主持词  节目:《春到福来》  朱军:春到福来,春上春伦云天外  周涛:春到福来,春向黄...
平安夜晚会主持词 平安夜晚会主持词  主持词是主持人在节目进行过程中用于串联节目的串联词。在一步步向前发展的社会中,司...
农村简单结婚典礼主持词 农村简单结婚典礼主持词  一、结婚典礼的内容简介  在世界各国大部分的文化里,会发展出一些结婚上的传...
大话西游最经典的台词 大话西游最经典的台词  大话西游是周星驰主演的一部经典的喜剧爱情片。里面的台词曾感染了无数观众。以下...
公司会议主持词 关于公司会议主持词(通用5篇)  主持词要把握好吸引观众、导入主题、创设情境等环节以吸引观众。在如今...
生日派对会的主持串词 关于生日派对会的主持串词  作者:赵可心  题目:我非常高兴  要求:用普通话  环节:  1、 开...
少先队员宣誓主持词 少先队员宣誓主持词(精选8篇)  主持词已成为各种演出活动和集会中不可或缺的一部分。我们眼下的社会,...
圣诞节活动主持词 圣诞节活动主持词(精选14篇)  主持人在台上表演的灵魂就表现在主持词中。在当今中国社会,主持词在各...
葬礼主持词 葬礼主持词(精选8篇)  主持词可以采用和历史文化有关的表述方法去写作以提升活动的文化内涵。在一步步...
主持词 主持词范文(精选21篇)  主持词需要富有情感,充满热情,才能有效地吸引到观众。在如今这个时代,活动...
六一儿童节颁奖主持词 六一儿童节颁奖主持词范文(通用5篇)  主持词分为会议主持词、晚会主持词、活动主持词、婚庆主持词等。...
业主在开工典礼的致辞 业主在开工典礼的致辞范文(精选12篇)  无论在学习、工作或是生活中,大家肯定对各类致辞都很熟悉吧,...
六一儿童节主持词 六一儿童节主持词(精选15篇)  契合现场环境的主持词能给集会带来双倍的效果。在当下的社会中,很多晚...
圣诞节主持词开场白   圣诞节(Christmas)又称耶诞节,译名为“基督弥撒”,西方传统节日,在每年12月25日。下...
黑龙江年度经济风云人物颁奖典... 黑龙江年度经济风云人物颁奖典礼的主持词  (灯光,音乐)  甲:各位领导  乙:各位来宾  丙:现场...
工会大会流程及主持词 工会大会流程及主持词  篇一:工会主持词  ……市……委工会成立大会  主 持 词  各位领导,会员...
电影大话西游经典台词 电影大话西游经典台词  如果你看星爷的大话西游觉得好玩有趣充满了幽默感,实话说你并没有看懂过。当你想...
五一晚会主持词 五一晚会主持词(通用10篇)  主持词的写作要突出活动的主旨并贯穿始终。在如今这个时代,我们对主持词...
笑傲江湖周云鹏的脱口秀台词 笑傲江湖周云鹏的脱口秀台词  地球人都是中国有个喜剧之王周星驰,而周云鹏才是脱口秀中的剧场之王!看过...
yy活动主持词 yy活动主持词  女:亲爱的朋友们!大家晚会好!我是今晚的主持人XXX,欢迎您走进“鹊桥会”——丫丫...