Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»How to Get Current TimeStamp in Java

    How to Get Current TimeStamp in Java

    RahulBy RahulNovember 13, 20151 Min ReadUpdated:February 1, 2020

    Java Code:

    Timestamp ts = new Timestamp(date.getTime());

    Above example command will return the current timestamp and save values in ts object variable.

    Java Get TimeStamp

    Following example will get the current time in milliseconds using getTime() function. After that Timestamp will convert it to the proper time. The below example is explained in multi-line but if you want the current timestamp only, Use above given one line code.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import java.util.Date;
    import java.sql.Timestamp;
     
    public class GetTimeStamp
    {
        public static void main( String[] args )
        {
     
    Date date= new Date();
     
    long time = date.getTime();
         System.out.println("Time in Milliseconds: " + time);
     
    Timestamp ts = new Timestamp(time);
    System.out.println("Current Time Stamp: " + ts);
        }
    }

    After executing the above Java code you will get the following values in return.

    Time in Milli Seconds: 1447402821007
    Current Time Stamp: 2015-11-13 13:50:21.007
    

    Java Get Date Only

    You can use SimpleDateFormat with Calendar class to find the date and time only.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
     
    public class GetTimeStamp
    {
        public static void main( String[] args )
        {
             Calendar cal = Calendar.getInstance();
     
             SimpleDateFormat dateOnly = new SimpleDateFormat("MM/dd/yyyy");
             System.out.println(dateOnly.format(cal.getTime()));
        }
    }

    Output:

    02/01/2020
    

    Java Get Time Only

    Similarly, you can use SimpleDateFormat with Calendar class to find the time only as well.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
     
    public class GetTimeStamp
    {
        public static void main( String[] args )
        {
             Calendar cal = Calendar.getInstance();
     
             SimpleDateFormat timeOnly = new SimpleDateFormat("HH:mm:ss");
             System.out.println(timeOnly.format(cal.getTime()));
        }
    }

    Output:

    15:05:40
    
    date Java time timestamp
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install PHP Zend Framework 2.5 on CentOS & RHEL
    Next Article How to Install CentOS Web Panel on CentOS, RedHat and cloudLinux

    Related Posts

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    Java HashMap – How to Get Key from Value

    Updated:April 6, 20222 Mins Read

    5 Methods to Print an Array in Java

    Updated:April 21, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    How to Install Tomcat 10 on Ubuntu 20.04

    Updated:February 18, 20226 Mins Read

    5 Comments

    1. yusuf on August 4, 2021 12:00 pm

      nice

      Reply
    2. Rikkert Batsbak on September 25, 2019 11:02 am

      can i just get the time, i dont need the date?

      Reply
    3. Srikanth on August 2, 2019 10:57 am

      More Simple.

      Timestamp ts = new Timestamp(System.currentTimeMillis());
      System.out.println(“Current Time Stamp: ” + ts);

      Reply
    4. Maicom MR on June 30, 2019 8:02 pm

      Works fine, thanks a lot!

      Reply
    5. Ram Gorre on May 30, 2019 4:55 am

      Simple and Neat. Good one.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.