Erhalten Sie das abgelaufene Datum (z. B. 0 Jahre, 0 Monate, 0 Tage) aus den beiden Daten. Beispiel 2020/08/01 und 2020/08/03 → 0 Jahre 0 Monate 2 Tage 2020/08/01 und 2020/09/01 → 0 Jahre 1 Monat 0 Tage 2020/08/01 und 2021/08/01 → 1 Jahr 0 Monate 0 Tage
    public static int getDateDiff(String paraFromDate, String paraToDate, int[] paraDiffDay) {
        int     diffYear = 0 ;
        int     diffMonth = 0 ;
        int     diffDay = 0 ;
        //Teilen Sie das Startdatum in Jahr, Monat und Tag auf(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)) ;
        //Teilen Sie das Basisdatum in Jahr, Monat und Tag auf(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) ;
        //Anfangsdatum>Wenn es sich um ein Basisdatum handelt, machen Sie einen Fehler
        if(comp_from.compareTo(comp_to) > 0) {
            return -1;
        }
        //Holen Sie sich den Jahresunterschied
        diffYear = to_year - from_year ;
        //Vergleiche Monate
        if(from_month > to_month) {
            //Wenn der Monat des Startdatums größer ist=Im Laufe des Jahres
            to_month = to_month + 12 ;
            //Minus Jahr
            //Wenn es das gleiche Jahr von ist_Monat kann nie größer sein, also kann es nicht negativ sein
            diffYear = diffYear - 1 ;
        }
        //Holen Sie sich die Monatsdifferenz
        diffMonth = to_month - from_month ;
        //Tage vergleichen
        //Anfangsdatum(From)Wenn es größer ist, wird beurteilt, dass es den Mond überspannt
        if(from_day > to_day) {
            //Fügen Sie die maximale Anzahl von Startdaten hinzu
            to_day = to_day + comp_from.getActualMaximum(comp_from.DATE) ;
            //Abzüglich des Unterschieds zwischen den Monaten
            diffMonth = diffMonth - 1 ;
        }
        //Holen Sie sich den Unterschied zwischen den Tagen
        diffDay = to_day - from_day ;
        //In Argument setzen
        paraDiffDay[0] = diffYear ;
        paraDiffDay[1] = diffMonth ;
        paraDiffDay[2] = diffDay ;
        return 0 ;
    }
Anrufer
    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] + "Jahr" + diffDate[1] + "Monate" + diffDate[2] + "Tag" ;
    }
    
・ Keine Fehlerbehandlung → Eine Ausnahme tritt auf, wenn das Argumentdatum nicht das Format JJJJ / MM / TT hat → Wenn das Datum auch im Format JJJJ / MM / TT nicht vorhanden ist, ist das Verhalten falsch.
Recommended Posts