用向量计算日期之间的天数差异

时间:2012-11-03 03:31:48

标签: java date random vector datediff

基于帖子Calculating difference in days between dates

如何使用随机日期提供向量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 个答案:

答案 0 :(得分: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. 要向量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. 您可能希望在vE方法中传递Vector retornaData();来计算差异:

         //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
    
  6. 如果您可以在示例程序中使用英语,我可以尝试建议进一步的更正/改进。