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的更多相关文章

  1. 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 ...

  2. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  3. [ubunut]打造Ubuntu下Java开发环境 (转)

    http://www.cnblogs.com/wufengtinghai/p/4542366.html 遇到困难: A Java Runtime Environment (JRE) or Java D ...

  4. Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListMap类进行详细的介绍.内容包括:ConcurrentSkipListMap介绍ConcurrentSki ...

  5. java中多线程中Runnable接口和Thread类介绍

    java中的线程时通过调用操作系统底层的线程来实现线程的功能的. 先看如下代码,并写出输出结果. // 请问输出结果是什么? public static void main(String[] args ...

  6. 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 ...

  7. 【转】java获取当前路径的几种方法

    1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...

  8. java获取当前路径的几种方法

    1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...

  9. 单链表---java实现

    单链表优点:1.不需要预先给出元素个数. 2.单链表插入删除时不需要移动数据元素. 单链表缺点:1.每个节点有指针,空间利用率低. 2.单链表不支持随机读取数据. Node.java package ...

随机推荐

  1. mybatis+spring配置

    可参考:http://www.javacodegeeks.com/2014/02/building-java-web-application-using-mybatis-with-spring.htm ...

  2. 安装MySQL-python报错:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory或者 build\lib.win-amd64-2.7\_mysql.pyd : fatal error LNK1120: 56 unresolved externa

    解决办法1: 直接下载MySQL-python-1.2.5.win-amd64/32-py2.7.exe,点击安装 此处要注意自己安装的Python和MySQL版本是64位还是32位,否则在进行安装M ...

  3. Java多线程中的内存模型

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6536131.html  一:现代计算机的高速缓存 在计算机组成原理中讲到,现代计算机为了匹配 计算机存储设备的 ...

  4. The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

    CurrentStock = db.BillEntry.Where(b => b.GoodsId == item.GoodsId).Sum(b => (decimal?)b.Qty) ?? ...

  5. 转 linux下php扩展mysqli的支持

    前两天一个站点转移过来,因为给我的数据库有问题,我也没有仔细处理这个站点.今天把数据库弄好了,发现还是用不了,报的错误如下:Fatal error: Class ‘mysqli’ not found ...

  6. 微服务(Microservice)那点事

    WHAT - 什么是微服务 微服务简介 这次参加JavaOne2015最大的困难就是听Microservice相关的session,无论内容多么水,只要题目带microservice,必定报不上名,可 ...

  7. Git/Github的使用以及与Eclipse的整合

    Git简介       Git是一个免费的.分布式的版本控制工具,或是一个强调了速度快的源代码管理工具.每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖于网络 ...

  8. Linux-socket的close和shutdown区别及应用场景

    shutdown的定义 #include<sys/socket.h> int shutdown(int sockfd,int how); how的方式有三种分别是: SHUT_RD(0): ...

  9. Linux-LVS为何不能完全替代DNS轮询

    转自:链接 上一篇文章“一分钟了解负载均衡的一切”引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepalive ...

  10. Web-URL编码流程图(详细了解乱码原因)

    URL编码流程图 原URL---->GET时浏览器根据HTTP头的Content-Type的charset,POST根据(<meta http-equiv="Content-Ty ...