zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    关于时间的字词
    Postgresql 存储过程调试 1
    Delphi 调试日子
    Delphi 调试日子
    TList,TObjectList 使用——资源释放
    Lazarus开发环境编译选项配置
    Delphi 递归搜索.SVN文件夹并“处理”
    Delphi 路径相关函数
    如何掌握程序语言(王垠)
    struct/class等内存字节对齐问题详解
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/10105240.html
Copyright © 2011-2022 走看看