Saturday, August 21, 2021

How to get current date, month, year and day of week in Java program - Example tutorial

Here is a quick Java tip to get the current date, month, year, and day of the week from the Java program. Java provides a rich Date and Time API though having thread-safety issue but it's rich in function and if used locally can give you all the date and time information that you need for your enterprise Java application. In the last Java tutorial on Date and Time API we have seen how to get the current date and time from different timezone using DateFormat and SimpleDateFormat classes and in this post, we will get some more details like the current month, year, day of the week, etc by using java.util.Calendar class.  Just keep in mind that the Calendar instance should not be shared between multiple threads.


The Calendar class in Java provides different fields to get different information like Calendar.DATE gives you the current date while Calendar.MONTH gives you the current month based on what type of Calendar you are using, which depends upon locale.


Java program to get the current month, year, date, and day of the week

How to get current date, time, month, year, dayofweek in Java with ExampleHere is a complete Java program to get various date and time-related information using java.util.Calendar class. In this program we are retrieving current time, current day, current month, current year, current day of the week, and current day of the month to demonstrate rich functionality of java.util.Calendar class. 

The calendar can also be used to get current time or date in a different timezone, which can be provided to Calendar class while calling getInstance() method of java.util.Calendar.



import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class JavaCalendarExample {

    public static void main(String args[]) {
        //getting local time, date, day of week and other details in local timezone
        Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
     
        Date currentTime = localCalendar.getTime();
        int currentDay = localCalendar.get(Calendar.DATE);
        int currentMonth = localCalendar.get(Calendar.MONTH) + 1;
        int currentYear = localCalendar.get(Calendar.YEAR);
        int currentDayOfWeek = localCalendar.get(Calendar.DAY_OF_WEEK);
        int currentDayOfMonth = localCalendar.get(Calendar.DAY_OF_MONTH);
        int CurrentDayOfYear = localCalendar.get(Calendar.DAY_OF_YEAR);

        System.out.println("Current Date and time details in local timezone");
        System.out.println("Current Date: " + currentTime);
        System.out.println("Current Day: " + currentDay);
        System.out.println("Current Month: " + currentMonth);
        System.out.println("Current Year: " + currentYear);
        System.out.println("Current Day of Week: " + currentDayOfWeek);
        System.out.println("Current Day of Month: " + currentDayOfMonth);
        System.out.println("Current Day of Year: " + CurrentDayOfYear);

     
     
        //getting time, date, day of the week, and other details in GMT timezone
        Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        //rest of stuff is same
    }

}

Output:
Current Date: Mon Jan 16 05:25:34 VET 2012
Current Day: 16
Current Month: 1
Current Year: 2012
Current Day of Week: 2
Current Day of Month: 16
Current Day of Year: 16

That’s all on how to get the current day, current month and year, current day of the month, and day of the week in Java. As I said Date and Time API in Java is very rich but has some serious issues like SimpleDateFormat is not thread-safe but hoping to get these issues resolved soon when Java 8 is coming with a new Date and Time API.

There is still an alternative with JODA Date and Time API but that is always the second choice to me because of additional dependency on the project.


Other Date and Time Java tutorials from Javarevisited Blog


No comments :

Post a Comment