第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JAVA:如何計(jì)算多個(gè)字符串日期的平均天數(shù)?

JAVA:如何計(jì)算多個(gè)字符串日期的平均天數(shù)?

PIPIONE 2023-10-12 16:46:11
我編寫了一個(gè)程序,它總是計(jì)算兩個(gè)日期之間的天數(shù)差異。我現(xiàn)在想總結(jié)所有差異天數(shù),并將其除以計(jì)數(shù),以便得到平均天數(shù)。有人能幫我嗎?有沒有辦法甚至避免 for 循環(huán)。import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class AVGDateCalculation {    public static void main (String [] args) throws ParseException{    String formatted =            "2014-04-28 ,2014-04-28 ,"            + "2015-10-26 ,2015-10-30 ,"            + "2015-07-30 ,2015-07-30 ,"            + "2015-04-14 ,2015-04-20 ,"            + "2013-11-14 ,2013-11-18 ,"            + "2014-04-16 ,2014-04-22 ,"            + "2014-11-19 ,2014-11-21 ,"            + "2019-10-01 ,2019-10-01 ";                String[] parts = formatted.split(",");                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                int count = 0;                int a;                long difference = 0L;                float daysBetween = 0.00f;                float averageDays = 0.00f;                Date dateBefore = null;                Date dateAfter = null;                for (a = 0; a<parts.length; a+=1) {                    dateBefore = sdf.parse(parts[a++]);                    count++;                    dateAfter = sdf.parse(parts[a+=0]);                    difference = dateAfter.getTime() - dateBefore.getTime();                    daysBetween = (difference / (1000*60*60*24));                        averageDays = (count / daysBetween);                    System.out.println(String.valueOf(dateAfter) + " - " + `String.valueOf(dateBefore));`                    System.out.println(String.valueOf(dateAfter.getTime()) + " - " + String.valueOf(dateBefore.getTime()));                    System.out.println(String.valueOf(daysBetween));                    System.out.println(String.valueOf(averageDays) + " days");                    System.out.println(String.valueOf(count));                }    }}
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

我寧愿使用LocalDate而不是SimpleDateFormat,因?yàn)樵谀抢锬憧梢愿p松地計(jì)算差異和許多其他事情。要分割成對(duì)出現(xiàn)的日期,您可以使用正則表達(dá)式在每隔一個(gè)逗號(hào)分隔字符串。


import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.time.temporal.ChronoUnit;

import java.util.LongSummaryStatistics;

import java.util.function.Function;

import java.util.regex.Pattern;


public class Test{


    public static void main(String[] args){

        String formatted

                = "2014-04-28 ,2014-04-28 ,"

                + "2015-10-26 ,2015-10-30 ,"

                + "2015-07-30 ,2015-07-30 ,"

                + "2015-04-14 ,2015-04-20 ,"

                + "2013-11-14 ,2013-11-18 ,"

                + "2014-04-16 ,2014-04-22 ,"

                + "2014-11-19 ,2014-11-21 ,"

                + "2019-10-01 ,2019-10-01 ";

        //a formater for your date pattern

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        //a function to calculate the date differnce for a given pair of dates of form "2014-04-28 ,2014-04-28"

        Function<String,Long> dateDiff = s ->  ChronoUnit.DAYS.between(

                        LocalDate.parse(s.split(",")[0].trim(), dtf), 

                        LocalDate.parse(s.split(",")[1].trim(), dtf));

        //split your original string at each second ',' with below regex

        LongSummaryStatistics statistics = Pattern.compile("(?<!\\G[\\d -]+),")

                .splitAsStream(formatted)

                .mapToLong(s -> dateDiff.apply(s))

                .summaryStatistics();  

        System.out.println(statistics);

    }

}

統(tǒng)計(jì)數(shù)據(jù)包含總和、計(jì)數(shù)、最小值、最大值和平均值。如果你只對(duì)平均值感興趣,也可以直接調(diào)用


    double average = Pattern.compile("(?<!\\G[\\d -]+),")

                .splitAsStream(formatted)

                .mapToLong(s -> dateDiff.apply(s))

                .average().getAsDouble(); 


查看完整回答
反對(duì) 回復(fù) 2023-10-12
?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

博士

ChronoUnit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// An enum delineating granularities of time.

.DAYS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // `DAYS` is one of the objects pre-defined on that enum.

.between(? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Calculates elapsed time.

? ? LocalDate.parse( "2015-10-26" ) ,? ? // `LocalDate` represents a date-only value, without time-of-day, without time zone.

? ? LocalDate.parse( "2015-10-30" )? ? ? // `LocalDate` by default parses strings that comply with standard ISO 8601 formats.

)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // Returns a `long`, the number of days elapsed. Uses Half-Open approach, where the beginning is *inclusive* while the ending is *exclusive*.?


4

錯(cuò)誤的班級(jí)

該類java.util.Date代表 UTC 中的某個(gè)時(shí)刻,而不是日期。此外,那個(gè)可怕的類在幾年前就被現(xiàn)代的java.time類取代了。

https://img1.sycdn.imooc.com/6527b29d0001962006560410.jpg

LocalDate

該類LocalDate表示僅日期值,沒有時(shí)間、時(shí)區(qū)或相對(duì)于 UTC 的偏移量。

您的輸入符合 ISO 8601,因此您可以直接解析。無需定義格式化模式。

LocalDate?start?=?LocalDate.parse(?"2015-10-26"?)?;

使用 計(jì)算經(jīng)過的時(shí)間Period。

Period?p?=?Period.of(?start?,?stop?)?;

或者直接詢問天數(shù)。

long?days?=?ChronoUnit.DAYS.between(?start?,?stop?)?;


查看完整回答
反對(duì) 回復(fù) 2023-10-12
  • 2 回答
  • 0 關(guān)注
  • 169 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)