JDK8时间相关类超详细总结(含多个实例)

 更新时间:2023年01月19日 10:59:49   作者:Alita11101_  
jdk1.8的一些新特性简化了代码的写法,减少了部分开发量,下面这篇文章主要给大家介绍了关于JDK8时间相关类超详细总结,文中包含了多个实例代码,需要的朋友可以参考下

一、带时区的时间

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());

六、结语

到此这篇关于JDK8时间相关类超详细总结的文章就介绍到这了,更多相关JDK8时间相关类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java实现多线程聊天室

    Java实现多线程聊天室

    这篇文章主要为大家详细介绍了Java实现多线程聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • MyBatis Puls统一封装前端传递的分页排序实例

    MyBatis Puls统一封装前端传递的分页排序实例

    这篇文章主要为大家介绍了MyBatis Puls统一封装前端传递的分页排序实现实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java数据类型转换的示例详解

    Java数据类型转换的示例详解

    Java程序中要求参与的计算的数据,必须要保证数据类型的一致性,如果数据类型不一致将发生类型的转换。本文将通过示例详细说说Java中数据类型的转换,感兴趣的可以了解一下
    2022-10-10
  • 深入解析Spring Boot 的SPI机制详情

    深入解析Spring Boot 的SPI机制详情

    这篇文章主要介绍了深入解析Spring Boot的SPI机制详情,SPI是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要用于框架中开发,更多相关介绍,感兴趣的小伙伴可以参考一下下面文章内容
    2022-08-08
  • java正则表达式表单验证类工具类(验证邮箱、手机号码、qq号码等)

    java正则表达式表单验证类工具类(验证邮箱、手机号码、qq号码等)

    这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱、手机号码、qq号码等方法,需要的朋友可以参考下
    2014-04-04
  • 基于Spring框架由ConditionalOnMissingBean注解引发的问题

    基于Spring框架由ConditionalOnMissingBean注解引发的问题

    这篇文章主要介绍了基于Spring框架由ConditionalOnMissingBean注解引发的问题,具有很好
    2023-11-11
  • JAVA Springboot配置i18n国际化语言详细步骤

    JAVA Springboot配置i18n国际化语言详细步骤

    国际化(Internationalization,缩写为i18n)是指根据来展示不同的内容,使应用程序能够适应不同的语言和文化习惯,下面这篇文章主要给大家介绍了关于JAVA Springboot配置i18n国际化语言的详细步骤,需要的朋友可以参考下
    2024-08-08
  • Spring+Quartz配置定时任务实现代码

    Spring+Quartz配置定时任务实现代码

    这篇文章主要介绍了Spring+Quartz配置定时任务实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Spring Security OAuth2实现使用JWT的示例代码

    Spring Security OAuth2实现使用JWT的示例代码

    这篇文章主要介绍了Spring Security OAuth2实现使用JWT的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 基于Maven的pom.xml文件详解

    基于Maven的pom.xml文件详解

    下面小编就为大家带来一篇基于Maven的pom.xml文件详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08

最新评论