【问题标题】:Percentage between two dates compared to today in android/javaandroid/java 中两个日期与今天相比的百分比
【发布时间】:2019-10-24 07:43:10
【问题描述】:

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

我想做这样的事情:

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

【问题讨论】:

  • 您当前的解决方案有什么问题?
  • 我认为你的代码唯一的问题是getDate返回的时间是Date而不是double。您可以使用 Calendar#timeInMillisDate#time 获取日期的当前毫秒数
  • 仅供参考,java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 等麻烦的日期时间类现在已被 java.time 类所取代。大多数 java.time 功能在 ThreeTen-Backport 项目中被反向移植到 Java 6 和 Java 7。进一步适用于ThreeTenABP 中的早期 Android (How to use ThreeTenABP…

标签: java android date percentage android-date


【解决方案1】:

tl;博士

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

java.time

您正在使用糟糕的日期时间类,这些类在几年前被 JSR 310 中定义的 java.time 类所取代。

LocalDate

LocalDate 类表示仅日期值,没有时间,也没有 time zoneoffset-from-UTC

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

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

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

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

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

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

或者指定一个日期。您可以通过数字设置月份,1 月至 12 月的编号为 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 枚举对象,一年中的每个月一个。提示:在整个代码库中使用这些 Month 对象,而不是仅仅使用整数,以使您的代码更具自记录性,确保有效值并提供 type-safetyYearYearMonth 同上。

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

经过的天数

您似乎想要一个基于天数的百分比。要获取经过的天数,请在 ChronoUnit 枚举类上使用 between 方法。

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

tod​​ay.toString(): 2019-06-09

stop.toString(): 2019-06-24

总天数:20

经过的天数:5

25%

验证输入

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

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

boolean startIsBeforeStop = start.isBefore( stop ) ;

您需要确认今天在这两个日期之间。

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

ThreeTen-Extra

如果您的工作经常涉及日期范围,请将ThreeTen-Extra 库添加到您的项目中。这为您提供了方便的 LocalDateRange 类。

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 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

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

从哪里获得 java.time 类?

【讨论】:

    【解决方案2】:

    这就是我所做的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多