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 Date and Time in Java

    How to Get Current Date and Time in Java

    RahulBy RahulApril 7, 20192 Mins ReadUpdated:April 17, 2019

    There are multiple ways to get the current date and time in Java programming language. Here we will discuss two ways using java.util.Date and java.util.Calendar Classes.

    Using java.util.Date Class

    Simply create a new Date() object to get the current date and time. Use the following Java program, In this program, we also used DateTimeFormatter class to show results in proper formats as required.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
     
    public class GetDateTime
    {
        public static void main( String[] args )
        {
            DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            Date date = new Date();
            System.out.println(dateFormat.format(date));
        }
    }

    Output: 04/06/2019 13:23:36
    

    Using java.util.Calendar Class

    Use the function Calendar.getInstance() from Calendar class. This provides options for manipulating the calendar fields, such as getting the date of the next week or next month, etc.

    You can also use date time format class as above with the below examples.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import java.util.Calendar;
     
    public class GetDateTime
    {
        public static void main( String[] args )
        {
            Calendar cal = Calendar.getInstance();
            System.out.println(cal.getTime());
        }
    }

    Output: Wed Apr 17 11:57:28 IST 2019
    

    Using the Calendar class, you can also find the date and time in the past or future. For example, what will the date after or before 10 days of the current date. Similarly, we can do the same for years, months, hours, minutes and seconds as well. Please find below a sample Java program.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    import java.util.Calendar;
     
    public class GetDateTime
    {
        public static void main( String[] args )
        {
            Calendar cal = Calendar.getInstance();
            System.out.println("Current Date & Time: " + cal.getTime());
     
            cal.add(Calendar.DATE, +10);
            System.out.println("Date after 10 days: " + cal.getTime());
     
            cal.add(Calendar.DATE, -10);
            System.out.println("Date before 10 days: " + cal.getTime());
     
            cal.add(Calendar.MONTH, 1);
            System.out.println("Date after 1 month: " + cal.getTime());
     
            cal.add(Calendar.YEAR, 5);
            System.out.println("Date after 5 years: " + cal.getTime());
        }
    }

    Output:

    Current Date & Time:  Wed Apr 17 12:06:05 IST 2019
    Date after 10 days:   Sat Apr 27 12:06:05 IST 2019
    Date before 10 days:  Wed Apr 17 12:06:05 IST 2019
    Date after 1 month:   Fri May 17 12:06:05 IST 2019
    Date after 5 years:   Fri May 17 12:06:05 IST 2024
    
    calendar date and time Java
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install and Use virtualenv with Python 2
    Next Article How to Enable JSON Gzip Compression in Apache

    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

    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.