[Android, Java] Convenient method to calculate the difference in days

Thing you want to do

I want to create a convenient method to calculate the difference in days

Receives string date information as an argument → For example, `2018/01/01``` and `2018/01/20``` → Returns the difference of the date received as an argument as an integer

Implementation details


    public static int dateDiff(String dateFromStrig, String dateToString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date dateTo = null;
        Date dateFrom = null;

        //Convert to Date type
        try {
            dateFrom = sdf.parse(dateFromStrig);
            dateTo = sdf.parse(dateToString);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        //Calculate the number of days of difference
        long dateTimeTo = dateTo.getTime();
        long dateTimeFrom = dateFrom.getTime();
        long dayDiff = ( dateTimeTo - dateTimeFrom  ) / (1000 * 60 * 60 * 24 );

        System.out.println( "Difference days: " + dayDiff );
        return (int) dayDiff;
    }

Recommended Posts

[Android, Java] Convenient method to calculate the difference in days
[Java] I want to calculate the difference from the date
How to get the class name / method name running in Java
Refer to C ++ in the Android Studio module (Java / kotlin)
How to learn JAVA in 7 days
Create a method to return the tax rate in Java
Call the super method in Java
[Android, Java] Method to find the elapsed date from two dates
Java reference to understand in the figure
[Java] How to use the toString () method
What is the main method in Java?
How to get the date in java
Java to C and C to Java in Android Studio
[Java] Handling of JavaBeans in the method chain
3 ways to import the library in Android Studio
[Android] Easily calculate the difference between two dates
How to use the replace () method (Java Silver)
Summarize the life cycle of Java objects to be aware of in Android development
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
# 2 [Note] I tried to calculate multiplication tables in Java.
Calculate the difference between numbers in a Ruby array
Source used to get the redirect source URL in Java
[Android Studio] [Java] How to fix the screen vertically
Java classes and instances to understand in the figure
I tried to implement the Euclidean algorithm in Java
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
How to use the getter / setter method (in object orientation)
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
Difference between Java and JavaScript (how to find the average)
Easy way to check method / field list in Java REPL
Calculate prime numbers in Java
Android Spot the Difference Game
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
I want to simplify the conditional if-else statement in Java
Input to the Java console
About the method to convert a character string to an integer / decimal number (cast data) in Java
[Java] How to search for a value in an array (or list) with the contains method
[Java] Get date information 10 days later using milliseconds in the Date class
Output the difference between each field of two objects in Java
Connecting to a database with Java (Part 1) Maybe the basic method
Source to display character array with numberPicker in Android studio (Java)
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
[Android / Java] Understand the description type in listener settings (button installation)
How to store the information entered in textarea in a variable in the method
[Java] I tried to make a maze by the digging method ♪
Determine if the strings to be compared are the same in Java
[Java] I want to perform distinct with the key in the object
[Android] Convert Map to JSON using GSON in Kotlin and Java
Sample code to call the Yahoo! Local Search API in Java
How to solve the unknown error when using slf4j in Java
[Android] I want to get the listener from the button in ListView
How to get the length of an audio file in java
Easy to understand the difference between Ruby instance method and class method.
How to increment the value of Map in one line in Java
How to use the link_to method
Access the network interface in Java
Try implementing Android Hilt in Java
Guess the character code in Java
How to use the include? method
How to use the form_with method