教你使用Java获取当前时间戳的详细代码
要获取Java中的当前时间戳:
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); //2016-11-16 06:43:19.77
这是两个Java示例,向您展示如何获取Java中的当前时间戳。 (使用Java 8更新)
1. java.sql.Timestamp
获得当前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
2. java.time.Instant
在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获取当前时间戳的文章就介绍到这了,更多相关Java获取当前时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring boot使用@Async异步注解的实现原理+源码
通常我们都是采用多线程的方式来实现上述业务功能,但spring 提供更优雅的方式来实现上述功能,就是@Async 异步注解,在方法上添加@Async,spring就会借助AOP,异步执行方法,接下来通过本文给大家介绍spring boot异步注解的相关知识,一起看看吧2021-06-06
mybatis-plus 查询传入参数Map,返回List<Map>方式
这篇文章主要介绍了mybatis-plus 查询传入参数Map,返回List<Map>方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12
springboot中Controller内文件上传到本地及阿里云操作方法
这篇文章主要介绍了springboot中Controller内文件上传到本地及阿里云操作方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧2024-12-12
Springboot基于websocket实现简单在线聊天功能
这篇文章主要介绍了Springboot基于websocket实现简单在线聊天功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-06-06


最新评论