今天小编给大家分享一下怎么使用Java获取当前时间戳的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。要获取Java中的当前时间
今天小编给大家分享一下怎么使用Java获取当前时间戳的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
要获取Java中的当前时间戳:
Timestamp timestamp = new Timestamp(System.currentTimeMillis());//2016-11-16 06:43:19.77
这是两个Java示例,向您展示如何获取Java中的当前时间戳。 (使用Java 8更新)
获得当前java.sql.Timestamp
两种方法
TimeStampExample.java
package com.mkyong.date; import java.sql.Timestamp;import java.text.SimpleDateFORMat;import java.util.Date;public class TimeStampExample { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); public static void main(String[] args) { //method 1 Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); //method 2 - via Date Date date = new Date(); System.out.println(new Timestamp(date.getTime())); //return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println(timestamp.getTime()); //format timestamp System.out.println(sdf.format(timestamp)); }}
输出量
2016-11-16 06:43:19.77
2016-11-16 06:43:19.769
1479249799770
2016.11.16.06.43.19
在Java 8中,可以将java.sql.Timestamp
转换为新的java.time.Instant
InstantExample.java
package com.mkyong.date; import java.sql.Timestamp;import java.time.Instant;public class InstantExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); //return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println(timestamp.getTime()); // Convert timestamp to instant Instant instant = timestamp.toInstant(); System.out.println(instant); //return number of milliseconds since the epoch of 1970-01-01T00:00:00Z System.out.println(instant.toEpochMilli()); // Convert instant to timestamp Timestamp tsFromInstant = Timestamp.from(instant); System.out.println(tsFromInstant.getTime()); }}
输出量
2016-11-16 06:55:40.11
1479250540110
2016-11-15T22:55:40.110Z
1479250540110
1479250540110
补充:java获取当前时间戳的方法
获取当前时间戳
//方法 一System.currentTimeMillis();//方法 二Calendar.getInstance().getTimeInMillis();//方法 三new Date().getTime();
获取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
获取时间戳三种方法执行效率比较:
import java.util.Calendar;import java.util.Date; public class TimeTest { private static long _TEN_THOUSAND=10000; public static void main(String[] args) { long times=1000*_TEN_THOUSAND; long t1=System.currentTimeMillis(); testSystem(times); long t2=System.currentTimeMillis(); System.out.println(t2-t1); testCalander(times); long t3=System.currentTimeMillis(); System.out.println(t3-t2); testDate(times); long t4=System.currentTimeMillis(); System.out.println(t4-t3); } public static void testSystem(long times){//use 188 for(int i=0;i<times;i++){ long currentTime=System.currentTimeMillis(); } public static void testCalander(long times){//use 6299 long currentTime=Calendar.getInstance().getTimeInMillis(); public static void testDate(long times){ long currentTime=new Date().getTime();}
执行结果:
133
2372
137
Calendar.getInstance().getTimeInMillis() 这种方式速度最慢,这是因为Canlendar要处理时区问题会耗费较多的时间。
以上就是“怎么使用Java获取当前时间戳”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。
--结束END--
本文标题: 怎么使用Java获取当前时间戳
本文链接: https://www.lsjlt.com/news/321373.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0