Java如何获取两个日期之间的百分比

huangapple go评论68阅读模式
英文:

Java How to get the percentage between 2 datetimes

问题

private void getpercentage(String dateTimeStart, String dateTimeExpiration) {
    LocalDateTime start = LocalDateTime.parse(dateTimeStart.replace(" ", "T"));
    LocalDateTime end = LocalDateTime.parse(dateTimeExpiration.replace(" ", "T"));
    String start_date = start.toString().replace("T", " ");
    String end_date = end.toString().replace("T", " ");
    double differenceInSeconds = Duration.between(start, end).getSeconds();
    double percentage = (differenceInSeconds / 86400) * 100; // Assuming a day has 86400 seconds
    String p = Math.round(percentage) + "%";
    Log.d("type", "Date parsed: " + p);
}
英文:

I want get the percentage between two DateTimes so i can use a progress bar.

I have the following code i'm passing in two datetimes and doing the sum but i am getting an error.

private void getpercentage(String dateTimeStart, String dateTimeExpiration) {

    LocalDateTime start = LocalDateTime.parse(dateTimeStart.replace( " " , "T" ) );
    LocalDateTime end = LocalDateTime.parse(dateTimeExpiration.replace( " " , "T" ) );
    String start_date = start.toString().replace( "T", " " );
    String end_date = end.toString().replace( "T", " " );
    String p = Math.round( (end_date - start_date) * 100) + '%';
    Log.d("type", "Date parsed : " + p);

}

答案1

得分: 2

你在不考虑时区的情况下无法正确执行这个操作。

private void getpercentage(String dateTimeStart, String dateTimeExpiration) {
    ZoneId zone = ZoneId.systemDefault();
    
    ZonedDateTime start = LocalDateTime.parse(dateTimeStart.replace(" ", "T"))
            .atZone(zone);
    ZonedDateTime end = LocalDateTime.parse(dateTimeExpiration.replace(" ", "T"))
            .atZone(zone);
    
    long total = ChronoUnit.MICROS.between(start, end);
    long passed = ChronoUnit.MICROS.between(start, ZonedDateTime.now(zone));
    
    long percentage = passed * 100 / total;
    
    System.out.println(String.valueOf(percentage) + " %");
}

要查看一天中已经过去了多少:

getpercentage("2020-05-30 00:00:00", "2020-05-31 00:00:00");

当在我的时区 Europe/Copenhagen 运行时:

91 %

为了说明时区的影响,当在印度/马尔代夫时区运行时:

103 %

因此,在那个时区中,5月30日已经结束,我们已经进入5月31日的 3%。

使用 long 类型和 ChronoUnit.MICROS(微秒)可以适用于长达 290,000 年的时间跨度。对于更长的时间跨度,选择更粗的时间单位。对于更准确和更短的时间跨度,使用更细的时间单位,即纳秒。

Java 9 及更高版本

long percentage = Duration.between(start, ZonedDateTime.now(zone))
        .multipliedBy(100)
        .dividedBy(Duration.between(start, end));

在 Java 9 中引入了重载的 Duration.dividedBy(Duration) 方法。这使我们可以使用 Duration 类进行计算,因此我们不需要决定使用特定的时间单位。

英文:

You can’t do that correctly without taking time zone into account.

private void getpercentage(String dateTimeStart, String dateTimeExpiration) {
	ZoneId zone = ZoneId.systemDefault();
	
    ZonedDateTime start = LocalDateTime.parse(dateTimeStart.replace( " " , "T" ) )
    		.atZone(zone);
    ZonedDateTime end = LocalDateTime.parse(dateTimeExpiration.replace( " " , "T" ) )
    		.atZone(zone);
    
    long total = ChronoUnit.MICROS.between(start, end);
    long passed = ChronoUnit.MICROS.between(start, ZonedDateTime.now(zone));
    
    long percentage = passed * 100 / total;
    
    System.out.println(String.valueOf(percentage) + " %");
}

To see how much of the day has passed:

    getpercentage("2020-05-30 00:00:00", "2020-05-31 00:00:00");

When run right now in my time zone, Europe/Copenhagen:

> 91 %

To illustrate my point about time zone: when instead run in Indian/Maldives time zone:

> 103 %

So in that time zone 30th May is already over and we’re 3 % into 31st May.

Using the long type and ChronoUnit.MICROS (microseconds) will work for spans of time up to 290 000 years. For a longer span, choose a coarrser unit. For more accuracy and a shorter span, use a finer unit, that is, nanoseconds.

Java 9 and later

long percentage = Duration.between(start, ZonedDateTime.now(zone))
        .multipliedBy(100)
        .dividedBy(Duration.between(start, end));

The overloaded Duration.dividedBy(Duration) method was introduced in Java 9. This allows us to use the Duration class for the calculation so we don’t need to decide on a specific time unit.

huangapple
  • 本文由 发表于 2020年5月30日 19:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/62101906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定