Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JAVA»How to Get Current TimeStamp in Java

    How to Get Current TimeStamp in Java

    By RahulFebruary 1, 20201 Min Read

    Java Code:

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

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

    Advertisement

    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.

    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.

    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.

    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

    Related Posts

    Bash – How to Get Future Date and Time

    How to Set JAVA_HOME environment variable on macOS

    How to Set JAVA_HOME environment variable on macOS

    What is the Access Modifiers in Java

    What are the Access Modifiers in Java

    View 5 Comments

    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

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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