android/java 中两个日期与今天相比的百分比

Percentage between two dates compared to today in android/java

如何找到 android/java 中两个日期与今天相比的百分比? 本题基于.

我想做这样的事情:

    Calendar c = Calendar.getInstance();
    yeartoday = c.get(Calendar.YEAR);
    monthtoday = c.get(Calendar.MONTH);
    daytoday = c.get(Calendar.DAY_OF_MONTH);
...

    Date datestart = getDate(styear,(stmonth-1),stday);
    Date dateend = getDate(year,(month-1),day);
    Date datetoday = getDate(yeartoday,monthtoday,daytoday);
    double percent = (((datetoday - datestart) / (dateend - datestart)) * 100);
...
public Date getDate(int year,int month,int day){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    Date date = calendar.getTime();
    return date;
}

我就是这么做的:

    double millisstart = datestart.getTime();
    double millisend = dateend.getTime();
    double millistoday = datetoday.getTime();
    double percent = (((millistoday - millisstart) / (millisend - millisstart)) * 100);
    percentAsString = Double.toString(percent);

    txt5.setText(percentAsString);

tl;博士

( ChronoUnit.DAYS.between( start , today ) * 100 ) 
/ 
ChronoUnit.DAYS.between( start , stop )

java.time

你使用的是可怕的 date-time classes,几年前被 java.time classes 取代JSR 310.

LocalDate

LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

时区对于确定日期至关重要。对于任何给定时刻,日期在全球范围内因地区而异。例如,Paris France is a new day while still “yesterday” in Montréal Québec.

午夜后几分钟

如果未指定时区,JVM 将隐式应用其当前默认时区。该默认值在运行时可能 change at any moment (!),因此您的结果可能会有所不同。最好明确指定您的 desired/expected 时区作为参数。如果关键,请与您的用户确认区域。

Continent/Region的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 ESTIST 等 2-4 字母缩写,因为它们 不是 真实时区,未标准化,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,代码将变得难以阅读,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样没有意识到这个问题。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

或指定日期。您可以通过数字设置月份,January-December 的数字为 1-12。

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

或者,更好的是,使用 Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year & YearMonth

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

经过的天数

您似乎想要一个基于天数的百分比。要计算经过的天数,请使用 between method on the ChronoUnit 枚举 class.

long days = ChronoUnit.DAYS.between( earlier , later ) ;

有了这些整数对,您就可以计算出您的百分比。

例子

ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
LocalDate start = today.minusDays( 5 ) ;
LocalDate stop = today.plusDays( 15 ) ;

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

long percentComplete = ( elapsedDays * 100 ) / totalDays ;

转储到控制台。

System.out.println( "start.toString(): " + start ) ;
System.out.println( "today.toString(): " + today ) ;
System.out.println( "stop.toString(): " + stop ) ;
System.out.println( "totalDays: " + totalDays ) ;
System.out.println( "elapsedDays: " + elapsedDays ) ;
System.out.println( percentComplete + "%" ) ;

看到这个 code run live at IdeOne.com

start.toString(): 2019-06-04

today.toString(): 2019-06-09

stop.toString(): 2019-06-24

totalDays: 20

elapsedDays: 5

25%

验证输入

当然,对于实际工作,您需要做更多的事情。

您应该验证开始是在停止之前。

boolean startIsBeforeStop = start.isBefore( stop ) ;

您需要验证今天是否在这对日期之间。

boolean todayIsWithinRange = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ;

ThreeTen-Extra

如果您的工作经常涉及日期范围,请添加 ThreeTen-Extra library to your project. This gives you the handy LocalDateRange class。

LocalDateRange range = LocalDateRange.of( start , stop ) ;
boolean startIsBeforeStop = ( range.lengthInDays() > 0 ) ; // A negative number beens the start is *not* before the stop.
boolean todayIsWithinRange = range.contains( today ) ;

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

在哪里获取java.time classes?