Android / Java-날짜 차이 (일)


80

아래 코드를 사용하여 현재 날짜 (1999 년 12 월 31 일 즉 mm / dd / yyyy 형식)를 얻고 있습니다.

Textview txtViewData;
txtViewDate.setText("Today is " +
        android.text.format.DateFormat.getDateFormat(this).format(new Date()));

2010-08-25 (예 : yyyy / mm / dd) 형식의 다른 날짜가 있습니다.

그래서 일수에서 날짜의 차이를 찾고 싶습니다. 일의 차이를 어떻게 알 수 있습니까?

(즉, CURRENT DATE-yyyy / mm / dd 형식의 날짜 차이를 찾고 싶습니다. )


이 코드는 이제 java.time 클래스로 대체 된 귀찮은 오래된 날짜-시간 클래스를 사용합니다. 이전 Java 및 Android의 경우 ThreeTen-Backport 및 ThreeTenABP 프로젝트를 참조하십시오 .
— Basil Bourque

비슷한 질문,하지만 전체 날짜보다는 순간을 사용 : 일의 날짜 차이, 안드로이드
— 바질 우르 큐

답변:


126

신뢰할 수있는 방법은 아니지만 JodaTime 을 사용하는 것이 좋습니다.

  Calendar thatDay = Calendar.getInstance();
  thatDay.set(Calendar.DAY_OF_MONTH,25);
  thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
  thatDay.set(Calendar.YEAR, 1985);

  Calendar today = Calendar.getInstance();

  long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

다음은 근사치입니다 ...

long days = diff / (24 * 60 * 60 * 1000);

문자열에서 날짜를 구문 분석하려면 다음을 사용할 수 있습니다.

  String strThatDay = "1985/08/25";
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
  Date d = null;
  try {
   d = formatter.parse(strThatDay);//catch exception
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 


  Calendar thatDay = Calendar.getInstance();
  thatDay.setTime(d); //rest is the same....

비록, 당신은 날짜 형식을 확신하기 때문에 ... 당신은 또한 Integer.parseInt()숫자 값을 얻기 위해 그것의 부분 문자열에 할 수 있습니다.


@stOle 고맙습니다,하지만 두 날짜를 문자열로 표시하고 있습니다. 그래서 어떻게해야하나요, pls는 저에게 자세히 알려줍니다. pls
— Paresh Mayani

1
@stOle이 정확한 답변을 얻지 못함, 코드에 작은 실수가있을 수 있습니다. String strThatDay = "2010/10/03"을 설정해도 274 일 간격이 발생합니다. 지원에 대한 고맙습니다 1 일이어야합니다
— Paresh Mayani

@Paresh, 죄송합니다. 을 월은 대문자 M, 분은 소문자 ("yyyy/mm/dd");로 바꿔야합니다 ("yyyy/MM/dd");. 수정되었습니다.
— st0le

1
@Gevorg, 나는 그것을 추천했다. :) Me
— Gusta

3
때때로이 코드는 밀리 초를 나눌 때 반올림 문제 (부족함)로 인해 휴일이 될 수 있습니다. 이것은 나를 위해 작동합니다 :Math.round(millisBetweenDates * 1f / TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
— Ciske Boekelo

83

이것은 내 작품이 아니며 여기 에서 답을 찾았습니다 . 미래에 끊어진 링크를 원하지 않았습니다 :).

핵심은 일광 설정을 고려하기위한이 라인, ref Full Code입니다.

TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

또는 TimeZone 매개 변수로 전달 하고 및 개체를 daysBetween()호출 setTimeZone()하십시오 .sDateeDate

그래서 여기에 간다 :

public static Calendar getDatePart(Date date){
    Calendar cal = Calendar.getInstance();       // get calendar instance
    cal.setTime(date);      
    cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
    cal.set(Calendar.MINUTE, 0);                 // set minute in hour
    cal.set(Calendar.SECOND, 0);                 // set second in minute
    cal.set(Calendar.MILLISECOND, 0);            // set millisecond in second
    
    return cal;                                  // return the date part
}

여기 에서 가져온 getDatePart ()

/**
 * This method also assumes endDate >= startDate
**/
public static long daysBetween(Date startDate, Date endDate) {
  Calendar sDate = getDatePart(startDate);
  Calendar eDate = getDatePart(endDate);

  long daysBetween = 0;
  while (sDate.before(eDate)) {
      sDate.add(Calendar.DAY_OF_MONTH, 1);
      daysBetween++;
  }
  return daysBetween;
}

