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. Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists

    Add a Column to a table if not exists MySQL allows you to create a table if it does not exist, but d ...

  2. Android 手把手带你玩转自己定义相机

    本文已授权微信公众号<鸿洋>原创首发,转载请务必注明出处. 概述 相机差点儿是每一个APP都要用到的功能,万一老板让你定制相机方不方?反正我是有点方. 关于相机的两天奋斗总结免费送给你. ...

  3. ubuntu 下 cajview 替代方案

    .caj 是知网提供的论文的标准格式,但是,知网只提供 win 版的工具.这里尝试了两个两个方案,均可行,做一下记录在此. 1. 使用 wine 版的 cajview  pdf 可以正常用,但是 ca ...

  4. Hibernate学习笔记五:反向工程快速开发

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6768513.html  一:反向工程 Myeclipse提供由 数据库表 生成 java pojo 和 hib ...

  5. 原生Ajax使用教程

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6682564.html    浏览器端 一:创建XMLHttpRequest对象       所有现代浏览器均支 ...

  6. 腾讯云ubuntu搭建tomcat

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6377945.html 一:工具准备 Putty+Xftp5,见上一篇博文:http://www.cnblogs ...

  7. Dlib三维点云示例

    Dlib三维点云示例 源代码来自Dlib的示例代码http://dlib.net/3d_point_cloud_ex.cpp.html 在windows下需要链接winmm``comctl32``gd ...

  8. Docker修改默认存储位置(转)

    方法一.软链接 默认情况下Docker的存放位置为:/var/lib/docker 可以通过下面命令查看具体位置: sudo docker info | grep "Docker Root ...

  9. Zuul Timeouts

    19.13 Zuul Timeouts 19.13.1 Service Discovery Configuration If Zuul is using service discovery there ...

  10. java中pojo、dao命名解释

    POJO::POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJB ...