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. Ant详解之-path、classpath和fileset

    转自:http://www.cnblogs.com/itech/archive/2011/11/01/2231206.html 一 .<path/> 和 <classpath/> ...

  2. 微信小程序 - tab选项卡(组件)

    更新日期: 2019/3/5:首次发布,默认下标“curIndex”超出红色提示 2019/3/7:增加tabName,可自定义数据标题名称(详情看示例) 支持单个/多个tab(显示/隐藏) 参数: ...

  3. Springmvc之接受请求参数二

    Springmvc之接受请求参数 准备工作 新建一个表单提交 请求地址: http://localhost:8080/ProjectName/user/login.do <form action ...

  4. PHP视频学习一 mysql

    设置mysql.exe文件目录到环境变量Path中去,可能在cmd下面使用mysql,wind7试了一下好像要重启以后才生效 什么是数据库 数据库就是存储数据的地方,是保存在计算机(硬盘/内存)中的数 ...

  5. win10更新后无法远程,报 credssp加密oracle修正

    答案都在图里,看不清就浏览器放大观看 打开开始菜单,搜索“编辑组策略”  进入

  6. 【转】IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段

    背景知识 IP地址 IP地址被用来当做Internet上的电脑的身份编号.大家日常见到的情况是每台联网的PC上都需要有IP地址,才能正常通信.我们可以把“个人电脑”比作“一台电话”,那么“IP地址”就 ...

  7. Eclipse和MyEclipse使用技巧--Eclipse中使用Git-让版本管理更简单

    详细步骤: 第一部分  GIT介绍 (1)GIT往世今生 2005年开发Samba的Andrew试图破解BitKeeper(一款商业的版本控制系统)的协议(这么干的其实也不只他一个),被BitMove ...

  8. nginx常用超时设置

    客户度连接nginx超时, 建议5s内 接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408 Syntax: client_header_timeout t ...

  9. CASE WHEN 及 SELECT CASE WHEN的用法(写了一坨烂代码发现两条sql就行了, 哎)

    转自:http://blog.sina.com.cn/s/blog_4c538f6c01012mzt.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE  ...

  10. ajax请求格式

    ajax请求格式........... var rowsData = $('#receiptPrintList').datagrid('getSelections'); $.ajax({ type: ...