Launch Eclipse IDE
Let’s begin with a new start of this tutorial with the launch of Eclipse IDE, which has already been mounted and configured at our end. When you launch the Eclipse IDE, you will be presented with the interface shown below. Tap on the “Create a new Java Project” option.
Create New Project
A window like the one below would be opened in which you can enter the name of a Java file to be created, i.e., test. You can continue with the already selected default location for Java program files or change it accordingly. Along with that, you need to select the latest version of JAVA JDK configured at your end by choosing the “Use a project-specific JRE” option beneath the “JRE” section, i.e., jdk-19. Hit the “Next” button afterward.
Now, you need to name the module for your Java program that you have been creating from the new window, i.e., we named it “test”. This section would create a “module-info.java” file within the Java environment. Press the “Create” button to proceed.
When the module has been successfully created in the Java environment, you can also change its settings using the “test” module title from the opened window. Now, you have to finish setting up the Java project by utilizing the “Finish” button from the currently opened screen.
Now, you can see the “module-info.java” file on the left side of the Eclipse IDE screen, as presented beneath.
Now, it’s time to create a new class of Java in the “test” project of “Eclipse IDE”. For this, click on the “File” menu from the Eclipse IDE currently opened screen and hover over the option “New”. It will expand the bunch of options that can be formed in seconds. So, tap on the “Class” option to generate a new class within the “test” module of the current project. A window named “New Java Class” has been launched to create a new Java class. You can add the Name of a class within the text area in front of the “Name” title. Make it public and add a main() function within it as well, i.e., using the method part. Hit the “Finish” button to proceed with a program now.
Within Java’s first illustration program, we will be elaborating on the use of Java.util.Date class of “java” to display the current date and time on our output screen. The program has already begun with the name of a package “test” and the initialization of a public class “test” that we have just generated. Also, the public main() function is here to get started with the program’s execution.
We are utilizing the “java.util.Date” class package to create a new class object “d” here to get the current date and time for the current timezone. After getting the current date and time for our current timezone, we have used this object variable “d” within the “println” function from the “System.out” package of “java” to display it on the Eclipse IDE output area. This program ends here and is ready to be compiled and executed.
We have executed our above Java code via the “Run” menu of the Eclipse IDE. The output area of the Eclipse IDE tool has been showing you the current day, “Mon”, and the current date, Oct 03, 2022, along with the current time of time timezone, “PKT”. The format of this time and date output is a little mixed up, i.e., the year 2022 is quite far from Oct 03, while the time has been displayed within the date and timezone. The time is displayed in an “hour:minutes:seconds” format.
The other way to use the “java.util.Date” package in the Java program is to import this class package within the program before the utilization of the public class. So, we have updated the same java code and imported the “java.util.Date” package here in the 2nd line of code via the “import” keyword. By importing the package “java.util.Date” this way, we have to only use the class name “Date” within the main() method to get a date and time in an object “d” via the keyword “new” as presented in the attached snap underneath. The use of variable object “d” to call the “toString” function within the println() function statement will display the time and date on the output.
On running this program, the very same format of time and date is displayed.
The use of Java.util.date package, along with some other java packages, can make you modify your output for java programs, especially the date and time formats. Let’s use the “java.util.Date” package along with the “java.text.SimpleDateFormat” package to modify the date format and time to be displayed. After importing both packages, we formed the “d” object from the “Date” class and displayed it in the standard format. The “f” object from the “SimpleDateFormat” class has been initialized to get the date and time and mold it to a different format that has been specified. The new format of date and time will be output.
The output shows the standard format and the new user-specified format for date and time separately.
Within the Eclipse IDE, we performed some java programs to elaborate the use of Java.util.date class in Java programming. Using the Eclipse IDE, we have discussed two simple java examples; one to show the use of the “java.util.Date” class to display a date and time in a standard format. While the other one elaborates on the use of Java.util.Date class along with the SimpleDateFormat package to modify the date and time format on the output.