一.何为java时间戳
在java中,时间戳通常指的是自1970年1月1日午夜(utc
)以来的毫秒数。
这个概念在java中主要通过java.util.date
类和java.sql.timestamp
类来表示
而在java 8及以后的版本中,引入了新的日期时间api
,即java.time
包,提供了更多的功能和灵活性。
二.获取当前时间戳
1.使用system.currenttimemillis()方法
long currenttimestamp = system.currenttimemillis();
2. 使用java.util.date 类
java.util.date
类可以用来表示特定的瞬间,它包含了自1970年1月1日午夜(utc
)以来的毫秒数。
// 创建一个date对象表示当前时间 date now = new date(); long timestamp = now.gettime(); // 获取时间戳 // 从一个已知的时间戳创建date对象 long timestamp = ... // 假设你有一个时间戳 date date = new date(timestamp);
3. 使用java.sql.timestamp 类
java.sql.timestamp
类是java.util.date
的一个子类,它提供了更高精度的时间表示,包括纳秒
// 创建一个timestamp对象表示当前时间 timestamp now = new timestamp(system.currenttimemillis()); // 从一个已知的时间戳创建timestamp对象 long timestamp = ... // 假设你有一个时间戳 timestamp ts = new timestamp(timestamp);
4.java 8中的 java.time 包
java 8引入了一个新的日期时间api,提供了更好的处理日期和时间的类。其中,instant
类可以用来表示时间戳。
// 获取当前时间的instant对象 instant now = instant.now(); long timestamp = now.toepochmilli(); // 获取时间戳 // 从一个已知的时间戳创建instant对象 long timestamp = ... // 假设你有一个时间戳 instant instant = instant.ofepochmilli(timestamp);
三.格式化时间戳
要将时间戳转换为可读的日期时间格式,可以使用simpledateformat
(java 8之前)或datetimeformatter
(java 8及以后)。
1.使用 simpledateformat
// 假设你有一个时间戳 long timestamp = ... // 创建simpledateformat对象 simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); // 将时间戳转换为date对象,然后格式化 string formatteddate = sdf.format(new date(timestamp)); system.out.println(formatteddate);
2.使用 datetimeformatter
// 假设你有一个时间戳 long timestamp = ... // 创建datetimeformatter对象 datetimeformatter dtf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss"); // 将时间戳转换为instant对象,然后转换为localdatetime对象,最后格式化 instant instant = instant.ofepochmilli(timestamp); localdatetime ldt = localdatetime.ofinstant(instant, zoneid.systemdefault()); string formatteddate = ldt.format(dtf); system.out.println(formatteddate);
四.时间戳与字符串的转换
1.将时间戳转换为字符串
// 假设你有一个时间戳 long timestamp = ... // 使用simpledateformat simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string datestr = sdf.format(new date(timestamp)); // 使用datetimeformatter datetimeformatter dtf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss"); instant instant = instant.ofepochmilli(timestamp); localdatetime ldt = localdatetime.ofinstant(instant, zoneid.systemdefault()); string datestr = ldt.format(dtf);
2.将字符串转换为时间戳
// 假设你有一个日期时间的字符串表示 string datestr = "2023-04-01 12:34:56"; // 使用simpledateformat simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); try { date date = sdf.parse(datestr); long timestamp = date.gettime(); } catch (parseexception e) { e.printstacktrace(); } // 使用datetimeformatter datetimeformatter dtf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss"); localdatetime ldt = localdatetime.parse(datestr, dtf); instant instant = ldt.atzone(zoneid.systemdefault()).toinstant(); long timestamp = instant.toepochmilli();
五.时区处理
在处理时间戳时,时区是一个非常重要的因素。java中的zoneid
类可以用来表示不同的时区。
// 获取当前时区的偏移量 zoneid currentzone = zoneid.systemdefault(); zoneddatetime zdt = zoneddatetime.now(currentzone); long offsetinmillis = zdt.getoffset().gettotalseconds() * 1000;
六.时间戳的加减
要对时间戳进行加减操作,可以使用java.time.duration
类或直接对毫秒数进行加减。
// 假设你有一个时间戳 long timestamp = ... // 增加一小时的时间戳 instant instant = instant.ofepochmilli(timestamp); duration onehour = duration.ofhours(1); instant newinstant = instant.plus(onehour); long newtimestamp = newinstant.toepochmilli(); // 或者直接对毫秒数进行加减 long newtimestamp = timestamp + (60 * 60 * 1000); // 增加一小时
总结
到此这篇关于java中时间戳各种操作方法详解的文章就介绍到这了,更多相关java中时间戳内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论