JAVA基础知识之Time总结

汤太咸

您有任何问题或意见都可以在评论区回复哦,欢迎大家一起来讨论,共同学习进步

新时间类型LocalDateTime,OffsetDateTime,LocalDate

//截取到天,小时
System.out.println(LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
System.out.println(LocalDateTime.now().truncatedTo(ChronoUnit.HOURS));
//输出
2020-05-06T00:00
2020-05-06T13:00
//设置小时为14点
System.out.println(LocalDateTime.now().withHour(14));
//减去12天
System.out.println(LocalDateTime.now().minusDays(12));
//增加12小时
System.out.println(LocalDateTime.now().plusHours(12));

//设定0时区时间,截取到整日,设定12点
OffsetDateTime.now(ZoneOffset.of("+00:00")).truncatedTo(ChronoUnit.DAYS).withHour(12)
//获取上海时区的时间
OffsetDateTime.now(ZoneId.of("Asia/Shanghai"))

//实间转换
LocalDate loc = LocalDate.parse("2020-01-02");
System.out.println(loc);
DateTimeFormatter usDateformatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
System.out.println(usDateformatter.format(loc));
//输出
2020-01-02
01-02-2020
//当parse的格式不是本地时间格式是,需要通过对应的formatter格式
LocalDate newDate = LocalDate.parse("07-04-2020",usDateformatter);
System.out.println(newDate);
//输出
2020-07-04

旧版本time类型Calendar,Date

Calendar cal = Calendar.getInstance();
cal.set(2020,1,26);
Date date = cal.getTime();
System.out.println(date);
//输出
Wed Feb 26 22:01:51 CST 2020
cal.add(Calendar.DAY_OF_MONTH,7);
System.out.println(cal.getTime());
//输出
Wed Mar 04 22:01:51 CST 2020

新Time类型Instant是不可改变的

System.out.println(Instant.MIN);
System.out.println(Instant.now());
System.out.println(Instant.MAX);
//输出
-1000000000-01-01T00:00:00Z
2020-05-17T14:10:23.739Z
+1000000000-12-31T23:59:59.999999999Z

Duration,包括toDays(),toHours(),toMinutes(),toNanos()等方法,minus()减去,plus增加,multipliedBy乘以

Instant start = Instant.now();
Thread.sleep(1);
Instant end = Instant.now();
Duration duration = Duration.between(start,end);
long millionSeconds = duration.toMillis();
System.out.println(millionSeconds);
//输出
1

LocalDate

TemporalAdjusters包括的方法:firstDayOfMonth,lastDayOfMonth,firstDayOfYear,lastDayOfYear,firstDayOfNextMonth,firstDayOfNextYear
firstInMonth(DayOfWeek.SUNDAY,lastInMonth(DayOfWeek.SUNDAY),dayOfWeekInMonth(2,DayOfWeek.SUNDAY)
next(DayOfWeek.SUNDAY),nextOrSame(DayOfWeek.SUNDAY),previous(DayOfWeek.SUNDAY),previousOrSame(DayOfWeek.SUNDAY)

LocalDate now = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(1987, Month.NOVEMBER, 23);
System.out.println(dateOfBirth);
System.out.println(now);
//输出
1987-11-23
2020-05-18
Period p = dateOfBirth.until(now);
System.out.println("Years:"+p.getYears());
//输出
Years:32
Long days = dateOfBirth.until(now,ChronoUnit.DAYS);
System.out.println("Days:"+days);
//输出
Days:11865
//其中的month差距只是余数,不是和上面的一样共计多少个月,而是除去整年后差的月数
System.out.println(p.get(ChronoUnit.YEARS));
System.out.println(p.get(ChronoUnit.MONTHS));
//输出
32
5
LocalDate nextSunday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(nextSunday);
//输出
2020-05-24

LocalTime

LocalTime localTime = LocalTime.now();
System.out.println(localTime);
System.out.println(localTime.plusHours(8));
System.out.println(LocalTime.of(20,10));
//输出
20:28:59.823
04:30:25.367
20:10

ZonedDateTime

Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
for(String zoneId:allZoneIds){
    if(zoneId.contains("America")){
        System.out.println(zoneId);
    }
}
//输出很多暂时忽略
ZoneId ukTZ = ZoneId.of("Asia/Shanghai");
System.out.println(ukTZ);
ZonedDateTime currentMetting = ZonedDateTime.of(LocalDate.of(2020,Month.JUNE,18),
        LocalTime.of(10,0),ZoneId.of("Asia/Shanghai"));
ZonedDateTime nextMetting = currentMetting.plus(Period.ofMonths(1));
ZonedDateTime nextUsMeeting = nextMetting.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(currentMetting);
System.out.println(nextMetting);
System.out.println(nextUsMeeting);
//输出
2020-06-18T10:00+08:00[Asia/Shanghai]
2020-07-18T10:00+08:00[Asia/Shanghai]
2020-07-17T22:00-04:00[America/New_York]

DateTimeFormatter

System.out.println("ISO_DATE_TIME="+DateTimeFormatter.ISO_DATE_TIME.format(currentMetting));
System.out.println("RFC_1123_DATE_TIME="+DateTimeFormatter.RFC_1123_DATE_TIME.format(currentMetting));
//输出
ISO_DATE_TIME=2020-06-18T10:00:00+08:00[Asia/Shanghai]
RFC_1123_DATE_TIME=Thu, 18 Jun 2020 10:00:00 +0800

老版本时间和新版本时间转换

Instant instant = Instant.now();
Date date = Date.from(instant);
instant = date.toInstant();

Timestamp timestamp = Timestamp.from(instant);
instant = timestamp.toInstant();
阅读 248

程序员一枚,也爱读书,也爱理财,还喜欢把学到的读到的总结出来写点东西,写的不难,希望大家喜欢。

3 声望
0 粉丝
0 条评论

程序员一枚,也爱读书,也爱理财,还喜欢把学到的读到的总结出来写点东西,写的不难,希望大家喜欢。

3 声望
0 粉丝
文章目录
宣传栏