Java | Current date and time

There can be multiple ways to print current date and time.

Using Date Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print current date and
// time using Date class.
import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
        Date d1 = new Date();
        System.out.println("Current date is " + d1);
    }
}

chevron_right


Output:

Current date is Sat Mar 24 18:22:46 UTC 2018

Using Calender class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code of calendar.get() function
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        Calendar c = Calendar.getInstance();
        System.out.println("Day of week : "
                           c.get(Calendar.DAY_OF_WEEK));
        System.out.println("Day of year : "
                            c.get(Calendar.DAY_OF_YEAR));
        System.out.println("Week in Month : "
                            c.get(Calendar.WEEK_OF_MONTH));
        System.out.println("Week in Year : " +
                            c.get(Calendar.WEEK_OF_YEAR));
        System.out.println("Day of Week in Month : " +                     
                      c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println("Hour : " + c.get(Calendar.HOUR));
        System.out.println("Minute : " + c.get(Calendar.MINUTE));
        System.out.println("Second : " + c.get(Calendar.SECOND));
        System.out.println("AM or PM : " + c.get(Calendar.AM_PM));
        System.out.println("Hour (24-hour clock) : "
                           c.get(Calendar.HOUR_OF_DAY));
    }
}

chevron_right


Output:

Day of week : 7
Day of year : 83
Week in Month : 4
Week in Year : 12
Day of Week in Month : 4
Hour : 6
Minute : 22
Second : 48
AM or PM : 1
Hour (24-hour clock) : 18

We can use calender and formatter class to print current date in a specific format.



My Personal Notes arrow_drop_up

Lets get started

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.