如何使用Java获取当前时间戳[通俗易懂]

如何使用Java获取当前时间戳[通俗易懂]要获取Java中的当前时间戳:Timestamptimestamp=newTimestamp(System.currentTimeMillis());//2016-11-1606:43:19.77这是两个Java示例,向您展示如何获取Java中的当前时间戳。(使用Java8更新)1.java.sql.Timestamp获得当前java.sql.Times…

大家好,又见面了,我是你们的朋友全栈君。

要获取Java中的当前时间戳:

Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//2016-11-16 06:43:19.77

这是两个Java示例,向您展示如何获取Java中的当前时间戳。 (使用Java 8更新)

1. java.sql.Timestamp

获得当前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));
        
    }

}

输出量

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

在Java 8中,可以将java.sql.Timestamp转换为新的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());

    }

}

输出量

2016-11-16 06:55:40.11
1479250540110
2016-11-15T22:55:40.110Z
1479250540110
1479250540110

参考文献

  1. java.sql.Timestamp JavaDoc
  2. java.time.Instant JavaDoc

翻译自: https://mkyong.com/java/how-to-get-current-timestamps-in-java/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130178.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • html中table样式_table样式设置

    html中table样式_table样式设置如果想在网页中建一个如下table表格应该怎么做呢?首先建一个表<table><table>/*table标签就是建一个表格*/ <tr>/*tr标签就是tablerow,即为表格中的一行*/ <th>学号</th>/*th标签即tablehead,就是表头*/ <th>姓名</th> <th>手机号</th> <th>家庭地址</th&

    2022年9月20日
    2
  • linux时间戳转换成标准时间_linux获取毫秒时间戳

    linux时间戳转换成标准时间_linux获取毫秒时间戳[lvhongmin@AY1401201736089361adZbin.gatemaster]$date2014年05月28日星期三11:24:59CST[lvhongmin@AY1401201736089361adZbin.gatemaster]$date+%s1401247504[lvhongmin@AY1401201736089361adZbin.gatemaster]…

    2022年10月3日
    2
  • iterator迭代器详解_c++迭代器iterator

    iterator迭代器详解_c++迭代器iterator迭代器 Iterator动机模式定义实例结构要点总结笔记动机在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象.我们希望在不暴露其内部结构地同时.可以让外部客户代码透明地访问其中包含地元素;同时这种”透明遍历”也为”同一种算法在多种集合对象上进行操作”提供了可能.使用面向对象技术将这种便利机制抽象为”迭代对象”为”应对变化中地集合对象”提供了一种优雅地方式模式定义提供了一种方法顺序访问一个聚合对象中地各个元素,而又不暴露(稳定)该对象地内部表示.实例结构要点总结迭代抽象

    2022年8月11日
    3
  • Laravel5.5+ 区分前后端用户登录

    Laravel5.5+ 区分前后端用户登录

    2021年11月8日
    37
  • phpstorm2021.10永久激活码【2021最新】

    (phpstorm2021.10永久激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月27日
    108
  • vr体验心得_在我们新的VR学习体验中逃脱女巫的小屋

    vr体验心得_在我们新的VR学习体验中逃脱女巫的小屋vr体验心得OurbrandnewprojectforUnityLearnisanimmersiveVRescaperoom.ExplorethepotentialofVRinUnityandcreateyourownexperienceinasimpleprototypeenvironment.Letusintroduceyou…

    2022年10月1日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号