How to get current timestamps in Java
How to get current timestamps in Java
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//2016-11-16 06:43:19.77
Here are two Java examples to show you how to get current timestamps in Java. (Updated with Java 8)
1. java.sql.Timestamp
Two methods to get the current 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));
}
}
Output
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
In Java 8, you can convert java.sql.Timestamp to the new 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());
}
}
Output
2016-11-16 06:55:40.11
1479250540110
2016-11-15T22:55:40.110Z
1479250540110
1479250540110
http://www.mkyong.com/java/how-to-get-current-timestamps-in-java/
http://www.mkyong.com/tutorials/java-date-time-tutorials/
How to get current timestamps in Java的更多相关文章
- Java – How to add days to current date
1. Calendar.add Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- [ubunut]打造Ubuntu下Java开发环境 (转)
http://www.cnblogs.com/wufengtinghai/p/4542366.html 遇到困难: A Java Runtime Environment (JRE) or Java D ...
- Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap
概要 本章对Java.util.concurrent包中的ConcurrentSkipListMap类进行详细的介绍.内容包括:ConcurrentSkipListMap介绍ConcurrentSki ...
- java中多线程中Runnable接口和Thread类介绍
java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...
- Core Java Volume I — 1.2. The Java "White Paper" Buzzwords
1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...
- 【转】java获取当前路径的几种方法
1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...
- java获取当前路径的几种方法
1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...
- 单链表---java实现
单链表优点:1.不需要预先给出元素个数. 2.单链表插入删除时不需要移动数据元素. 单链表缺点:1.每个节点有指针,空间利用率低. 2.单链表不支持随机读取数据. Node.java package ...
随机推荐
- bootstrap自适应栅格系统布局
- 无法将 Ethernet0连接到虚拟网络 VMnet8 以及无法使用桥接的详细解决步骤
前言 首先我们需要明确如下表所示的对应关系: 网络类型 网络适配器名 Bridged VMnet0 NAT VMnet8 Host-only VMnet1 解决"无法将 ...
- switch omega - VNP
文件下载 安装插件 1.Chrome地址栏输入:chrome://extensions/ 2.将附件拖入窗口,安装
- windows快捷键补充?
win + + 放大镜 win + r osk 虚拟键盘 win + r psr 自带屏幕录制 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论
- SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现
上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1. 外部架包依赖引入 外部依赖包引入 ...
- http协议版本历史
1.http 0.9 2.http 1.0 3. http 1.1 4.http 2.0 推送:主动发送js.css推送到浏览器. 二进制流:可以并行发送数据. 2019.3.18补充: (1)htt ...
- 【leetcode】solution in java——Easy5
转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...
- 三种分布式对象主流技术——COM、Java和COBRA
既上一遍,看到还有一遍将关于 对象的, 分布式对象, 故摘抄入下: 目前国际上,分布式对象技术有三大流派——COBRA.COM/DCOM和Java.CORBA技术是最早出现的,1991年OMG颁布了C ...
- Hadoop之我见
最近在学习Hadoop,很想把自己的一些学习遇到的问题以及解决方案分享出来,也碍于最近一堆忙不完的事,就把这事给搁了很久. 那下面我就先来简单说下我为什么要学习Hadoop以及我学习Hadoop的一些 ...
- Dockerfile 构建google-chrom应用
cat Dockerfile.chrome FROM centos MAINTAINER zh******ech.cn ENV LANG en_US.UTF-8 RUN /bin/cp /usr/sh ...