两个日期之间的Android差异

D F*_*rra 42 android date difference

我有两个约会:

String date_1="yyyyMMddHHmmss";
String date_2="yyyyMMddHHmmss";
Run Code Online (Sandbox Code Playgroud)

我想打印差异如:

2d 3h 45m
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?谢谢!

Dig*_*tel 148

DateTimeUtils obj = new DateTimeUtils();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

try {
    Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
    Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

    obj.printDifference(date1, date2);

} catch (ParseException e) {
    e.printStackTrace();
}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate) { 
    //milliseconds
    long different = endDate.getTime() - startDate.getTime();

    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
}
Run Code Online (Sandbox Code Playgroud)

out put是:

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds
Run Code Online (Sandbox Code Playgroud)

  • 错误:无法将DateTimeUtils解析为类型. (6认同)
  • 这假定存在JodaTime(DateTimeUtils来自该lib).库是如此之大,我个人避免在大多数Android项目中使用它以避免达到臭名昭着的Dex方法限制.答案至少应该提到它依赖于一些外部库.顺便说一句,在我看来,如果一个人决定使用外部lib时间,那么它应该是ThreeTenABP,而不是Joda.对于当前的情况,我希望只使用时间戳就足够了. (4认同)
  • 什么是`DateTimeUtils`? (2认同)

Mee*_*eet 18

简短而甜蜜:

/**
 * Get a diff between two dates
 *
 * @param oldDate the old date
 * @param newDate the new date
 * @return the diff value, in the days
 */
public static long getDateDiff(SimpleDateFormat format, String oldDate, String newDate) {
    try {
        return TimeUnit.DAYS.convert(format.parse(newDate).getTime() - format.parse(oldDate).getTime(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

int dateDifference = (int) getDateDiff(new SimpleDateFormat("dd/MM/yyyy"), "29/05/2017", "31/05/2017");
System.out.println("dateDifference: " + dateDifference);
Run Code Online (Sandbox Code Playgroud)

输出:

dateDifference: 2
Run Code Online (Sandbox Code Playgroud)

科特林版本:

@ExperimentalTime
fun getDateDiff(format: SimpleDateFormat, oldDate: String, newDate: String): Long {
    return try {
        DurationUnit.DAYS.convert(
            format.parse(newDate).time - format.parse(oldDate).time,
            DurationUnit.MILLISECONDS
        )
    } catch (e: Exception) {
        e.printStackTrace()
        0
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不错的方法。说“MILLISECONDS.toDays(...)”更简单 (2认同)
  • *DurationUnit* 对我不起作用!我只是将其更改为 *TimeUnit* 然后它就起作用了。我正在使用科特林 (2认同)

The*_*ble 10

这工作并转换为String作为奖金;)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    try {
        //Dates to compare
        String CurrentDate=  "09/24/2015";
        String FinalDate=  "09/26/2015";

        Date date1;
        Date date2;

        SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");

        //Setting dates
        date1 = dates.parse(CurrentDate);
        date2 = dates.parse(FinalDate);

        //Comparing dates
        long difference = Math.abs(date1.getTime() - date2.getTime());
        long differenceDates = difference / (24 * 60 * 60 * 1000);

        //Convert long to String
        String dayDifference = Long.toString(differenceDates);

        Log.e("HERE","HERE: " + dayDifference);

    } catch (Exception exception) {
        Log.e("DIDN'T WORK", "exception " + exception);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 9

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));
Run Code Online (Sandbox Code Playgroud)


Swa*_*oid 5

它会让你在几个月内有所不同

long milliSeconds1 = calendar1.getTimeInMillis();
long milliSeconds2 = calendar2.getTimeInMillis();
long periodSeconds = (milliSeconds2 - milliSeconds1) / 1000;
long elapsedDays = periodSeconds / 60 / 60 / 24;

System.out.println(String.format("%d months", elapsedDays/30));
Run Code Online (Sandbox Code Playgroud)

  • 这不是几个月,这是30天的倍数.微小的差异.:) (7认同)