뉘앙스 : 두 날짜의 차이를 찾는 것은 두 날짜를 빼고 결과를 (24 * 60 * 60 * 1000)으로 나누는 것만 큼 간단하지 않습니다. 사실, 그 오류입니다!

예 : 2007 년 3 월 24 일과 2007 년 3 월 25 일 두 날짜의 차이는 1 일이어야합니다. 그러나 위의 방법을 사용하면 영국에서는 0 일이됩니다!

직접 참조하십시오 (아래 코드). 밀리 초 방식으로 진행하면 반올림 오류가 발생하며 일광 절약 시간과 같은 작은 문제가 발생하면 오류가 가장 분명해집니다.

전체 코드 :

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTest {

public class DateTest {

static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

public static void main(String[] args) {

  TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

  //diff between these 2 dates should be 1
  Date d1 = new Date("01/01/2007 12:00:00");
  Date d2 = new Date("01/02/2007 12:00:00");

  //diff between these 2 dates should be 1
  Date d3 = new Date("03/24/2007 12:00:00");
  Date d4 = new Date("03/25/2007 12:00:00");

  Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);
  Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);
  Calendar cal3 = Calendar.getInstance();cal3.setTime(d3);
  Calendar cal4 = Calendar.getInstance();cal4.setTime(d4);

  printOutput("Manual   ", d1, d2, calculateDays(d1, d2));
  printOutput("Calendar ", d1, d2, daysBetween(cal1, cal2));
  System.out.println("---");
  printOutput("Manual   ", d3, d4, calculateDays(d3, d4));
  printOutput("Calendar ", d3, d4, daysBetween(cal3, cal4));
}


private static void printOutput(String type, Date d1, Date d2, long result) {
  System.out.println(type+ "- Days between: " + sdf.format(d1)
                    + " and " + sdf.format(d2) + " is: " + result);
}

/** Manual Method - YIELDS INCORRECT RESULTS - DO NOT USE**/
/* This method is used to find the no of days between the given dates */
public static long calculateDays(Date dateEarly, Date dateLater) {
  return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);
}

/** Using Calendar - THE CORRECT WAY**/
public static long daysBetween(Date startDate, Date endDate) {
  ...
}

산출:

수동-2007 년 1 월 1 일과 2007 년 1 월 2 일 사이의 날짜 : 1

달력-2007 년 1 월 1 일과 2007 년 1 월 2 일 사이의 날짜 : 1


수동-2007 년 3 월 24 일과 2007 년 3 월 25 일 사이의 날짜 : 0

달력-2007 년 3 월 24 일과 2007 년 3 월 25 일 사이의 날짜 : 1


동의하다. 최상위 방법 사용 더 안정적이고 우아한 솔루션을 얻고 있습니다. 감사!
— Roger Alien

방법의 경우 : daysBetween if date가 2012 년 7 월 24 일 15:00이고 endDate가 2012 년 7 월 24 일 16:00이면 날짜가 endDate 이전이지만 하루 종일이 아니라 1 시간입니다. 내가 뭔가를 놓쳤거나 daysBetween의 결과 가이 경우에 잘못 되었습니까 (예상 결과가 0이지만 주어진 계산으로 0이 아닌 1이되어야 함)?
— AgentKnopf

@Zainodis, 머리 위로 코드를 업데이트했습니다. 나는 이것이 문제를 해결해야한다고 생각한다.
— Samuel

@SamQuest 업데이트 해 주셔서 감사합니다! 좀 더 순진한 접근 방식을 취했습니다. sDate.before (eDate)를 사용하는 while 루프가 중지되고 시작과 종료가 같은 일, 월 및 연도에 있으면 결과가 반환됩니다. 이것은 또한 첫 번째 반복 시작과 끝이 같은 일 / 월 / 년에있는 경우 (시간별 시작이 종료 이전 임에도 불구하고) 0이 올바르게 반환되도록합니다.
— AgentKnopf

당신은 종을받을 자격이 있습니다!
— marienke 2014

38

대부분의 답변은 귀하의 문제에 대해 훌륭하고 옳았습니다.

