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 ...
随机推荐
- php 字符串中的\n换行符无效、不能换行的解决方法
php 字符串中的\n换行符无效.不能换行的解决方法 程序的中的换行符\n会直接输出,无法正确换行,解决方法是把单引号改为双引号 aa
- 关于操作系统:eos、deepin
朋友星神推荐了这两个操作系统:eos.deepin,大致看了一下介绍,貌似看起来很棒,界面清新,而且开源,支持的应用也不少,后续我准备尝试一下.此处Mark一下: 官网分别为: https://ele ...
- Warning: Divide by zero.
问题:如标题 解决方案:分母加上+eps 参考自:http://www.ilovematlab.cn/thread-43128-1-1.html
- mahout基于Hadoop的CF代码分析(转)
来自:http://www.codesky.net/article/201206/171862.html mahout的taste框架是协同过滤算法的实现.它支持DataModel,如文件.数据库.N ...
- 2721: [Violet 5]樱花|约数个数
先跪一发题目背景QAQ 显然x,y>n!,然后能够设y=n!+d 原式子能够化简成 x=n!2d+n! 那么解的个数也就是n!的因子个数,然后线性筛随便搞一搞 #include<cstdi ...
- 一个简单的 JSON 生成/解析库
这是一个单文件的,适用于C语言的, JSON 读写库. 先说明,不想造轮子,代码是从这里拿来的: https://www.codeproject.com/Articles/887604/jWrite- ...
- TL 重构
import dependencies.*;import org.apache.commons.lang3.ArrayUtils;import org.apache.commons.lang3.Str ...
- 移植到windows下的iconv
This is a short memo about installing iconv on Windows host (specifically: Windows 7 SP1 x64). Iconv ...
- 在发送信息时应用PendingIntent.FLAG_UPDATE_CURRENT
1. 连续发送两条信息时,出现bug.以下是bug现象描述. 发送第一条信息,sentReceiver弹出toast告知发送成功,同时在listview中的发送状态立即同步更新为发送成功. 继续发送第 ...
- ios中图片旋转
@interface ViewController () { UIImageView *_imageview; BOOL flag; } @end @implementation ViewContro ...