gpt4 book ai didi

java - 使用 vector 计算日期之间的天数差异

转载 作者:行者123 更新时间:2023-12-01 18:58:24 27 4
gpt4 key购买 nike

基于帖子Calculating difference in days between dates

如何向 vector vE 和 vS 提供随机日期,然后返回日期之间的差异?还记得 vS 必须大于 vE 吗?实际上,我应该分为两种方法:随机日期和其他计算差异。

/*
* Randomizacao
*/
package random04DiferencaDataVetor;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Random04DiferencaDataVetor {

public static void main(String[] args) throws ParseException {


final long intervalo = 1000000000;
Random rnd = new Random();
String[] vE = new String[5];
String[] vS = new String[5];
for (int i = 0; i < vE.length; i++) {
/*
* arrumar vetores para gerar datas aleatorias
* lembrando que vS deve ser maior que vE
*/
retornaData();
}
}

static void retornaData() throws ParseException {
final long intervalo = 1000000000;
Random rnd = new Random();
// formatando as datas
DateFormat formato = new SimpleDateFormat("yyyy");
Date anoE = formato.parse("2012");
long timeE = anoE.getTime();
Date anoS = formato.parse("2013");
long timeS = anoS.getTime();
// define o intervalo de datas em 1 ano
long tempoIntervalo = timeE - timeS;
// randomiza a data de entrada
long rndTempoE = timeE + (long) (rnd.nextDouble() * tempoIntervalo);
// data entrada
String dataE = new SimpleDateFormat("hh:mm dd/MM/yyyy").format(rndTempoE);
// randomiza a data de saida
long rndTempoS = rndTempoE + (long) (rnd.nextDouble() * intervalo * 2);
// data de saida
String dataS = new SimpleDateFormat("hh:mm dd/MM/yyyy").format(rndTempoS);
// formato de data
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm dd/MM/yyyy");
try {
Date dataEnt = sdf.parse(dataE);
Date dataSaida = sdf.parse(dataS);
long differenceMilliSeconds = dataSaida.getTime() - dataEnt.getTime();
long days = differenceMilliSeconds / 1000 / 60 / 60 / 24;
long hours = (differenceMilliSeconds % (1000 * 60 * 60 * 24)) / 1000 / 60 / 60;
long minutes = (differenceMilliSeconds % (1000 * 60 * 60)) / 1000 / 60;
System.out.println(days + " days, " + hours + " hours, " + minutes + " minutes.");
} catch (ParseException e) {
e.printStackTrace();
}
}
}

最佳答案

  1. 您声明的是Array而不是Vector。您可以使用“vector 声明如下:

     Vector<String> vE = new Vector<String>();
    Vector<String> vS = new Vector<String>();

    但是您可能想使用 List/ArrayList 代替 Vector,如下所示:

     List<String> vE = new ArrayList<String>();
    List<String> vS = new ArrayList<String>();
  2. 要在 vector 中添加日期字符串,您可以使用 add 方法,如下所示:

     String dateString1 = "01/01/2012";
    vE.add(dateString1);
  3. 要为 Vector vE 添加 5 个日期,您可以执行以下操作:

     for (int i = 0; i < 5; i++) {
    int day = 1+ rnd.nextInt(28); //day from 1 to 28
    int month = 1+rnd.nextInt(12); //day from 1 to 12
    int year = 2000 +rnd.nextInt(13); //year from 2000 to 2012
    String dateString = month+"/"+day+"/"year;
    vE.add(dateString);
    }
  4. 您可能希望在 retornaData(); 方法中传递 Vector vE 来计算差异:

         //call in `main` method  outside the `for` loop as:
    retornaData(vE);

    //change method signature as
    static void retornaData(Vector<String> vE) throws ParseException {
  5. retornaData()内,您可能想要检索两个日期字符串并计算差异:

        String date1 = vE.get(0);//use some index
    String date2 = vE.get(1); //use some index

    //compute the difference between date1 and date2

如果您可以在示例程序中使用英语,我可能会尝试建议进一步的更正/改进。

关于java - 使用 vector 计算日期之间的天数差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com