그래서 일수에서 날짜의 차이를 찾고 싶습니다. 일의 차이를 어떻게 알 수 있습니까?

모든 시간대에서 정확한 차이를 제공 할 수있는 매우 간단하고 직접적인 접근 방식을 제안합니다.

int difference= 
((int)((startDate.getTime()/(24*60*60*1000))
-(int)(endDate.getTime()/(24*60*60*1000))));

그리고 그게 다야!


이 너무 : 기타 정확한에 너무 복잡하고 길이었다 .. 나를 위해 일한
— 싯다 르트 나라 얀

1
두 번 나누는 것을 방지하기 위해 먼저 빼고 나중에 나누면 더 좋을 것입니다.
— ravindu1024 2017 년

@ ravindu1024 그렇게하면 startDate가 endDate보다 작 으면 +1의 차이가 발생합니다. 이 경우 +1 차이가 있습니다. 답에 -1을 추가하여 해결할 수 있습니다.
— sHOLE

@sHOLE 어떻게? 내가 말하는 것은 t1 / C-t2 / C 대신 (t1-t2) / C를해야한다는 것입니다. t1 / C도 t2 / C도 0이 아니기 때문에 그것이 답에 어떤 영향을 미치는지 알 수 없습니다.
— ravindu1024

@ ravindu1024 나는 당신이 말하려는 것을 이해 하고이 답변을 읽을 때 똑같이 궁금해했습니다. 구현 후에야 왜 그렇게되지 않았는지 알아 차 렸습니다 (위에서 언급 한 이유).
— sHOLE

25

jodatime API 사용

Days.daysBetween(start.toDateMidnight() , end.toDateMidnight() ).getDays() 

여기서 'start'와 'end'는 DateTime 객체입니다. 날짜 문자열을 DateTime 개체로 구문 분석하려면 parseDateTime 메서드를 사용하십시오.

도 있습니다 안드로이드 특정의 JodaTime 라이브러리 .


3
지원에
— 감사하지만

2
조다에게 +1. Java Calendar API는 끔찍한 엉망이고 Joda는 깨끗하고 아름답습니다.
— LuxuryMode 2012

JodaTime 안드로이드의 여러 장치와 몇 가지 버그를주고, 나는 몇 가지 그 문제가 왜 몰라
— josemwarrior

1
Joda 시간 라이브러리는 프로젝트에 4744 개의 메서드를 추가합니다. 65K 방법 제한을 피하려면 현명하게 선택하십시오.
— Lior Iluz 2015

14

이 조각은 일광 절약 시간을 설명하며 O (1)입니다.

private final static long MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;

