Get day, day of week, day of month, day of year, month, and year values from the current date using Java

In this tutorial, we will create a program that gets day, day of week, day of month, day of year, month, and year values from the current date using Java. I have already discussed how to get the date and time, but here in this program, we will breakdown and get the complete information on the current date. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of subString.java. 2. Import java.util package because in this library, Calendar abstract class is there.
  1. import java.util.*;
3. In your Main, instantiate variable clndr as a Calendar class to get an instance.
  1. Calendar clndr = Calendar.getInstance();
To display the current date with the time:
  1. System.out.println("Current Date: " + clndr.getTime());
To display the day from the current date:
  1. System.out.println("Day: " + clndr.get(Calendar.DATE));
To display the month from the current date:
  1. System.out.println("Month: " + (clndr.get(Calendar.MONTH) + 1));
To display the year from the current date:
  1. System.out.println("Year: " + clndr.get(Calendar.YEAR));
To display the day of the week from the current date:
  1. System.out.println("Day of Week: " + clndr.get(Calendar.DAY_OF_WEEK));
To display the day of the month from the current date:
  1. System.out.println("Day of Month: " + clndr.get(Calendar.DAY_OF_MONTH));
To display the day of the year from the current date:
  1. System.out.println("Day of Year: " + clndr.get(Calendar.DAY_OF_YEAR));
Press F5 to run it. Output: outputFull source code:
  1. import java.util.*;
  2.  
  3. public class dateValues
  4. {
  5. public static void main(String[] args)
  6. {
  7. Calendar clndr = Calendar.getInstance();
  8.  
  9. System.out.println("Current Date: " + clndr.getTime());
  10. System.out.println("Day: " + clndr.get(Calendar.DATE));
  11. System.out.println("Month: " + (clndr.get(Calendar.MONTH) + 1));
  12. System.out.println("Year: " + clndr.get(Calendar.YEAR));
  13. System.out.println("Day of Week: " + clndr.get(Calendar.DAY_OF_WEEK));
  14. System.out.println("Day of Month: " + clndr.get(Calendar.DAY_OF_MONTH));
  15. System.out.println("Day of Year: " + clndr.get(Calendar.DAY_OF_YEAR));
  16. }
  17. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment