Sunday, December 24, 2017

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

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

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

How to get current date, time, month, year, dayofweek in Java with ExampleHere is 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 week and current day of month to demonstrate rich functionality of java.util.Calendar class. Calendar can also be used to get current time or date in 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 week and other details in GMT timezone
        Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        //rest of stuff are same


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 current day,current month and year , current day of moth and day of week in Java . As I said Date and Time API in Java is very rich but has some serious issue like SimpleDateFormat is not thread-safe but hoping to get these issue resolved soon when Java 8 is coming with new Date and Time API. There is still an alternative with JODA Date and Time API but that is always second choice to me because of additional dependency on project.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Other Date and Time Java tutorials from Javarevisited Blog

No comments :

Post a Comment