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. 微信小程序 - 组件传值给调用页面

    演示如下(可以ctrl+鼠标放大或者下载GIF图片) 1. 先组件定义事件“checkbox”(组件内事件) 2. 调用页面调用它需要加bind:事件名 = 调用页面事件名(bind即绑定调用组件“o ...

  2. 初识:JMX

    来自:http://blog.csdn.net/derekjiang/article/details/4531952 JMX是一份规范,SUN依据这个规范在JDK(1.3.1.4.5.0)提供了JMX ...

  3. C++库研究笔记--用__attribute__((deprecated)) 管理过时代码

    用__attribute__((deprecated)) 管理过时代码.同一时候保留兼容的接口 Linux下: #define DEPR_AFTER __attribute__((deprecated ...

  4. IOS-异常处理

    http://blog.csdn.net/ndscoahz/article/details/50866229 http://www.cnblogs.com/snail-007/p/4564422.ht ...

  5. col-xs , col-sm , col-md , col-lg是什么意思?什么时候用?

    .col-xs- 超小屏幕 手机 (<768px) .col-sm- 小屏幕 平板 (≥768px) .col-md- 中等屏幕 桌面显示器 (≥992px) .col-lg- 大屏幕 大桌面显 ...

  6. 要想找出以“y”结尾的名字

    要想找出以“y”结尾的名字:mysql> SELECT * FROM pet WHERE name LIKE '%y'“_”:匹配任何单个字符“%”:匹配任意数目字符(包括零字符)

  7. nginx 代理静态资源报 403

    用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R ...

  8. jenkins+maven+junit构建自动化测试,整合junit xml生成直观的测试报告[留存]

    在自动化测试过程中,测试报告最能直观的体现测试的价值,之前一直使用maven+junit来构建我的自动化测试,但这样有几个缺点,一是,不能定时构建自动化任务(也许是我没有找到maven有没有提供这样的 ...

  9. Java用freemarker导出word

    概述 最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子. 详细 代码下载:http://w ...

  10. 远程阿里云window服务器报错身份验证错误

    整理文章,很久之前遇到的一个问题,一直呆在草稿箱,特发布出来,帮助可能遇到该问题的人 mstsc连接时报错如下 解决方法: 修改本地安全组策略[安全组  gpedit.msc]