Nice programing

Android / Java-날짜 차이 (일)

nicepro 2020. 10. 17. 12:23
반응형

Android / Java-날짜 차이 (일)


아래 코드를 사용하여 현재 날짜 (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 형식의 날짜 차이를 찾고 싶습니다. )


신뢰할 수있는 방법은 아니지만 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()숫자 값을 얻기 위해 그것의 부분 문자열에 할 수 있습니다.


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

핵심은 일광 설정을 고려하기위한이 라인, 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


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

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

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

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

그리고 그게 다야!


jodatime API 사용

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

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

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


이 조각은 일광 절약 시간을 설명하며 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));
}

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

       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);
        }

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

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

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);
}

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

  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;
}

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 TutorialWikipedia 를 참조하십시오.


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, 그리고 .


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

   /**
     * 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;
    }

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

문제는 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이 제안한 것과 다소 유사 하지만 내가 생각하는 더 작은 코드입니다.


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

이 솔루션은 첫 번째 문제를 해결하고 두 번째 문제를 약 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.


다른 방법 :

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);
    }

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

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
}

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'
}

        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));

이 기능을 사용하십시오

    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;
}

        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();


}

참고 URL : https://stackoverflow.com/questions/3838527/android-java-date-difference-in-days

반응형