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
Here 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.
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.
Other Date and Time Java tutorials from Javarevisited Blog