private static long getDateToLong(Date date) {
    return Date.UTC(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
}

public static int getSignedDiffInDays(Date beginDate, Date endDate) {
    long beginMS = getDateToLong(beginDate);
    long endMS = getDateToLong(endDate);
    long diff = (endMS - beginMS) / (MILLISECS_PER_DAY);
    return (int)diff;
}

public static int getUnsignedDiffInDays(Date beginDate, Date endDate) {
    return Math.abs(getSignedDiffInDays(beginDate, endDate));
}

6

이것은 나를위한 간단하고 최고의 계산이며 당신을위한 것일 수 있습니다.

       try {
            /// String CurrDate=  "10/6/2013";
            /// String PrvvDate=  "10/7/2013";
            Date date1 = null;
            Date date2 = null;
            SimpleDateFormat df = new SimpleDateFormat("M/dd/yyyy");
            date1 = df.parse(CurrDate);
            date2 = df.parse(PrvvDate);
            long diff = Math.abs(date1.getTime() - date2.getTime());
            long diffDays = diff / (24 * 60 * 60 * 1000);


            System.out.println(diffDays);

        } catch (Exception e1) {
            System.out.println("exception " + e1);
        }

@PareshMayani는 로그 고양이 확인
— 리시 가우 탐

3

Correct Way첫 번째 날짜가 두 번째보다 이전 인 경우 샘 퀘스트의 대답에서에만 작동합니다. 또한 두 날짜가 하루 이내이면 1을 반환합니다.

이것이 저에게 가장 잘 맞는 솔루션입니다. 대부분의 다른 솔루션과 마찬가지로 일광 절약 오프셋이 잘못되어 1 년에 2 일 동안 잘못된 결과가 표시됩니다.

private final static long MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;

long calculateDeltaInDays(Calendar a, Calendar b) {

    // Optional: avoid cloning objects if it is the same day
    if(a.get(Calendar.ERA) == b.get(Calendar.ERA) 
            && a.get(Calendar.YEAR) == b.get(Calendar.YEAR)
            && a.get(Calendar.DAY_OF_YEAR) == b.get(Calendar.DAY_OF_YEAR)) {
        return 0;
    }
    Calendar a2 = (Calendar) a.clone();
    Calendar b2 = (Calendar) b.clone();
    a2.set(Calendar.HOUR_OF_DAY, 0);
    a2.set(Calendar.MINUTE, 0);
    a2.set(Calendar.SECOND, 0);
    a2.set(Calendar.MILLISECOND, 0);
    b2.set(Calendar.HOUR_OF_DAY, 0);
    b2.set(Calendar.MINUTE, 0);
    b2.set(Calendar.SECOND, 0);
    b2.set(Calendar.MILLISECOND, 0);
    long diff = a2.getTimeInMillis() - b2.getTimeInMillis();
    long days = diff / MILLISECS_PER_DAY;
    return Math.abs(days);
}

3

이를 수행하는 가장 쉽고 쉬운 방법

  public int getDays(String begin) throws ParseException {
     long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

    long begin = dateFormat.parse(begin).getTime();
    long end = new Date().getTime(); // 2nd date want to compare
    long diff = (end - begin) / (MILLIS_PER_DAY);
    return (int) diff;
}

3

tl; dr

ChronoUnit.DAYS.between( 
    LocalDate.parse( "1999-12-28" ) , 
    LocalDate.parse( "12/31/1999" , DateTimeFormatter.ofPattern( "MM/dd/yyyy" ) ) 
)

세부

다른 답변은 구식입니다. Java의 초기 버전과 함께 번들로 제공되는 이전 날짜-시간 클래스는 잘못 설계되고 혼란스럽고 성가신 것으로 입증되었습니다. 그들을 피하십시오.

java.time

Joda-Time 프로젝트는 이전 클래스를 대체하는 데 매우 성공적이었습니다. 이러한 클래스는 Java 8 이상에 빌드 된 java.time 프레임 워크에 대한 영감을 제공했습니다 .

많은 java.time 기능은 자바 6 7 백 포팅 ThreeTen - 백 포트 및 상기 안드로이드 적응 ThreeTenABP .

LocalDate

LocalDate클래스는 시간이 하루의 시간 영역없이없이 날짜 만 값을 나타냅니다.

문자열 구문 분석

입력 문자열이 표준 ISO 8601 형식 인 경우 LocalDate클래스는 문자열을 직접 구문 분석 할 수 있습니다.

LocalDate start = LocalDate.parse( "1999-12-28" );

ISO 8601 형식이 아닌 경우 DateTimeFormatter.

String input = "12/31/1999";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM/dd/yyyy" );
LocalDate stop = LocalDate.parse( input , formatter );

경과 일수 ChronoUnit

이제 해당 LocalDate개체 쌍 사이에 경과 된 일 수를 가져옵니다 . ChronoUnit열거 계산 시간 경과.

long totalDays = ChronoUnit.DAYS.between( start , stop ) ; 

Java 열거 형에 익숙하지 않은 경우 대부분의 다른 프로그래밍 언어에서 기존 열거 형보다 훨씬 강력하고 유용하다는 것을 알고 있어야합니다. 자세한 내용은 Enum클래스 문서, Oracle Tutorial 및 Wikipedia 를 참조하십시오.


java.time 정보

java.time의 프레임 워크는 나중에 자바 8에 내장되어 있습니다. 이 클래스는 까다로운 기존에 대신 기존 과 같은 날짜 - 시간의 수업을 java.util.Date, Calendar, SimpleDateFormat.

Joda 타임 프로젝트는 지금에 유지 관리 모드 의로 마이그레이션을 조언 java.time의 클래스.

자세한 내용은 Oracle Tutorial을 참조하십시오 . 그리고 많은 예제와 설명을 위해 Stack Overflow를 검색하십시오. 사양은 JSR 310 입니다.

java.time 클래스는 어디서 구할 수 있습니까?

ThreeTen - 추가 프로젝트 추가 클래스와 java.time를 확장합니다. 이 프로젝트는 java.time에 향후 추가 될 수있는 가능성을 입증하는 근거입니다. 당신은 여기에 몇 가지 유용한 클래스와 같은 찾을 수 있습니다 Interval, YearWeek, YearQuarter, 그리고 더 .


java.time.LocalDate 안드로이드에서 지원되지 않습니다
— 마흐디 Astanei에게

1
@MahdiAstanei Android 용 ThreeTenABP 라이브러리에 대한 세 번째 단락을 다시 읽으십시오 . 이전 날짜-시간 클래스가 정말 그렇게 나쁘기 때문에 앱에 추가 할 가치가 있습니다.
— Basil Bourque

2

다음 기능을 사용하십시오.

   /**
     * Returns the number of days between two dates. The time part of the
     * days is ignored in this calculation, so 2007-01-01 13:00 and 2007-01-02 05:00
     * have one day inbetween.
     */
    public static long daysBetween(Date firstDate, Date secondDate) {
        // We only use the date part of the given dates
        long firstSeconds = truncateToDate(firstDate).getTime()/1000;
        long secondSeconds = truncateToDate(secondDate).getTime()/1000;
        // Just taking the difference of the millis.
        // These will not be exactly multiples of 24*60*60, since there
        // might be daylight saving time somewhere inbetween. However, we can
        // say that by adding a half day and rounding down afterwards, we always
        // get the full days.
        long difference = secondSeconds-firstSeconds;
        // Adding half a day
        if( difference >= 0 ) {
            difference += SECONDS_PER_DAY/2; // plus half a day in seconds
        } else {
            difference -= SECONDS_PER_DAY/2; // minus half a day in seconds
        }
        // Rounding down to days
        difference /= SECONDS_PER_DAY;

        return difference;
    }

    /**
     * Truncates a date to the date part alone.
     */
    @SuppressWarnings("deprecation")
    public static Date truncateToDate(Date d) {
        if( d instanceof java.sql.Date ) {
            return d; // java.sql.Date is already truncated to date. And raises an
                      // Exception if we try to set hours, minutes or seconds.
        }
        d = (Date)d.clone();
        d.setHours(0);
        d.setMinutes(0);
        d.setSeconds(0);
        d.setTime(((d.getTime()/1000)*1000));
        return d;
    }

2

적어도 나에게는 가능한 유일한 해결책 인 간단한 해결책이 있습니다.

문제는 Joda, Calendar, Date 등을 사용하여 내가 던지는 모든 답변이 밀리 초 만 고려한다는 것입니다. 결과적으로 실제 일 수가 아니라 두 날짜 사이의 24 시간주기 수를 계산합니다 . 따라서 1 월 1 일 오후 11 시부 터 1 월 2 일 오전 1 시까 지 0 일이 반환됩니다.

startDate과 사이의 실제 일수를 계산하려면 endDate다음을 수행하십시오.

// Find the sequential day from a date, essentially resetting time to start of the day
long startDay = startDate.getTime() / 1000 / 60 / 60 / 24;
long endDay = endDate.getTime() / 1000 / 60 / 60 / 24;

// Find the difference, duh
long daysBetween = endDay - startDay;

1 월 2 일과 1 월 1 일 사이에 "1"이 반환됩니다. 종료일을 계산해야하는 경우 1을 추가 daysBetween하십시오 (범위의 총 일수를 계산하고 싶었으므로 코드에서 수행해야했습니다).

이것은 Daniel이 제안한 것과 다소 유사 하지만 내가 생각하는 더 작은 코드입니다.


2

이러한 모든 솔루션에는 두 가지 문제 중 하나가 있습니다. 반올림 오류, 윤일 및 초 등으로 인해 솔루션이 완벽하게 정확하지 않거나 두 알 수없는 날짜 사이의 일 수를 반복하게됩니다.

이 솔루션은 첫 번째 문제를 해결하고 두 번째 문제를 약 365 배 개선합니다. 최대 범위가 무엇인지 안다면 더 좋습니다.

/**
 * @param thisDate
 * @param thatDate
 * @param maxDays
 *            set to -1 to not set a max
 * @returns number of days covered between thisDate and thatDate, inclusive, i.e., counting both
 *          thisDate and thatDate as an entire day. Will short out if the number of days exceeds
 *          or meets maxDays
 */
public static int daysCoveredByDates(Date thisDate, Date thatDate, int maxDays) {
    //Check inputs
    if (thisDate == null || thatDate == null) {
        return -1;
    }

    //Set calendar objects
    Calendar startCal = Calendar.getInstance();
    Calendar endCal = Calendar.getInstance();
    if (thisDate.before(thatDate)) {
        startCal.setTime(thisDate);
        endCal.setTime(thatDate);
    }
    else {
        startCal.setTime(thatDate);
        endCal.setTime(thisDate);
    }

    //Get years and dates of our times.
    int startYear = startCal.get(Calendar.YEAR);
    int endYear = endCal.get(Calendar.YEAR);
    int startDay = startCal.get(Calendar.DAY_OF_YEAR);
    int endDay = endCal.get(Calendar.DAY_OF_YEAR);

    //Calculate the number of days between dates.  Add up each year going by until we catch up to endDate.
    while (startYear < endYear && maxDays >= 0 && endDay - startDay + 1 < maxDays) {
        endDay += startCal.getActualMaximum(Calendar.DAY_OF_YEAR); //adds the number of days in the year startDate is currently in
        ++startYear;
        startCal.set(Calendar.YEAR, startYear); //reup the year
    }
    int days = endDay - startDay + 1;

    //Honor the maximum, if set
    if (maxDays >= 0) {
        days = Math.min(days, maxDays);
    }
    return days;
}

당신이 (후자 일 uninclusive) 날짜 사이의 일을해야하는 경우, 바로 없애 + 1당신이 볼 때 endDay - startDay + 1.


1

다른 방법 :

public static int numberOfDaysBetweenDates(Calendar fromDay, Calendar toDay) {
        fromDay = calendarStartOfDay(fromDay);
        toDay = calendarStartOfDay(toDay);
        long from = fromDay.getTimeInMillis();
        long to = toDay.getTimeInMillis();
        return (int) TimeUnit.MILLISECONDS.toDays(to - from);
    }

제공하신 코드에 대한 의견을 남겨주세요. 그래서 사람들은 코드의 의미를 이해합니다.
— Abed Putra

1

이 기능을 사용하십시오

    public static int getDateDifference(int previousYear, int previousMonthOfYear, int previousDayOfMonth, int nextYear, int nextMonthOfYear, int nextDayOfMonth, int differenceToCount){
    // int differenceToCount = can be any of the following
    //  Calendar.MILLISECOND;
    //  Calendar.SECOND;
    //  Calendar.MINUTE;
    //  Calendar.HOUR;
    //  Calendar.DAY_OF_MONTH;
    //  Calendar.MONTH;
    //  Calendar.YEAR;
    //  Calendar.----

    Calendar previousDate = Calendar.getInstance();
    previousDate.set(Calendar.DAY_OF_MONTH, previousDayOfMonth);
    // month is zero indexed so month should be minus 1
    previousDate.set(Calendar.MONTH, previousMonthOfYear);
    previousDate.set(Calendar.YEAR, previousYear);

    Calendar nextDate = Calendar.getInstance();
    nextDate.set(Calendar.DAY_OF_MONTH, previousDayOfMonth);
    // month is zero indexed so month should be minus 1
    nextDate.set(Calendar.MONTH, previousMonthOfYear);
    nextDate.set(Calendar.YEAR, previousYear);

    return getDateDifference(previousDate,nextDate,differenceToCount);
}
public static int getDateDifference(Calendar previousDate,Calendar nextDate,int differenceToCount){
    // int differenceToCount = can be any of the following
    //  Calendar.MILLISECOND;
    //  Calendar.SECOND;
    //  Calendar.MINUTE;
    //  Calendar.HOUR;
    //  Calendar.DAY_OF_MONTH;
    //  Calendar.MONTH;
    //  Calendar.YEAR;
    //  Calendar.----

    //raise an exception if previous is greater than nextdate.
    if(previousDate.compareTo(nextDate)>0){
        throw new RuntimeException("Previous Date is later than Nextdate");
    }

    int difference=0;
    while(previousDate.compareTo(nextDate)<=0){
        difference++;
        previousDate.add(differenceToCount,1);
    }
    return difference;
}

이 코드는 이제 java.time 클래스로 대체 된 귀찮은 오래된 날짜-시간 클래스를 사용합니다. 이전 Java 및 Android의 경우 ThreeTen-Backport 및 ThreeTenABP 프로젝트를 참조하십시오 .
— Basil Bourque

달력 수업은 오래된 날짜-시간 수업인가요?
— neal zedlav 2017

예, java.time패키지 외부에서 발견 된 모든 날짜-시간 관련 클래스 는 이제 레거시이므로 피해야합니다. 여기에는 Date및 Calendar및 java.sql 클래스가 포함됩니다. Oracle Tutorial을 참조하십시오.
— 바질 우르 큐

1
        public void dateDifferenceExample() {

        // Set the date for both of the calendar instance
        GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02,5,23,43);
        GregorianCalendar cal2 = new GregorianCalendar(2015, 04, 02);

        // Get the represented date in milliseconds
        long millis1 = calDate.getTimeInMillis();
        long millis2 = cal2.getTimeInMillis();

        // Calculate difference in milliseconds
        long diff = millis2 - millis1;

        // Calculate difference in seconds
        long diffSeconds = diff / 1000;

        // Calculate difference in minutes
        long diffMinutes = diff / (60 * 1000);

        // Calculate difference in hours
        long diffHours = diff / (60 * 60 * 1000);

        // Calculate difference in days
        long diffDays = diff / (24 * 60 * 60 * 1000);
    Toast.makeText(getContext(), ""+diffSeconds, Toast.LENGTH_SHORT).show();


}

