How to Calculate Date Difference in JavaIn Java, date plays a very important role in calculating date differences. In designing the application, a date can be of joining an organization, admission date, appointment date etc. Many times we need to calculate the difference between two dates. There can be more than one reason for calculating the date difference. In Java, there is more than one way of getting the difference between two dates, which are as follows:
![]() Let's understand all three ways, one by one and understand how all these three classes are used to calculate the date difference in Java. SimpleDateFormat and Date classesThe SimpleDateFormat class is used for formatting and parsing the data. It is used to convert a date from one format into another format. In order to create a Date object of a given string date format, the SimpleDateFormat class is very helpful. There are the following steps that we will use to find the date difference using SimpleDateFormat and Date classes. ![]() 1) Create an object In the first step, we will create an instance of the SimpleDatFormat class and convert the string date format into a date object. 2) Parse date We will use the parse() method of that class for parsing both the string date into date object format. 3) Using getTime() method In the next step, we will get the difference between both the dates using the getTime() method. We will calculate the difference in milliseconds. 4) Use mathematical formula We can also use the mathematical formula if we want to get the difference in terms of years, days, hours, minutes, and seconds. 5) Print result At last, we will print the difference between both the dates. Let's take an example to understand how we can implement the above steps: DifferenceExample1.java Output: ![]() TimeUnit classJava provides the best way of finding the date difference, i.e., TimeUnit class. The process of finding the difference between the two dates is the same as using the SimpleDateFormatClass. The only difference is that we use the TimeUnit built-in class and its methods like toSeconds(), toMinutes(), toHours(), and toDays(). These methods directly return the days, hours, minutes and seconds. Let's take an example to understand how the TimeUnit class is used to get the date difference. DifferenceExample2.java Output: ![]() Period classJava provides another important built-in class that is very helpful to find the difference between the two days. We use the Period class to find the difference in terms of days, months and years. The Period class is similar to the TimeUnit class. The period class's between() method is responsible for calculating the difference between dates. The Period class provides several methods like ofYears(), withMonths(), withYears(), withDays(), toTotalMonths(), ofDays(), ofWeeks(), and ofMonths() etc. Let's take an example to understand how the period class between() method is used to get the date difference in days, months, and years. DifferenceExample3.java Output: ![]() The above three methods are commonly used ways for getting the date difference in Java. The SimpleDateFormat class is the simplest way of getting the date difference in terms of days, months and years.
Next TopicHow to Improve Coding Skills in Java
|