When you discuss the functionality of languages, the first thing that comes to mind is the number of built-in functions it has been providing you to perform some specific tasks without repeating the same lines of code over and over again. This is to save our time and make the programs more efficient. The Java language also came up with many such functions, including its current date and time functions. These functions are used to get the current timezone information and utilize it in many ways, like counting the time for multithreaded processes, etc. This article will include some Java examples of using different Java classes to get the current date and time without complicating the programs. Now, let’s begin.
Let’s start with an example of using Java’s “DateTimeFromatter” and “LocalDateTime” classes to get the instances for date and time and display them in the user-defined format. Thus, a new program got initiated in the “test.java” file of the Eclipse IDE while using the Windows operating system. Now that the program has started, it is time to create a valid Java code structure to make this program executable. Therefore, use the import statements to import the DateTimeFormatter and LocateDateTime class from the Java package and name your public class the same as you named your Java file, i.e., “test”.
Our program is going to use a single function, i.e., the main() function, to do the execution process for getting the date and time. The main() method has been started with the initialization of an instance “f” for the DateTimeFormatter class using its “ofPattern” function that has been taking a user-defined pattern for date and time. This instance “f” will simply specify the format for date-time that will be derived from the LocalDateTime class instance “now”, i.e., initialized right after the initialization of an instance “f”.
After this, Java’s “System.out.println” function statement is used here, which has been casting off the “format” function with the instance “f” within its parenthesis. The format() function will use the “now” instance to get the current date and time. In contrast, the “f” instance calling the “format” function will convert the date and time into a user-defined format before displaying it in the output area after the execution. Now that this program has been completed, we will save it before its execution.
After saving our Java code, we executed this using the Eclipse IDE run icon from the taskbar of Eclipse IDE and got the result in the output console. The output image attached beneath shows us the date and time of our local time zone within the user-specified format, i.e., using the DateTimeFormatter and LocalDateTime class instances.
Let’s use the LocalDateTime class in a new example to see how it works alone. So, we have imported it at the 2nd line of our new Java code used in the same “test.java” file. In a similar way, we have added a public class named “test” with the main() function within it. Without creating an object for the LocalDateTime class, we have simply called the now() function using the java.time.LocalDate class package in the println() function statement of Java. Now that we have been only using the LocateDate class to call the now() method, it will only display the date and not the current time for our timezone via the println() output statement.
The execution output is the same as expected, i.e., only the date got displayed.
Suppose you want to use the LocateDateTime class within the Java class without utilizing any other class function except the now() function. In that case, it will be printing the output in the slightest different way. Upon updating our previous Java code, we imported the LocalDateTime class to use it within the main() function. The println() statement will be calling the now() method using the whole package name “java.time.LocalDateTime” without creating an instance, i.e., this is said to be the direct use of a class instance. Let’s just quickly save our code and run it.
Using this way, we have the date and time in a more precise way, i.e., time in milliseconds.
In a similar way, java.time package can be used to get the current date and time using the “Clock” class along with its systemUTC() function that has also been calling the built-in instant() function for this purpose.
The output will be very similar to the bunch of code examples we have utilized.
In our third illustration, we will cast off the SimpleDateFormat class of Java to get the current timezone and make it display. So, the code file has been updated in Eclipse IDE. We imported the SimpleDateFormat class using the Java package, and then the Date class was imported the same way. The public class “test” has been updated a little within its main() method. We have initialized an object “f” for the SimpleDateFormat class to specify the particular format for a date and time to be displayed, i.e., the current timezone. Now, it’s time to get the date and time using the Date class, i.e., by creating its object “d”.
After this, the println() function statement will be utilized to print out the formatted date via the format() function called by the “f” instance. It will use the “d” instance within the format() function to convert the newly found date and time into a specified format before displaying.
After updating the Java code, we need to save it and execute it. On execution, it will display the date and time for your current timezone in the same specified format.
After realizing the importance of date and time classes and functions in different languages, we have discussed three other yet slightly similar examples to practice getting the current date and time. For this, the use of LocalDate, LocalDateTime, DateTimeFormatter, and SimpleDateFormat classes is worth mentioning. Along with that, the use of now(), format(), and instant() function is necessary where required.