0

이 작업을 수행하는 매우 쉬운 방법을 찾았으며 내 앱에서 사용하고 있습니다.

Time 객체에 날짜가 있다고 가정 해 보겠습니다 (또는 밀리 초 만 있으면됩니다).

Time date1 = initializeDate1(); //get the date from somewhere
Time date2 = initializeDate2(); //get the date from somewhere

long millis1 = date1.toMillis(true);
long millis2 = date2.toMillis(true);

long difference = millis2 - millis1 ;

//now get the days from the difference and that's it
long days = TimeUnit.MILLISECONDS.toDays(difference);

//now you can do something like
if(days == 7)
{
    //do whatever when there's a week of difference
}

if(days >= 30)
{
    //do whatever when it's been a month or more
}

0

Joda-Time

가장 좋은 방법은 프로젝트에 추가 할 매우 성공적인 오픈 소스 라이브러리 인 Joda-Time 을 사용 하는 것입니다.

String date1 = "2015-11-11";
String date2 = "2013-11-11";
DateTimeFormatter formatter = new DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime d1 = formatter.parseDateTime(date1);
DateTime d2 = formatter.parseDateTime(date2);
long diffInMillis = d2.getMillis() - d1.getMillis();

Duration duration = new Duration(d1, d2);
int days = duration.getStandardDays();
int hours = duration.getStandardHours();
int minutes = duration.getStandardMinutes();

