[Android, Java] Method to find the elapsed date from two dates

Thing you want to do

Obtain the elapsed date (such as 0 years, 0 months, 0 days) from the two dates. Example 2020/08/01 and 2020/08/03 → 0 years 0 months 2 days 2020/08/01 and 2020/09/01 → 0 years, 1 month, 0 days 2020/08/01 and 2021/08/01 → 1 year 0 months 0 days


specification

-From date> To date returns an error (error code: -1) ・ Date format is yyyy / MM / dd only -From date and To date are both String type ・ Return year, month, and day with int [](0: year, 1: month, 2: day)


Implementation

    public static int getDateDiff(String paraFromDate, String paraToDate, int[] paraDiffDay) {
        int     diffYear = 0 ;
        int     diffMonth = 0 ;
        int     diffDay = 0 ;

        //Break down the date of the start date into year, month, and day(From)
        int     from_year = parseInt(paraFromDate.substring(0,4)) ;
        int     from_month = parseInt(paraFromDate.substring(5,7)) ;
        int     from_day = parseInt(paraFromDate.substring(8,10)) ;

        //Break down the base date into year, month, and day(To)
        int     to_year = parseInt(paraToDate.substring(0,4)) ;
        int     to_month = parseInt(paraToDate.substring(5,7)) ;
        int     to_day = parseInt(paraToDate.substring(8,10)) ;

        Calendar comp_from = Calendar.getInstance() ;
        comp_from.set(from_year, from_month - 1, from_day) ;

        Calendar comp_to = Calendar.getInstance() ;
        comp_to.set(to_year, to_month - 1, to_day) ;

        //Starting date>If it is a base date, make an error
        if(comp_from.compareTo(comp_to) > 0) {
            return -1;
        }

        //Get the year difference
        diffYear = to_year - from_year ;

        //Compare months
        if(from_month > to_month) {
            //When the month of the starting date is larger=Across the year
            to_month = to_month + 12 ;
            //Minus year
            //If it is the same year from_The month can never be bigger, so it can't be negative
            diffYear = diffYear - 1 ;
        }

        //Get the difference between the months
        diffMonth = to_month - from_month ;

        //Compare days
        //Starting date(From)If is larger, it is judged that it straddles the moon
        if(from_day > to_day) {
            //Add the maximum number of start dates
            to_day = to_day + comp_from.getActualMaximum(comp_from.DATE) ;
            //Minus the difference between the months
            diffMonth = diffMonth - 1 ;
        }
        //Get the difference between days
        diffDay = to_day - from_day ;

        //Set in argument
        paraDiffDay[0] = diffYear ;
        paraDiffDay[1] = diffMonth ;
        paraDiffDay[2] = diffDay ;

        return 0 ;

    }

Caller

    String fromDate = '2020/08/20' ;
    String toDate = '2020/10/01' ;
    int diffDate[] = {0,0,0} ;
    if( getDateDiff(fromDate, toDate, diffDate) != 0 ) {
        //error
    }
    else {
        String keikaDate = diffDate[0] + "Year" + diffDate[1] + "Months" + diffDate[2] + "Day" ;
    }
    

Problems, issues

・ No error handling → Exception occurs if the argument date is not in the format of yyyy / MM / dd → If the date does not exist even if it is in yyyy / MM / dd format, the behavior is incorrect.


good point

-Get the maximum number of days in the month using getActualMaximum → At first 31st in January, 3,5,7,8,10, December 30th in April, June, September and November February was 29 days in leap years, 28 days otherwise. Solved with a sentence of getActualMaximum.

Recommended Posts

[Android, Java] Method to find the elapsed date from two dates
[Java] I want to calculate the difference from the date
[Android, Java] Convenient method to calculate the difference in days
The road from JavaScript to Java
[Ruby] From the basics to the inject method
[Java] How to use the toString () method
[Java] How to set the Date time to 00:00:00
How to get the date in java
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Android] Call Kotlin's default argument method from Java
[Java] From two Lists to one array list
[Android] Easily calculate the difference between two dates
How to use the replace () method (Java Silver)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
[Android Studio] [Java] How to fix the screen vertically
From Java to Ruby !!
[Android] Uploading images from your device to the server
How to get the class name / method name running in Java
Java to fly data from Android to ROS of Jetson Nano
Find the address class and address type from the IP address with Java
[JDBC] I tried to access the SQLite3 database from Java.
Difference between Java and JavaScript (how to find the average)
Get to the abbreviations from 5 examples of iterating Java lists
Refer to C ++ in the Android Studio module (Java / kotlin)
How to create a form to select a date from the calendar
Android app to select and display images from the gallery
Create a method to return the tax rate in Java
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Send the accelerometer value from Android to PC via UDP
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
Input to the Java console
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
Android: How to deal with "Could not determine java version from '10 .0.1'"
If you want to change the Java development environment from Eclipse
Connecting to a database with Java (Part 1) Maybe the basic method
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] I tried to make a maze by the digging method ♪
[Java] How to convert from String to Path type and get the path
[Android] I want to get the listener from the button in ListView
How to use the link_to method
How to use the include? method
[java8] To understand the Stream API
How to find the average angle
[Android] Get the date on Monday
[Java] How to use join method
Run node.js from android java (processing)
Java comparison using the compareTo () method
I tried to explain the method
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
Welcome to the Java Library Swamp! !!
Try to extract java public method
Find the angle between two vectors
Reintroducing Java 8 available from Android Studio 2.4