教你使用Java获取当前时间戳的详细代码
作者:cyan20115 时间:2021-09-19 04:41:02
要获取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.sql.Timestamp JavaDoc
java.time.Instant JavaDoc
补充: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要处理时区问题会耗费较多的时间。
来源:https://blog.csdn.net/cyan20115/article/details/106553787
标签:Java,当前,时间戳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java C++ 算法题解leetcode1608特殊数组特征值
2023-05-21 21:09:01
![](https://img.aspxhome.com/file/2023/1/61121_0s.png)
SpringBoot持久化层操作支持技巧
2023-11-24 06:40:32
![](https://img.aspxhome.com/file/2023/3/59933_0s.png)
SpringBoot项目依赖和配置最新示例讲解
2021-08-13 23:04:23
MyBatis快速入门
2023-11-13 06:45:14
![](https://img.aspxhome.com/file/2023/5/62155_0s.png)
简单谈谈java自定义注解
2021-07-21 18:40:53
Java中的这些骚操作你不能不知道!!!
2022-07-08 12:28:14
![](https://img.aspxhome.com/file/2023/4/60574_0s.png)
详解idea maven nexus 常见命令配置
2021-06-07 18:29:03
![](https://img.aspxhome.com/file/2023/1/62731_0s.png)
MyBatis框架迭代器模式实现原理解析
2021-08-07 13:56:00
springboot2.x整合tkmapper的示例代码
2021-09-19 11:56:59
java多线程加锁以及Condition类的使用实例解析
2023-08-07 07:25:30
![](https://img.aspxhome.com/file/2023/3/57713_0s.png)
springboot实现配置两个parent的方法
2022-04-05 07:54:43
Spring Boot 在启动时进行配置文件加解密的方法详解
2023-11-12 17:13:54
关于Mybatis-Plus字段策略与数据库自动更新时间的一些问题
2023-08-05 20:44:22
深入了解Java ServletContext
2023-11-08 22:36:27
![](https://img.aspxhome.com/file/2023/7/59387_0s.png)
java实现支付宝支付接口的调用
2023-10-13 09:43:40
![](https://img.aspxhome.com/file/2023/1/58611_0s.jpg)
详细总结Java堆栈内存、堆外内存、零拷贝浅析与代码实现
2023-11-21 01:52:29
![](https://img.aspxhome.com/file/2023/5/59325_0s.png)
微信开发--自定义菜单查询返码乱码的解决方法
2023-11-25 04:47:55
Java由浅入深刨析继承
2023-11-23 08:04:24
![](https://img.aspxhome.com/file/2023/4/59564_0s.jpg)
spring如何动态指定具体实现类
2022-04-13 07:52:21
![](https://img.aspxhome.com/file/2023/9/61859_0s.jpg)
Java Stopwatch类,性能与时间计时器案例详解
2023-07-24 04:08:50