Android Studio를 사용하는 경우 joda-time을 매우 쉽게 추가 할 수 있습니다. build.gradle (앱)에서 :

dependencies {
  compile 'joda-time:joda-time:2.4'
  compile 'joda-time:joda-time:2.4'
  compile 'joda-time:joda-time:2.2'
}

좋은 대답입니다. toStringon 을 호출 Duration하면 ISO 8601 표준 형식 중 하나 인 PnYnMnDTnHnMnS . P마크 시작, 그동안 T시간 - 분 - 초에서 분리 년 - 월 - 일. 그래서 P3D세 가지 일이며, P3DT12H3 년 반 일입니다.
— Basil Bourque 2015

2
Joda 시간 라이브러리는 프로젝트에 4744 개의 메서드를 추가합니다. 65K 방법 제한을 피하려면 현명하게 선택하십시오.
— Lior Iluz 2015

이 요구는 ( 'YYYY-MM-DD') DateTimeFormatter 포맷터 = DateTimeFormat.forPattern로 변경하기
— sb_269

0
        Date userDob = new SimpleDateFormat("yyyy-MM-dd").parse(dob);
        Date today = new Date();
        long diff =  today.getTime() - userDob.getTime();
        int numOfDays = (int) (diff / (1000 * 60 * 60 * 24));
        int hours = (int) (diff / (1000 * 60 * 60));
        int minutes = (int) (diff / (1000 * 60));
        int seconds = (int) (diff / (1000));
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.