Java中时间戳和时间的转换方法代码

 更新时间:2025年03月25日 08:28:44   作者:思思文  
这篇文章主要介绍了Java中时间戳和时间的转换的相关资料,Java8中时间戳与日期时间对象之间的转换是编程中常见的操作,通过时间字符串获取时间对象也是其中的一种方法,需要的朋友可以参考下

前言

不论是什么编程语言,无论是刚开始学习,还是做了多长时间的猿,都离不开时间戳和时间的转换。Java中也是这样,现在接触到最低的java版本是Java8.其中的时间

获取时间戳

//使用系统时间戳
long timestamp = System.currentTimeMillis();
System.out.println("timestamp:" + timestamp);
//>>timestamp:1733907943319

Long timestamp1 = Instant.now().toEpochMilli();
System.out.println("timestamp1:"+timestamp1);
//>>1733908000856

//获取秒级时间戳
long timestamp2 = System.currentTimeMillis() / 1000;
System.out.println("timestamp2:"+timestamp2);
//>>1733908113

long timestamp3 = Instant.now().getEpochSecond();
System.out.println("timestamp3:"+timestamp3);
//>>1733908113

日期时间对象转时间戳

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime:"+localDateTime);
//>>localDateTime:2024-12-11T17:10:45.800
//获取我们常见的时间格式
String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("dateTime:"+dateTime);
//>>dateTime:2023-04-07 14:06:53
//localDateTime获取时间戳
long timestamp4 = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println("timestamp4:"+timestamp4);
//>>1733908245800
long timestamp5 = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println("timestamp5:"+timestamp5);
//>> 1733908324740
long timestamp6 = ZonedDateTime.now().toInstant().toEpochMilli();
System.out.println("timestamp6:"+timestamp6);
//>>1733908374866
long timestamp7 = ZonedDateTime.now().toEpochSecond();
System.out.println("timestamp7:"+timestamp7);
//>>1733908374
long timestamp8 = ZonedDateTime.now().toLocalDateTime().toEpochSecond(ZoneOffset.of("+8"));
System.out.println("timestamp8:"+timestamp8);

时间戳转时间对象

long time = System.currentTimeMillis();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 System.out.println("time:"+time);
 // 将毫秒时间戳转换为 Instant 对象
Instant instant=Instant.ofEpochMilli(time);
System.out.println("instant:"+instant);
 // 将 Instant 对象转换为 LocalDateTime 对象,使用系统默认时区
LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println("localDateTime:"+localDateTime.format(formatter));
LocalDateTime localDateTime1 =LocalDateTime.ofEpochSecond(instant.getEpochSecond(),0, ZoneOffset.of("+8"));
System.out.println("localDateTime1:"+localDateTime1.format(formatter));

通过时间字符串获取时间对象

String dt = "2024-12-11";
String dtt = "2024-12-11 17:44:28";
String format_DateTime = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime);
LocalDateTime localDateTime = LocalDateTime.parse(dtt, df);
System.out.println("localDateTime:"+localDateTime);
//转为Instant
Instant instant = localDateTime.toInstant(ZoneOffset.of("+8"));
System.out.println("instant:"+instant);
System.out.println("毫秒级时间戳:"+instant.toEpochMilli());
System.out.println("秒级时间戳:"+instant.getEpochSecond());
LocalDate localDate = LocalDate.parse(dt);
System.out.println("localDate:"+localDate);
System.out.println("localDate.atStartOfDay():"+localDate.atStartOfDay().format(df));
//转为Instant
Instant instant1 = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8"));
System.out.println("毫秒级时间戳:"+instant.toEpochMilli());
System.out.println("秒级时间戳:"+instant.getEpochSecond());

以上就是java8中获取时间戳以及通过实践对象获取时间戳和通过时间戳获取时间对象。

总结

到此这篇关于Java中时间戳和时间的转换方法的文章就介绍到这了,更多相关Java时间戳和时间转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java反射机制(Reflection)浅析

    Java反射机制(Reflection)浅析

    这篇文章主要介绍了Java反射机制(Reflection)浅析,本文以实例讲解Java的反射机制,需要的朋友可以参考下
    2014-07-07
  • Spring中的PathVariable注释解析

    Spring中的PathVariable注释解析

    这篇文章主要介绍了Spring中的PathVariable注释用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Eclipse使用maven搭建spring mvc图文教程

    Eclipse使用maven搭建spring mvc图文教程

    这篇文章主要为大家分享了Eclipse使用maven搭建spring mvc图文教程,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • springboot连接kafka集群的使用示例

    springboot连接kafka集群的使用示例

    在项目中使用kafka的场景有很多,尤其是实时产生的数据流,本文主要介绍了springboot连接kafka集群的使用示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • 图解Java经典算法希尔排序的原理与实现

    图解Java经典算法希尔排序的原理与实现

    希尔排序是希尔(Donald Shell)于1959年提出的一种排序算法。希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序,同时该算法是冲破O(n2)的第一批算法之一。本文会以图解的方式详细介绍希尔排序的基本思想及其代码实现
    2022-09-09
  • java多线程编程技术详解和实例代码

    java多线程编程技术详解和实例代码

    这篇文章主要介绍了 java多线程编程技术详解和实例代码的相关资料,需要的朋友可以参考下
    2017-04-04
  • Java数据结构顺序表的详细讲解

    Java数据结构顺序表的详细讲解

    大家好,今天给大家带来的是顺序表,我觉得顺序表还是有比较难理解的地方的,于是我就把这一块的内容全部整理到了一起,希望能够给刚刚进行学习数据结构的人带来一些帮助,或者是已经学过这块的朋友们带来更深的理解,我们现在就开始吧
    2022-05-05
  • 详解Spring Retry实现原理

    详解Spring Retry实现原理

    这篇文章主要介绍了详解Spring Retry实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • spring boot整合flyway实现数据的动态维护的示例代码

    spring boot整合flyway实现数据的动态维护的示例代码

    本文主要介绍了spring boot整合flyway实现数据的动态维护的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-04-04
  • 关于Maven parent.relativePath说明

    关于Maven parent.relativePath说明

    Maven中的relativePath用于指定父项目pom.xml的相对路径,默认值为../pom.xml,这个配置帮助Maven在构建时定位父模块的位置,确保模块间的依赖关系正确,relativePath可以指向本地或远程仓库中的父项目,如果不需要寻找父项目,可以将其设置为空
    2024-09-09

最新评论