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

    How to Get Current Date and Time in Java

    By RahulApril 17, 20192 Mins Read

    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.

    Advertisement

    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.

    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.

    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.

    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

    Related Posts

    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

    Installing Java on Ubuntu 22.04

    How to Install JAVA on Ubuntu 22.04

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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