0

基于帖子计算日期之间的天数差异

如何为向量 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();
        }
    }
}
4

1 回答 1

4
  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. 要在向量中添加日期字符串,您可以使用add以下方法:

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

     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. 您可能希望vEretornaData();方法中传递 Vector 来计算差异:

         //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
    

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

于 2012-11-03T03:39:53.537 回答