Date & Time Handling In JAVA
While automating any web application, there might arise a situation where you might need to play around with Date and Time objects and manipulate them as per your need. Also, you may land into a situation where you need to pick dates from the UI. There are ample situations where I have used Date in all different kinds of projects. So let us understand how to achieve this in Java.
Java provides an inbuilt Date class available under the java.util package, this class holds the current date and time. Also, there are other classes using which we can simply use the
date.
date.
First, let's see some methods to get the current date and time in Java. Following is a snippet showing three different ways to get Date/Time in Java, which can further be used to do some other action.
Following is the output based on Line Number from the above code.
As per the required pattern of date+time format, we can print the date into our console or save them in a variable. Line No. 23/24 shows the easiest way of printing the Current System Date/Time in a single format only. Though we have flexibility in the above two methods for defining our format.
Date Formatting Using SimpleDateFormat
Any formatting in date/time can be done using the class SimpleDateFormat along with parsing dates.
1. A date (MM/dd/yyyy) element has been picked from UI in form String and has to be changed to some other date format MMM, dd yyyy.
Here, we have saved a Date as a String object (Line 12) and then parsed it to a Date class object with the actual date format (Line 15). Then as per required Date Format, a new Formatter object is created (Line 17) and the actual date is formatted into a String object again (Line 18).
As per the above code snippet, we have converted the current date to a date which is 4 months back and then 2 days ahead. Firstly, we have created a Calendar object and then parsed the date in the format MM/dd/yyyy (Line 11/13). Then using the same object we have added MONTH and DAY_OF_MONTH into it. Also, there are other methods by use of which we can increase the Year, Hours, etc.
Note: If you want to add date towards ahead of date then simply write the whole number value, but if you want to reduce the Date/Month/Year then use a - sign along with the number. Like in the above case, to reduce the Month -4 is used, but to increase the Days by 2, only value 2 is written. (Line 15/16).
Note: If you want to add date towards ahead of date then simply write the whole number value, but if you want to reduce the Date/Month/Year then use a - sign along with the number. Like in the above case, to reduce the Month -4 is used, but to increase the Days by 2, only value 2 is written. (Line 15/16).
There are many other classes and ways of using Date/Time into the code. But here I have tried to list the best and efficient ways of handling date and String objects having dates saved in them as an Automation Engineer.
Do Share and leave a comment about your queries.
Keep Rocking !!
Nicely explained 😊
ReplyDeleteThank you !!
Delete